Ponysearch/searx/engines/gigablast.py

115 lines
3.1 KiB
Python
Raw Normal View History

"""
Gigablast (Web)
2015-12-22 20:25:57 +01:00
@website https://gigablast.com
@provide-api yes (https://gigablast.com/api.html)
@using-api yes
@results XML
@stable yes
@parse url, title, content
"""
2015-02-08 14:12:14 +01:00
2017-08-31 16:11:04 +02:00
import random
from json import loads
2015-10-16 12:05:50 +02:00
from time import time
from lxml.html import fromstring
2016-11-30 18:43:03 +01:00
from searx.url_utils import urlencode
from searx.utils import eval_xpath
2015-02-08 14:12:14 +01:00
# engine dependent config
categories = ['general']
paging = True
number_of_results = 10
language_support = True
safesearch = True
2015-02-08 14:12:14 +01:00
# search-url
2015-12-22 20:25:57 +01:00
base_url = 'https://gigablast.com/'
search_string = 'search?{query}'\
'&n={number_of_results}'\
'&c=main'\
'&s={offset}'\
'&format=json'\
'&qh=0'\
'&qlang={lang}'\
2016-06-12 00:06:31 +02:00
'&ff={safesearch}'\
'&rxiec={rxieu}'\
'&ulse={ulse}'\
2019-10-16 15:27:37 +02:00
'&rand={rxikd}'\
'&dbez={dbez}'
2015-02-08 14:12:14 +01:00
# specific xpath variables
results_xpath = '//response//result'
url_xpath = './/url'
title_xpath = './/title'
content_xpath = './/sum'
supported_languages_url = 'https://gigablast.com/search?&rxikd=1'
2016-08-07 05:19:21 +02:00
2015-02-08 14:12:14 +01:00
# do search-request
def request(query, params):
offset = (params['pageno'] - 1) * number_of_results
if params['language'] == 'all':
language = 'xx'
else:
language = params['language'].replace('-', '_').lower()
if language.split('-')[0] != 'zh':
language = language.split('-')[0]
if params['safesearch'] >= 1:
safesearch = 1
else:
safesearch = 0
2017-08-31 16:11:04 +02:00
# rxieu is some kind of hash from the search query, but accepts random atm
search_path = search_string.format(query=urlencode({'q': query}),
offset=offset,
number_of_results=number_of_results,
2017-08-31 16:11:04 +02:00
rxikd=int(time() * 1000),
2017-08-31 21:49:19 +02:00
rxieu=random.randint(1000000000, 9999999999),
ulse=random.randint(100000000, 999999999),
lang=language,
2019-10-16 15:27:37 +02:00
safesearch=safesearch,
dbez=random.randint(100000000, 999999999))
2015-02-08 14:12:14 +01:00
params['url'] = base_url + search_path
return params
# get response from search-request
def response(resp):
results = []
# parse results
response_json = loads(resp.text)
2015-02-08 14:12:14 +01:00
for result in response_json['results']:
2015-02-08 14:12:14 +01:00
# append result
results.append({'url': result['url'],
'title': result['title'],
'content': result['sum']})
2015-02-08 14:12:14 +01:00
# return results
return results
# get supported languages from their site
def _fetch_supported_languages(resp):
supported_languages = []
dom = fromstring(resp.text)
links = eval_xpath(dom, '//span[@id="menu2"]/a')
for link in links:
href = eval_xpath(link, './@href')[0].split('lang%3A')
if len(href) == 2:
code = href[1].split('_')
if len(code) == 2:
code = code[0] + '-' + code[1].upper()
else:
code = code[0]
supported_languages.append(code)
return supported_languages