summaryrefslogtreecommitdiff
path: root/searx/engines/9gag.py
blob: b142adc0ee0cd072cbd7dbcb598c06a868002288 (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
# 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