summaryrefslogtreecommitdiff
path: root/searx/engines/duckduckgo_weather.py
blob: 3d59c4b0a480d5c0fddf71e0955d5164520574ef (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""
DuckDuckGo Weather
~~~~~~~~~~~~~~~~~~
"""

from typing import TYPE_CHECKING
from json import loads
from urllib.parse import quote

from dateutil import parser as date_parser
from flask_babel import gettext

from searx.engines.duckduckgo import fetch_traits  # pylint: disable=unused-import
from searx.engines.duckduckgo import get_ddg_lang
from searx.enginelib.traits import EngineTraits

if TYPE_CHECKING:
    import logging

    logger: logging.Logger

traits: EngineTraits


about = {
    "website": 'https://duckduckgo.com/',
    "wikidata_id": 'Q12805',
    "official_api_documentation": None,
    "use_official_api": True,
    "require_api_key": False,
    "results": "JSON",
}

send_accept_language_header = True

# engine dependent config
categories = ["weather"]
URL = "https://duckduckgo.com/js/spice/forecast/{query}/{lang}"


def generate_condition_table(condition):
    res = ""

    res += f"<tr><td><b>{gettext('Condition')}</b></td>" f"<td><b>{condition['conditionCode']}</b></td></tr>"

    res += (
        f"<tr><td><b>{gettext('Temperature')}</b></td>"
        f"<td><b>{condition['temperature']}°C / {c_to_f(condition['temperature'])}°F</b></td></tr>"
    )

    res += (
        f"<tr><td>{gettext('Feels like')}</td><td>{condition['temperatureApparent']}°C / "
        f"{c_to_f(condition['temperatureApparent'])}°F</td></tr>"
    )

    res += (
        f"<tr><td>{gettext('Wind')}</td><td>{condition['windDirection']}° — "
        f"{(condition['windSpeed'] * 1.6093440006147):.2f} km/h / {condition['windSpeed']} mph</td></tr>"
    )

    res += f"<tr><td>{gettext('Visibility')}</td><td>{condition['visibility']} m</td>"

    res += f"<tr><td>{gettext('Humidity')}</td><td>{(condition['humidity'] * 100):.1f}%</td></tr>"

    return res


def generate_day_table(day):
    res = ""

    res += (
        f"<tr><td>{gettext('Min temp.')}</td><td>{day['temperatureMin']}°C / "
        f"{c_to_f(day['temperatureMin'])}°F</td></tr>"
    )
    res += (
        f"<tr><td>{gettext('Max temp.')}</td><td>{day['temperatureMax']}°C / "
        f"{c_to_f(day['temperatureMax'])}°F</td></tr>"
    )
    res += f"<tr><td>{gettext('UV index')}</td><td>{day['maxUvIndex']}</td></tr>"
    res += f"<tr><td>{gettext('Sunrise')}</td><td>{date_parser.parse(day['sunrise']).strftime('%H:%M')}</td></tr>"
    res += f"<tr><td>{gettext('Sunset')}</td><td>{date_parser.parse(day['sunset']).strftime('%H:%M')}</td></tr>"

    return res


def request(query, params):

    eng_region = traits.get_region(params['searxng_locale'], traits.all_locale)
    eng_lang = get_ddg_lang(traits, params['searxng_locale'])

    # !ddw paris :es-AR --> {'ad': 'es_AR', 'ah': 'ar-es', 'l': 'ar-es'}
    params['cookies']['ad'] = eng_lang
    params['cookies']['ah'] = eng_region
    params['cookies']['l'] = eng_region
    logger.debug("cookies: %s", params['cookies'])

    params["url"] = URL.format(query=quote(query), lang=eng_lang.split('_')[0])
    return params


def c_to_f(temperature):
    return "%.2f" % ((temperature * 1.8) + 32)


def response(resp):
    results = []

    if resp.text.strip() == "ddg_spice_forecast();":
        return []

    result = loads(resp.text[resp.text.find('\n') + 1 : resp.text.rfind('\n') - 2])

    current = result["currentWeather"]

    title = result['location']

    infobox = f"<h3>{gettext('Current condition')}</h3><table><tbody>"

    infobox += generate_condition_table(current)

    infobox += "</tbody></table>"

    last_date = None

    for time in result['forecastHourly']['hours']:
        current_time = date_parser.parse(time['forecastStart'])

        if last_date != current_time.date():
            if last_date is not None:
                infobox += "</tbody></table>"

            infobox += f"<h3>{current_time.strftime('%Y-%m-%d')}</h3>"

            infobox += "<table><tbody>"

            for day in result['forecastDaily']['days']:
                if date_parser.parse(day['forecastStart']).date() == current_time.date():
                    infobox += generate_day_table(day)

            infobox += "</tbody></table><table><tbody>"

        last_date = current_time.date()

        infobox += f"<tr><td rowspan=\"7\"><b>{current_time.strftime('%H:%M')}</b></td></tr>"

        infobox += generate_condition_table(time)

    infobox += "</tbody></table>"

    results.append(
        {
            "infobox": title,
            "content": infobox,
        }
    )

    return results