summaryrefslogtreecommitdiff
path: root/searx/engines/chefkoch.py
blob: cffa59a0fadaf502b7dec9f39f3bb9d4b31d8d54 (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
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Chefkoch is a German database of recipes.
"""

from datetime import datetime
from urllib.parse import urlencode

about = {
    'website': "https://www.chefkoch.de",
    'official_api_documentation': None,
    'use_official_api': False,
    'require_api_key': False,
    'results': 'JSON',
    'language': 'de',
}

paging = True
categories = []

number_of_results = 20
skip_premium = True


base_url = "https://api.chefkoch.de"
thumbnail_format = "crop-240x300"


def request(query, params):
    args = {'query': query, 'limit': number_of_results, 'offset': (params['pageno'] - 1) * number_of_results}
    params['url'] = f"{base_url}/v2/search-gateway/recipes?{urlencode(args)}"
    return params


def response(resp):
    results = []

    json = resp.json()

    for result in json['results']:
        recipe = result['recipe']

        if skip_premium and (recipe['isPremium'] or recipe['isPlus']):
            continue

        publishedDate = None
        if recipe['submissionDate']:
            publishedDate = datetime.strptime(result['recipe']['submissionDate'][:19], "%Y-%m-%dT%H:%M:%S")

        content = (
            "difficulity: "
            + str(recipe['difficulty'])
            + " / preparation time: "
            + str(recipe['preparationTime'])
            + "min / ingredient count: "
            + str(recipe['ingredientCount'])
        )

        if recipe['subtitle']:
            content = f"{recipe['subtitle']} / {content}"

        results.append(
            {
                'url': recipe['siteUrl'],
                'title': recipe['title'],
                'content': content,
                'thumbnail': recipe['previewImageUrlTemplate'].replace("<format>", thumbnail_format),
                'publishedDate': publishedDate,
            }
        )

    return results