67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
from json import loads
|
|
from urllib.parse import urlencode
|
|
from searx.utils import to_string, html_to_text
|
|
|
|
search_token = ''
|
|
host = ''
|
|
|
|
cookies = {}
|
|
headers = {
|
|
"Authorization": ''
|
|
}
|
|
'''Some engines might offer different result based on cookies or headers.
|
|
Possible use-case: To set safesearch cookie or header to moderate.'''
|
|
|
|
# parameters for engines with paging support
|
|
#
|
|
# number of results on each page
|
|
# (only needed if the site requires not a page number, but an offset)
|
|
page_size = 1
|
|
# number of the first page (usually 0 or 1)
|
|
first_page_num = 1
|
|
|
|
|
|
def request(query, params):
|
|
# query = urlencode({'q': query})[2:]
|
|
|
|
headers['Authorization'] = 'Bearer %s' % search_token
|
|
params['cookies'].update(cookies)
|
|
params['headers'].update(headers)
|
|
|
|
params['url'] = host + '/graphql?query={pages {search(query: "%s") {results{id title description path locale __typename} suggestions totalHits __typename} __typename }}' % query
|
|
return params
|
|
|
|
|
|
def identity(arg):
|
|
return arg
|
|
|
|
|
|
def response(resp):
|
|
results = []
|
|
json = loads(resp.text)
|
|
title_filter = identity
|
|
content_filter = identity
|
|
|
|
search_result = json['data']['pages']['search']
|
|
rs = search_result['results']
|
|
if not len(rs):
|
|
return results
|
|
for result in rs:
|
|
try:
|
|
url = result['path']
|
|
title = result['title']
|
|
except:
|
|
continue
|
|
results.append(
|
|
{
|
|
'url': host + to_string(url),
|
|
'title': to_string(title),
|
|
'content': '',
|
|
}
|
|
)
|
|
for suggestion in search_result['suggestions']:
|
|
results.append({'suggestion': suggestion})
|
|
return results
|