From 4fb26cd96d31fece21f488fda10f5bb00928a133 Mon Sep 17 00:00:00 2001 From: bonswouar Date: Mon, 4 Sep 2023 00:42:19 +0200 Subject: [PATCH] [fix] engine duckduckgo weather api changes --- searx/engines/duckduckgo_weather.py | 48 +++++++++++++---------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/searx/engines/duckduckgo_weather.py b/searx/engines/duckduckgo_weather.py index f239ce8f9..3d59c4b0a 100644 --- a/searx/engines/duckduckgo_weather.py +++ b/searx/engines/duckduckgo_weather.py @@ -9,7 +9,7 @@ from typing import TYPE_CHECKING from json import loads from urllib.parse import quote -from datetime import datetime +from dateutil import parser as date_parser from flask_babel import gettext from searx.engines.duckduckgo import fetch_traits # pylint: disable=unused-import @@ -43,24 +43,24 @@ URL = "https://duckduckgo.com/js/spice/forecast/{query}/{lang}" def generate_condition_table(condition): res = "" - res += f"{gettext('Condition')}" f"{condition['summary']}" + res += f"{gettext('Condition')}" f"{condition['conditionCode']}" res += ( f"{gettext('Temperature')}" - f"{f_to_c(condition['temperature'])}°C / {condition['temperature']}°F" + f"{condition['temperature']}°C / {c_to_f(condition['temperature'])}°F" ) res += ( - f"{gettext('Feels like')}{f_to_c(condition['apparentTemperature'])}°C / " - f"{condition['apparentTemperature']}°F" + f"{gettext('Feels like')}{condition['temperatureApparent']}°C / " + f"{c_to_f(condition['temperatureApparent'])}°F" ) res += ( - f"{gettext('Wind')}{condition['windBearing']}° — " + f"{gettext('Wind')}{condition['windDirection']}° — " f"{(condition['windSpeed'] * 1.6093440006147):.2f} km/h / {condition['windSpeed']} mph" ) - res += f"{gettext('Visibility')}{condition['visibility']} km" + res += f"{gettext('Visibility')}{condition['visibility']} m" res += f"{gettext('Humidity')}{(condition['humidity'] * 100):.1f}%" @@ -71,20 +71,16 @@ def generate_day_table(day): res = "" res += ( - f"{gettext('Min temp.')}{f_to_c(day['temperatureLow'])}°C / " - f"{day['temperatureLow']}°F" + f"{gettext('Min temp.')}{day['temperatureMin']}°C / " + f"{c_to_f(day['temperatureMin'])}°F" ) res += ( - f"{gettext('Max temp.')}{f_to_c(day['temperatureHigh'])}°C / " - f"{day['temperatureHigh']}°F" - ) - res += f"{gettext('UV index')}{day['uvIndex']}" - res += ( - f"{gettext('Sunrise')}{datetime.fromtimestamp(day['sunriseTime']).strftime('%H:%M')}" - ) - res += ( - f"{gettext('Sunset')}{datetime.fromtimestamp(day['sunsetTime']).strftime('%H:%M')}" + 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 @@ -104,8 +100,8 @@ def request(query, params): return params -def f_to_c(temperature): - return "%.2f" % ((temperature - 32) / 1.8) +def c_to_f(temperature): + return "%.2f" % ((temperature * 1.8) + 32) def response(resp): @@ -116,9 +112,9 @@ def response(resp): result = loads(resp.text[resp.text.find('\n') + 1 : resp.text.rfind('\n') - 2]) - current = result["currently"] + current = result["currentWeather"] - title = result['flags']['ddg-location'] + title = result['location'] infobox = f"

{gettext('Current condition')}

" @@ -128,8 +124,8 @@ def response(resp): last_date = None - for time in result['hourly']['data']: - current_time = datetime.fromtimestamp(time['time']) + 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: @@ -139,8 +135,8 @@ def response(resp): infobox += "
" - for day in result['daily']['data']: - if datetime.fromtimestamp(day['time']).date() == current_time.date(): + for day in result['forecastDaily']['days']: + if date_parser.parse(day['forecastStart']).date() == current_time.date(): infobox += generate_day_table(day) infobox += "
"