# 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"{gettext('Condition')}" f"{condition['conditionCode']}" res += ( f"{gettext('Temperature')}" f"{condition['temperature']}°C / {c_to_f(condition['temperature'])}°F" ) res += ( f"{gettext('Feels like')}{condition['temperatureApparent']}°C / " f"{c_to_f(condition['temperatureApparent'])}°F" ) res += ( f"{gettext('Wind')}{condition['windDirection']}° — " f"{(condition['windSpeed'] * 1.6093440006147):.2f} km/h / {condition['windSpeed']} mph" ) res += f"{gettext('Visibility')}{condition['visibility']} m" res += f"{gettext('Humidity')}{(condition['humidity'] * 100):.1f}%" return res def generate_day_table(day): res = "" res += ( f"{gettext('Min temp.')}{day['temperatureMin']}°C / " f"{c_to_f(day['temperatureMin'])}°F" ) res += ( f"{gettext('Max temp.')}{day['temperatureMax']}°C / " f"{c_to_f(day['temperatureMax'])}°F" ) res += f"{gettext('UV index')}{day['maxUvIndex']}" res += f"{gettext('Sunrise')}{date_parser.parse(day['sunrise']).strftime('%H:%M')}" res += f"{gettext('Sunset')}{date_parser.parse(day['sunset']).strftime('%H:%M')}" 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"

{gettext('Current condition')}

" infobox += generate_condition_table(current) infobox += "
" 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 += "" infobox += f"

{current_time.strftime('%Y-%m-%d')}

" infobox += "" for day in result['forecastDaily']['days']: if date_parser.parse(day['forecastStart']).date() == current_time.date(): infobox += generate_day_table(day) infobox += "
" last_date = current_time.date() infobox += f"" infobox += generate_condition_table(time) infobox += "
{current_time.strftime('%H:%M')}
" results.append( { "infobox": title, "content": infobox, } ) return results