From 884eeb8541e0a4cf3d65c2a17e1c2f788cab7fb1 Mon Sep 17 00:00:00 2001 From: Cqoicebordel Date: Mon, 1 Jun 2015 00:00:32 +0200 Subject: New Qwant engines - Web - Images - News - Social media --- searx/engines/qwant.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 searx/engines/qwant.py (limited to 'searx/engines/qwant.py') diff --git a/searx/engines/qwant.py b/searx/engines/qwant.py new file mode 100644 index 000000000..91c12a19e --- /dev/null +++ b/searx/engines/qwant.py @@ -0,0 +1,66 @@ +""" + Qwant (Web) + + @website https://qwant.com/ + @provide-api not officially (https://api.qwant.com/api/search/) + + @using-api yes + @results JSON + @stable yes + @parse url, title, content +""" + +from urllib import urlencode +from json import loads + +# engine dependent config +categories = ['general'] +paging = True +language_support = True + +# search-url +url = 'https://api.qwant.com/api/search/web?count=10&offset={offset}&f=&{query}' + + +# do search-request +def request(query, params): + offset = (params['pageno'] - 1) * 10 + + params['url'] = url.format(query=urlencode({'q': query}), + offset=offset) + + # add language tag if specified + if params['language'] != 'all': + params['url'] += '&locale=' + params['language'].lower() + + return params + + +# get response from search-request +def response(resp): + results = [] + + search_results = loads(resp.text) + + # return empty array if there are no results + if 'data' not in search_results: + return [] + + data = search_results.get('data', {}) + + res = data.get('result', {}) + + # parse results + for result in res.get('items', {}): + + title = result['title'] + res_url = result['url'] + content = result['desc'] + + # append result + results.append({'title': title, + 'content': content, + 'url': res_url}) + + # return results + return results -- cgit v1.2.3 From f05087b93ac1ebef3bdacd353524bac0d8041832 Mon Sep 17 00:00:00 2001 From: Cqoicebordel Date: Tue, 2 Jun 2015 20:36:58 +0200 Subject: Refactor Use only one engine for the four search from Qwant --- searx/engines/qwant.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'searx/engines/qwant.py') diff --git a/searx/engines/qwant.py b/searx/engines/qwant.py index 91c12a19e..38bafb043 100644 --- a/searx/engines/qwant.py +++ b/searx/engines/qwant.py @@ -1,5 +1,5 @@ """ - Qwant (Web) + Qwant (Web, Images, News, Social) @website https://qwant.com/ @provide-api not officially (https://api.qwant.com/api/search/) @@ -12,21 +12,25 @@ from urllib import urlencode from json import loads +from datetime import datetime # engine dependent config -categories = ['general'] +categories = None paging = True language_support = True +search_url_keyword = None + # search-url -url = 'https://api.qwant.com/api/search/web?count=10&offset={offset}&f=&{query}' +url = 'https://api.qwant.com/api/search/{keyword}?count=10&offset={offset}&f=&{query}' # do search-request def request(query, params): offset = (params['pageno'] - 1) * 10 - params['url'] = url.format(query=urlencode({'q': query}), + params['url'] = url.format(keyword=search_url_keyword, + query=urlencode({'q': query}), offset=offset) # add language tag if specified @@ -57,10 +61,28 @@ def response(resp): res_url = result['url'] content = result['desc'] - # append result - results.append({'title': title, - 'content': content, - 'url': res_url}) + if search_url_keyword == 'web': + results.append({'title': title, + 'content': content, + 'url': res_url}) + + elif search_url_keyword == 'images': + thumbnail_src = result['thumbnail'] + img_src = result['media'] + results.append({'template': 'images.html', + 'url': res_url, + 'title': title, + 'content': '', + 'thumbnail_src': thumbnail_src, + 'img_src': img_src}) + + elif search_url_keyword == 'news' or search_url_keyword == 'social': + published_date = datetime.fromtimestamp(result['date'], None) + + results.append({'url': res_url, + 'title': title, + 'publishedDate': published_date, + 'content': content}) # return results return results -- cgit v1.2.3 From e0774c849c48373c7a49515d5d769c5868596494 Mon Sep 17 00:00:00 2001 From: Cqoicebordel Date: Tue, 2 Jun 2015 22:11:47 +0200 Subject: Removed the keywords from the settings in qwant engine --- searx/engines/qwant.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'searx/engines/qwant.py') diff --git a/searx/engines/qwant.py b/searx/engines/qwant.py index 38bafb043..872bd4e95 100644 --- a/searx/engines/qwant.py +++ b/searx/engines/qwant.py @@ -19,7 +19,10 @@ categories = None paging = True language_support = True -search_url_keyword = None +category_to_keyword = {'general': 'web', + 'images': 'images', + 'news': 'news', + 'social media': 'social'} # search-url url = 'https://api.qwant.com/api/search/{keyword}?count=10&offset={offset}&f=&{query}' @@ -29,9 +32,15 @@ url = 'https://api.qwant.com/api/search/{keyword}?count=10&offset={offset}&f=&{q def request(query, params): offset = (params['pageno'] - 1) * 10 - params['url'] = url.format(keyword=search_url_keyword, - query=urlencode({'q': query}), - offset=offset) + if categories[0] and categories[0] in category_to_keyword: + + params['url'] = url.format(keyword=category_to_keyword[categories[0]], + query=urlencode({'q': query}), + offset=offset) + else: + params['url'] = url.format(keyword='web', + query=urlencode({'q': query}), + offset=offset) # add language tag if specified if params['language'] != 'all': @@ -61,12 +70,12 @@ def response(resp): res_url = result['url'] content = result['desc'] - if search_url_keyword == 'web': + if category_to_keyword.get(categories[0], '') == 'web': results.append({'title': title, 'content': content, 'url': res_url}) - elif search_url_keyword == 'images': + elif category_to_keyword.get(categories[0], '') == 'images': thumbnail_src = result['thumbnail'] img_src = result['media'] results.append({'template': 'images.html', @@ -76,7 +85,8 @@ def response(resp): 'thumbnail_src': thumbnail_src, 'img_src': img_src}) - elif search_url_keyword == 'news' or search_url_keyword == 'social': + elif (category_to_keyword.get(categories[0], '') == 'news' or + category_to_keyword.get(categories[0], '') == 'social'): published_date = datetime.fromtimestamp(result['date'], None) results.append({'url': res_url, -- cgit v1.2.3