Skip to content

Commit

Permalink
Add OpenWeatherMap language support.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisveilleux committed May 12, 2021
1 parent cdfacb6 commit b15367d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class WeatherSkill(MycroftSkill):
def __init__(self):
super().__init__("WeatherSkill")
self.weather_api = OpenWeatherMapApi()
self.weather_api.set_language_parameter(self.lang)
self.weather_config = WeatherConfig(self.config_core, self.settings)
self.platform = self.config_core["enclosure"]["platform"]

Expand Down
74 changes: 73 additions & 1 deletion source/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,64 @@
from mycroft.api import Api
from .weather import WeatherReport

OPEN_WEATHER_MAP_LANGUAGES = (
"af",
"al",
"ar",
"bg",
"ca",
"cz",
"da",
"de",
"el",
"en",
"es",
"eu",
"fa",
"fi",
"fr",
"gl",
"he",
"hi",
"hr",
"hu",
"id",
"it",
"ja",
"kr",
"la",
"lt",
"mk",
"nl",
"no",
"pl",
"pt",
"pt_br",
"ro",
"ru",
"se",
"sk",
"sl",
"sp",
"sr",
"sv",
"th",
"tr",
"ua",
"uk",
"vi",
"zh_cn",
"zh_tw",
"zu"
)


class OpenWeatherMapApi(Api):
"""Use Open Weather Map's One Call API to retrieve weather information"""

def __init__(self):
super().__init__(path="owm")
self.language = "en"

def get_weather_for_coordinates(
self, measurement_system: str, latitude: float, longitude: float
Expand All @@ -42,10 +94,30 @@ def get_weather_for_coordinates(
:param longitude: the geologic longitude of the weather location
"""
query_parameters = dict(
exclude="minutely", lat=latitude, lon=longitude, units=measurement_system
exclude="minutely",
lang=self.language,
lat=latitude,
lon=longitude,
units=measurement_system
)
api_request = dict(path="/onecall", query=query_parameters)
response = self.request(api_request)
local_weather = WeatherReport(response)

return local_weather

def set_language_parameter(self, language_config: str):
"""
OWM supports 31 languages, see https://openweathermap.org/current#multi
Convert language code to owm language, if missing use 'en'
"""
special_cases = {"cs": "cz", "ko": "kr", "lv": "la"}
language_part_one, language_part_two = language_config.split('-')
if language_config.replace('-', '_') in OPEN_WEATHER_MAP_LANGUAGES:
self.language = language_config.replace('-', '_')
elif language_part_one in OPEN_WEATHER_MAP_LANGUAGES:
self.language = language_part_one
elif language_part_two in OPEN_WEATHER_MAP_LANGUAGES:
self.language = language_part_two
elif language_part_one in special_cases:
self.language = special_cases[language_part_one]

0 comments on commit b15367d

Please sign in to comment.