.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
9gag.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=invalid-name
3"""9GAG (social media)"""
4
5from json import loads
6from datetime import datetime
7from urllib.parse import urlencode
8
9about = {
10 "website": 'https://9gag.com/',
11 "wikidata_id": 'Q277421',
12 "official_api_documentation": None,
13 "use_official_api": True,
14 "require_api_key": False,
15 "results": 'JSON',
16}
17
18categories = ['social media']
19paging = True
20
21search_url = "https://9gag.com/v1/search-posts?{query}"
22page_size = 10
23
24
25def request(query, params):
26 query = urlencode({'query': query, 'c': (params['pageno'] - 1) * page_size})
27
28 params['url'] = search_url.format(query=query)
29
30 return params
31
32
33def response(resp):
34 results = []
35
36 json_results = loads(resp.text)['data']
37
38 for result in json_results['posts']:
39 result_type = result['type']
40
41 # Get the not cropped version of the thumbnail when the image height is not too important
42 if result['images']['image700']['height'] > 400:
43 thumbnail = result['images']['imageFbThumbnail']['url']
44 else:
45 thumbnail = result['images']['image700']['url']
46
47 if result_type == 'Photo':
48 results.append(
49 {
50 'template': 'images.html',
51 'url': result['url'],
52 'title': result['title'],
53 'content': result['description'],
54 'publishedDate': datetime.utcfromtimestamp(result['creationTs']),
55 'img_src': result['images']['image700']['url'],
56 'thumbnail_src': thumbnail,
57 }
58 )
59 elif result_type == 'Animated':
60 results.append(
61 {
62 'template': 'videos.html',
63 'url': result['url'],
64 'title': result['title'],
65 'content': result['description'],
66 'publishedDate': datetime.utcfromtimestamp(result['creationTs']),
67 'thumbnail': thumbnail,
68 'iframe_src': result['images'].get('image460sv', {}).get('url'),
69 }
70 )
71
72 if 'tags' in json_results:
73 for suggestion in json_results['tags']:
74 results.append({'suggestion': suggestion['key']})
75
76 return results
response(resp)
Definition 9gag.py:33
request(query, params)
Definition 9gag.py:25