From dd9127492f85dc5cfa25fbb5351735ea2190b058 Mon Sep 17 00:00:00 2001 From: ta Date: Mon, 22 Aug 2022 17:35:07 +0700 Subject: [PATCH 1/5] add 9gag engine 9GAG is a social media website where users upload and share user-generated images and videos --- searx/engines/9gag.py | 66 +++++++++++++++++++++++++++++++++++++++++++ searx/settings.yml | 5 ++++ 2 files changed, 71 insertions(+) create mode 100644 searx/engines/9gag.py diff --git a/searx/engines/9gag.py b/searx/engines/9gag.py new file mode 100644 index 000000000..b142adc0e --- /dev/null +++ b/searx/engines/9gag.py @@ -0,0 +1,66 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +# pylint: disable=C0103 +"""9GAG (social media)""" + +from json import loads +from datetime import datetime +from urllib.parse import urlencode + +about = { + "website": 'https://9gag.com/', + "wikidata_id": 'Q277421', + "official_api_documentation": None, + "use_official_api": True, + "require_api_key": False, + "results": 'JSON', +} + +categories = ['social media'] +paging = True + +search_url = "https://9gag.com/v1/search-posts?{query}" +page_size = 10 + + +def request(query, params): + query = urlencode({'query': query, 'c': (params['pageno'] - 1) * page_size}) + + params['url'] = search_url.format(query=query) + + return params + + +def response(resp): + results = [] + + json_results = loads(resp.text)['data']['posts'] + + for result in json_results: + result_type = result['type'] + + if result_type == 'Photo': + results.append( + { + 'template': 'images.html', + 'url': result['url'], + 'title': result['title'], + 'content': result['description'], + 'publishedDate': datetime.utcfromtimestamp(result['creationTs']), + 'img_src': result['images']['image700']['url'], + 'thumbnail_src': result['images']['imageFbThumbnail']['url'], + } + ) + elif result_type == 'Animated': + results.append( + { + 'template': 'videos.html', + 'url': result['url'], + 'title': result['title'], + 'content': result['description'], + 'publishedDate': datetime.utcfromtimestamp(result['creationTs']), + 'thumbnail': result['images']['imageFbThumbnail']['url'], + } + ) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index a61be1071..73119d2d4 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -262,6 +262,11 @@ categories_as_tabs: social media: engines: + - name: 9gag + engine: 9gag + shortcut: 9g + disabled: true + - name: apk mirror engine: apkmirror timeout: 4.0 From d22f4690106777aafc4f2d19e11ca0a4f802c2fa Mon Sep 17 00:00:00 2001 From: ta Date: Mon, 22 Aug 2022 18:27:35 +0700 Subject: [PATCH 2/5] use `invalid-name` instead of `C0103` for pylint --- searx/engines/9gag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searx/engines/9gag.py b/searx/engines/9gag.py index b142adc0e..8fa909942 100644 --- a/searx/engines/9gag.py +++ b/searx/engines/9gag.py @@ -1,6 +1,6 @@ # SPDX-License-Identifier: AGPL-3.0-or-later # lint: pylint -# pylint: disable=C0103 +# pylint: disable=invalid-name """9GAG (social media)""" from json import loads From 79d06509c13699dcf498cd8f048f76a71536a321 Mon Sep 17 00:00:00 2001 From: ta Date: Tue, 23 Aug 2022 05:18:35 +0700 Subject: [PATCH 3/5] add tags as suggestions --- searx/engines/9gag.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/searx/engines/9gag.py b/searx/engines/9gag.py index 8fa909942..6482ca811 100644 --- a/searx/engines/9gag.py +++ b/searx/engines/9gag.py @@ -34,9 +34,9 @@ def request(query, params): def response(resp): results = [] - json_results = loads(resp.text)['data']['posts'] + json_results = loads(resp.text)['data'] - for result in json_results: + for result in json_results['posts']: result_type = result['type'] if result_type == 'Photo': @@ -63,4 +63,8 @@ def response(resp): } ) + if 'tags' in json_results: + for suggestion in json_results['tags']: + results.append({'suggestion': suggestion['key']}) + return results From 040e24f9adfdc13e70ba21271dee2b6edfd866c7 Mon Sep 17 00:00:00 2001 From: ta Date: Wed, 24 Aug 2022 16:48:31 +0700 Subject: [PATCH 4/5] support playing videos directly --- searx/engines/9gag.py | 1 + 1 file changed, 1 insertion(+) diff --git a/searx/engines/9gag.py b/searx/engines/9gag.py index 6482ca811..5c0f54c26 100644 --- a/searx/engines/9gag.py +++ b/searx/engines/9gag.py @@ -60,6 +60,7 @@ def response(resp): 'content': result['description'], 'publishedDate': datetime.utcfromtimestamp(result['creationTs']), 'thumbnail': result['images']['imageFbThumbnail']['url'], + 'iframe_src': result['images'].get('image460sv', {}).get('url'), } ) From cef7bbab228173db6f3a68a28b57e5dc241b9982 Mon Sep 17 00:00:00 2001 From: ta Date: Wed, 24 Aug 2022 18:33:11 +0700 Subject: [PATCH 5/5] get the not cropped version of the thumbnail when the image height is not too important --- searx/engines/9gag.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/searx/engines/9gag.py b/searx/engines/9gag.py index 5c0f54c26..d1846725c 100644 --- a/searx/engines/9gag.py +++ b/searx/engines/9gag.py @@ -39,6 +39,12 @@ def response(resp): for result in json_results['posts']: result_type = result['type'] + # Get the not cropped version of the thumbnail when the image height is not too important + if result['images']['image700']['height'] > 400: + thumbnail = result['images']['imageFbThumbnail']['url'] + else: + thumbnail = result['images']['image700']['url'] + if result_type == 'Photo': results.append( { @@ -48,7 +54,7 @@ def response(resp): 'content': result['description'], 'publishedDate': datetime.utcfromtimestamp(result['creationTs']), 'img_src': result['images']['image700']['url'], - 'thumbnail_src': result['images']['imageFbThumbnail']['url'], + 'thumbnail_src': thumbnail, } ) elif result_type == 'Animated': @@ -59,7 +65,7 @@ def response(resp): 'title': result['title'], 'content': result['description'], 'publishedDate': datetime.utcfromtimestamp(result['creationTs']), - 'thumbnail': result['images']['imageFbThumbnail']['url'], + 'thumbnail': thumbnail, 'iframe_src': result['images'].get('image460sv', {}).get('url'), } )