summaryrefslogtreecommitdiff
path: root/searx/engines/9gag.py
blob: 962e0af649467263cd88157d8fd9cdef46cd28a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=invalid-name
"""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']

    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(
                {
                    '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': thumbnail,
                }
            )
        elif result_type == 'Animated':
            results.append(
                {
                    'template': 'videos.html',
                    'url': result['url'],
                    'title': result['title'],
                    'content': result['description'],
                    'publishedDate': datetime.utcfromtimestamp(result['creationTs']),
                    'thumbnail': thumbnail,
                    'iframe_src': result['images'].get('image460sv', {}).get('url'),
                }
            )

    if 'tags' in json_results:
        for suggestion in json_results['tags']:
            results.append({'suggestion': suggestion['key']})

    return results