From 024b234ef0372973e8d206843ea38286102633fe Mon Sep 17 00:00:00 2001 From: SuperHelios <20555025+Imperator26@users.noreply.github.com> Date: Sun, 9 Jun 2019 11:43:12 +0200 Subject: [PATCH 1/7] feat(package/weather): Version 1.0 of weather package --- bridges/python/Pipfile | 1 + bridges/python/Pipfile.lock | 21 ++++- packages/weather/.gitignore | 1 + packages/weather/OpenWeatherMap.py | 102 +++++++++++++++++++++ packages/weather/README.md | 46 ++++++++++ packages/weather/__init__.py | 0 packages/weather/config/.gitkeep | 0 packages/weather/config/config.sample.json | 8 ++ packages/weather/data/.gitkeep | 0 packages/weather/data/answers/.gitkeep | 0 packages/weather/data/answers/en.json | 29 ++++++ packages/weather/data/expressions/.gitkeep | 0 packages/weather/data/expressions/en.json | 74 +++++++++++++++ packages/weather/version.txt | 1 + 14 files changed, 279 insertions(+), 4 deletions(-) create mode 100644 packages/weather/.gitignore create mode 100644 packages/weather/OpenWeatherMap.py create mode 100644 packages/weather/README.md create mode 100644 packages/weather/__init__.py create mode 100644 packages/weather/config/.gitkeep create mode 100644 packages/weather/config/config.sample.json create mode 100644 packages/weather/data/.gitkeep create mode 100644 packages/weather/data/answers/.gitkeep create mode 100644 packages/weather/data/answers/en.json create mode 100644 packages/weather/data/expressions/.gitkeep create mode 100644 packages/weather/data/expressions/en.json create mode 100644 packages/weather/version.txt diff --git a/bridges/python/Pipfile b/bridges/python/Pipfile index 5af4c007c..615e70bf5 100644 --- a/bridges/python/Pipfile +++ b/bridges/python/Pipfile @@ -8,5 +8,6 @@ requests = "==2.21.0" pytube = "==9.5.0" tinydb = "==3.9.0" beautifulsoup4 = "==4.7.1" +pyowm = "*" [dev-packages] diff --git a/bridges/python/Pipfile.lock b/bridges/python/Pipfile.lock index 54fc9aed1..38ad1a757 100644 --- a/bridges/python/Pipfile.lock +++ b/bridges/python/Pipfile.lock @@ -1,12 +1,10 @@ { "_meta": { "hash": { - "sha256": "73bde89b379ffec7cec145fa30f1ebb49c349204b730a94108151fd9b71b31ec" + "sha256": "a1854c42058dfa8c1cc04c1b87b2e08615bde6fb3a55b7603a00b995c0bea35e" }, "pipfile-spec": 6, - "requires": { - "python_version": "3.6" - }, + "requires": {}, "sources": [ { "name": "pypi", @@ -39,6 +37,13 @@ ], "version": "==3.0.4" }, + "geojson": { + "hashes": [ + "sha256:b175e00a76d923d6e7409de0784c147adcdd6e04b311b1d405895a4db3612c9d", + "sha256:b2bfb5c8e6b4b0c55dd139996317145aa8526146b3f8570586f9613c527a648a" + ], + "version": "==2.4.1" + }, "idna": { "hashes": [ "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407", @@ -46,6 +51,14 @@ ], "version": "==2.8" }, + "pyowm": { + "hashes": [ + "sha256:2ac88777565518b9e9aa3fc172c543ada4cbfc667d83775cd17c0e52ea6ccc86", + "sha256:8fd41a18536f4d6c432bc6d9ea69994efb1ea9b43688cf19523659b6f4d86cf7" + ], + "index": "pypi", + "version": "==2.10.0" + }, "pytube": { "hashes": [ "sha256:2a32f3475f063d25e7b7a7434a93b51d59aadbbda7ed24af48f097b2876c0964", diff --git a/packages/weather/.gitignore b/packages/weather/.gitignore new file mode 100644 index 000000000..29569f686 --- /dev/null +++ b/packages/weather/.gitignore @@ -0,0 +1 @@ +config/configuration25.sample.py diff --git a/packages/weather/OpenWeatherMap.py b/packages/weather/OpenWeatherMap.py new file mode 100644 index 000000000..7cabfcdbd --- /dev/null +++ b/packages/weather/OpenWeatherMap.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- + +import functools +from datetime import datetime + +from pyowm import OWM + +import utils + + +# Decorators + +def load_config(func): + @functools.wraps(func) + def wrapper_load_config(string, entities): + payload = dict() + payload["string"] = string + payload["entities"] = entities + + api_key = utils.config("api_key") + pro = utils.config("pro") + payload["temperature_units"] = utils.config("temperature_units") + + if (payload["temperature_units"] != "celsius") and (payload["temperature_units"] != "fahrenheit"): + return utils.output("end", "invalid_temperature_units", utils.translate("invalid_temperature_units")) + + if pro: + payload["owm"] = OWM(api_key, subscription_type="pro") + else: + payload["owm"] = OWM(api_key) + + return func(payload) + return wrapper_load_config + +def acquire_weather(func): + @functools.wraps(func) + def wrapper_acquire_weather(payload): + for item in payload["entities"]: + if item["entity"] == "city": + utils.output("inter", "acquiring", utils.translate("acquiring")) + + payload["city"] = item["sourceText"] + payload["observation"] = payload["owm"].weather_at_place(payload["city"]) + payload["wtr"] = payload["observation"].get_weather() + + return func(payload) + return utils.output("end", "request_error", utils.translate("request_error")) + + return wrapper_acquire_weather + + +# Methods + +@load_config +@acquire_weather +def current_weather(payload): + """ + Get the current weather. + """ + + detailed_status = payload["wtr"].get_detailed_status() + temperatures = payload["wtr"].get_temperature(payload["temperature_units"]) # {"temp_max": 10.5, "temp": 9.7, "temp_min": 9.0} + humidity = payload["wtr"].get_humidity() + wind = payload["wtr"].get_wind() # {"speed": 4.6, "deg": 330} + + return utils.output( + "end", + "current_weather", + utils.translate( + "current_weather", + { + "detailed_status": detailed_status.capitalize(), + "city": payload["city"], + "temperature": temperatures["temp"], + "temperature_units": payload["temperature_units"].capitalize(), + "humidity": humidity + } + ) + ) + +@load_config +@acquire_weather +def sunrise(payload): + """ + Get when the sun rises. + """ + + dt = payload["wtr"].get_sunrise_time("date") + + return utils.output("end", "sunrise", utils.translate("sunrise", {"time": dt.strftime("%H:%M:%S"), "city": payload["city"]})) + +@load_config +@acquire_weather +def sunset(payload): + """ + Get when the sun sets. + """ + + dt = payload["wtr"].get_sunset_time("date") + + return utils.output("end", "sunset", utils.translate("sunset", {"time": dt.strftime("%H:%M:%S"), "city": payload["city"]})) diff --git a/packages/weather/README.md b/packages/weather/README.md new file mode 100644 index 000000000..fa2002ab7 --- /dev/null +++ b/packages/weather/README.md @@ -0,0 +1,46 @@ +# Weather Package + +The weather package contains modules which include getting the latest weather forecast. + +## Modules + +### OpenWeatherMap + +#### Requirements +- PyOWM + +#### Usage + +1. Register a new account on [OpenWeatherMap Sign Up](https://openweathermap.org/sign_up) unless you already have one. +2. Generate a new API key on [OpenWeatherMap API keys](https://home.openweathermap.org/api_keys). +3. Duplicate the file `packages/weather/config/config.sample.json` and rename it `config.json`. +4. Copy the API key in `packages/weather/config/config.json` and set the other options to your liking. +5. This package uses PyOWM. To install it: from inside leon directory `cd bridges/python` +6. `pipenv install pyowm` +7. Done! + +``` +(en-US) +- "What's the weather like in Milan?" +- "When is the sun going to set in Rome?" + +(it-IT) +- "Che tempo fa a Milano?" +- "Quando tramonta il sole a Roma?" +... +``` + +#### Options +- `pro`: Set this to `true` if you have a premium subscription. +- `temperature_units`: Choose which temperature scale to use. ["celsius", "fahrenheit"] + +#### Links + +- [OpenWeatherMap API](https://developers.google.com/youtube/v3/getting-started) +- [PyOWM GitHub](https://github.com/csparpa/pyowm) +- [PyOWM Docs](https://pyowm.readthedocs.io/en/latest/) + +#### TODO +- Implement some sort of caching mechanism. PyOWM does support caching, but it's a bit messy. It might be easier to implement it from scratch, perhaps with some sort of db like TinyDb which is already supported by leon. +- Implement `pro` functionality for OWM. +- Add more weather services and related (UV, pollution, sky events, etc.). diff --git a/packages/weather/__init__.py b/packages/weather/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/packages/weather/config/.gitkeep b/packages/weather/config/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/packages/weather/config/config.sample.json b/packages/weather/config/config.sample.json new file mode 100644 index 000000000..d5a2f79c0 --- /dev/null +++ b/packages/weather/config/config.sample.json @@ -0,0 +1,8 @@ +{ + "OpenWeatherMap": { + "api_key": "YOUR_OPEN_WEATHER_MAP_TOKEN", + "pro": false, + "temperature_units": "celsius", + "options": {} + } +} diff --git a/packages/weather/data/.gitkeep b/packages/weather/data/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/packages/weather/data/answers/.gitkeep b/packages/weather/data/answers/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/packages/weather/data/answers/en.json b/packages/weather/data/answers/en.json new file mode 100644 index 000000000..d92bf60cd --- /dev/null +++ b/packages/weather/data/answers/en.json @@ -0,0 +1,29 @@ +{ + "OpenWeatherMap": { + "acquiring": [ + "I'll take a look.", + "Let me see..." + ], + "current_weather": [ + "%detailed_status% over %city%. It's %temperature% degree %temperature_units%. Humidity is %humidity% percent." + ], + "sunrise": [ + "Today the sun rises at %time% in %city%." + ], + "sunset": [ + "Today the sun sets set at %time% in %city%." + ], + "invalid_temperature_units": [ + "Invalid temperature unit. Check your config.json file." + ], + "settings_error": [ + "Please check your settings. An error occured because %reason%. The message is: %message%." + ], + "request_error": [ + "I could not understand your request." + ], + "connection_error": [ + "I'm having issuse reaching the server." + ] + } +} diff --git a/packages/weather/data/expressions/.gitkeep b/packages/weather/data/expressions/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/packages/weather/data/expressions/en.json b/packages/weather/data/expressions/en.json new file mode 100644 index 000000000..5f7cbb8b0 --- /dev/null +++ b/packages/weather/data/expressions/en.json @@ -0,0 +1,74 @@ +{ + "OpenWeatherMap": { + "current_weather": { + "expressions": [ + "What's the weather like in" + ], + "entities": [ + { + "type": "trim", + "name": "city", + "conditions": [ + { + "type": "after_last", + "from": "in" + }, + { + "type": "between", + "from": "in", + "to": "?" + } + ] + } + ] + }, + "sunrise": { + "expressions": [ + "When is the sun going to rise in", + "When does the sun rise in", + "When did the sun rise in" + ], + "entities": [ + { + "type": "trim", + "name": "city", + "conditions": [ + { + "type": "after_last", + "from": "in" + }, + { + "type": "between", + "from": "in", + "to": "?" + } + ] + } + ] + }, + "sunset": { + "expressions": [ + "When is the sun going to set in", + "When does the sun set in", + "When did the sun set in" + ], + "entities": [ + { + "type": "trim", + "name": "city", + "conditions": [ + { + "type": "after_last", + "from": "in" + }, + { + "type": "between", + "from": "in", + "to": "?" + } + ] + } + ] + } + } +} diff --git a/packages/weather/version.txt b/packages/weather/version.txt new file mode 100644 index 000000000..afaf360d3 --- /dev/null +++ b/packages/weather/version.txt @@ -0,0 +1 @@ +1.0.0 \ No newline at end of file From 9b35f200b5fa95cb3b32a58254f5cec8cf3fac9a Mon Sep 17 00:00:00 2001 From: SuperHelios <20555025+Imperator26@users.noreply.github.com> Date: Sun, 9 Jun 2019 16:08:42 +0200 Subject: [PATCH 2/7] feat(package/weather): Adds temperature specific expression --- packages/weather/OpenWeatherMap.py | 22 ++++++++++++++++++ packages/weather/data/answers/en.json | 3 +++ packages/weather/data/expressions/en.json | 28 +++++++++++++++++++++++ packages/weather/version.txt | 2 +- 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/packages/weather/OpenWeatherMap.py b/packages/weather/OpenWeatherMap.py index 7cabfcdbd..1dc1c0268 100644 --- a/packages/weather/OpenWeatherMap.py +++ b/packages/weather/OpenWeatherMap.py @@ -79,6 +79,28 @@ def current_weather(payload): ) ) +@load_config +@acquire_weather +def temperature(payload): + """ + Get the current temperature. + """ + + temperatures = payload["wtr"].get_temperature(payload["temperature_units"]) # {"temp_max": 10.5, "temp": 9.7, "temp_min": 9.0} + + return utils.output( + "end", + "temperature", + utils.translate( + "temperature", + { + "city": payload["city"], + "temperature": temperatures["temp"], + "temperature_units": payload["temperature_units"].capitalize() + } + ) + ) + @load_config @acquire_weather def sunrise(payload): diff --git a/packages/weather/data/answers/en.json b/packages/weather/data/answers/en.json index d92bf60cd..d5dc0676e 100644 --- a/packages/weather/data/answers/en.json +++ b/packages/weather/data/answers/en.json @@ -7,6 +7,9 @@ "current_weather": [ "%detailed_status% over %city%. It's %temperature% degree %temperature_units%. Humidity is %humidity% percent." ], + "temperature": [ + "It's %temperature% degree %temperature_units% in %city%." + ], "sunrise": [ "Today the sun rises at %time% in %city%." ], diff --git a/packages/weather/data/expressions/en.json b/packages/weather/data/expressions/en.json index 5f7cbb8b0..6772bc9f2 100644 --- a/packages/weather/data/expressions/en.json +++ b/packages/weather/data/expressions/en.json @@ -22,6 +22,34 @@ } ] }, + "temperature": { + "expressions": [ + "Is it cold in", + "How cold is it in", + "Is it hot in", + "How hot is it in", + "Is it warm in", + "How warm is it in", + "What's the temperature like in" + ], + "entities": [ + { + "type": "trim", + "name": "city", + "conditions": [ + { + "type": "after_last", + "from": "in" + }, + { + "type": "between", + "from": "in", + "to": "?" + } + ] + } + ] + }, "sunrise": { "expressions": [ "When is the sun going to rise in", diff --git a/packages/weather/version.txt b/packages/weather/version.txt index afaf360d3..7dea76edb 100644 --- a/packages/weather/version.txt +++ b/packages/weather/version.txt @@ -1 +1 @@ -1.0.0 \ No newline at end of file +1.0.1 From 37e8416055eefae6fc7cc658672b5b3d6f42d685 Mon Sep 17 00:00:00 2001 From: SuperHelios <20555025+Imperator26@users.noreply.github.com> Date: Sun, 9 Jun 2019 16:12:18 +0200 Subject: [PATCH 3/7] feat(package/weather): Adds humidity specific expression --- packages/weather/OpenWeatherMap.py | 21 +++++++++++++++++++++ packages/weather/data/answers/en.json | 3 +++ packages/weather/data/expressions/en.json | 23 +++++++++++++++++++++++ packages/weather/version.txt | 2 +- 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/weather/OpenWeatherMap.py b/packages/weather/OpenWeatherMap.py index 1dc1c0268..386750941 100644 --- a/packages/weather/OpenWeatherMap.py +++ b/packages/weather/OpenWeatherMap.py @@ -101,6 +101,27 @@ def temperature(payload): ) ) +@load_config +@acquire_weather +def humidity(payload): + """ + Get the current humidity. + """ + + humidity = payload["wtr"].get_humidity() + + return utils.output( + "end", + "humidity", + utils.translate( + "humidity", + { + "city": payload["city"], + "humidity": humidity + } + ) + ) + @load_config @acquire_weather def sunrise(payload): diff --git a/packages/weather/data/answers/en.json b/packages/weather/data/answers/en.json index d5dc0676e..1b37ed13d 100644 --- a/packages/weather/data/answers/en.json +++ b/packages/weather/data/answers/en.json @@ -10,6 +10,9 @@ "temperature": [ "It's %temperature% degree %temperature_units% in %city%." ], + "humidity": [ + "The humidity in %city% is %humidity% percent." + ], "sunrise": [ "Today the sun rises at %time% in %city%." ], diff --git a/packages/weather/data/expressions/en.json b/packages/weather/data/expressions/en.json index 6772bc9f2..87f44f2bd 100644 --- a/packages/weather/data/expressions/en.json +++ b/packages/weather/data/expressions/en.json @@ -50,6 +50,29 @@ } ] }, + "humidity": { + "expressions": [ + "Is it humid in", + "What is the humidity in" + ], + "entities": [ + { + "type": "trim", + "name": "city", + "conditions": [ + { + "type": "after_last", + "from": "in" + }, + { + "type": "between", + "from": "in", + "to": "?" + } + ] + } + ] + }, "sunrise": { "expressions": [ "When is the sun going to rise in", diff --git a/packages/weather/version.txt b/packages/weather/version.txt index 7dea76edb..6d7de6e6a 100644 --- a/packages/weather/version.txt +++ b/packages/weather/version.txt @@ -1 +1 @@ -1.0.1 +1.0.2 From ec8f4d6b5505e7ad2de8523fe0b263f0db6cdc5b Mon Sep 17 00:00:00 2001 From: SuperHelios <20555025+Imperator26@users.noreply.github.com> Date: Sun, 9 Jun 2019 17:12:31 +0200 Subject: [PATCH 4/7] feat(package/weather): Adds wind specific expression --- packages/weather/OpenWeatherMap.py | 37 ++++++++++++++++++++-- packages/weather/README.md | 2 ++ packages/weather/config/config.sample.json | 1 + packages/weather/data/answers/en.json | 8 ++++- packages/weather/data/expressions/en.json | 22 +++++++++++++ packages/weather/version.txt | 2 +- 6 files changed, 68 insertions(+), 4 deletions(-) diff --git a/packages/weather/OpenWeatherMap.py b/packages/weather/OpenWeatherMap.py index 386750941..86582551b 100644 --- a/packages/weather/OpenWeatherMap.py +++ b/packages/weather/OpenWeatherMap.py @@ -21,10 +21,20 @@ def wrapper_load_config(string, entities): api_key = utils.config("api_key") pro = utils.config("pro") payload["temperature_units"] = utils.config("temperature_units") + payload["wind_speed_units"] = utils.config("wind_speed_units") if (payload["temperature_units"] != "celsius") and (payload["temperature_units"] != "fahrenheit"): return utils.output("end", "invalid_temperature_units", utils.translate("invalid_temperature_units")) + if payload["wind_speed_units"] == "meters per seconds": + payload["wind_speed_units_response"] = payload["wind_speed_units"] + payload["wind_speed_units"] = "meters_sec" + elif payload["wind_speed_units"] == "miles per hour": + payload["wind_speed_units_response"] = payload["wind_speed_units"] + payload["wind_speed_units"] = "miles_hour" + else: + return utils.output("end", "invalid_wind_speed_units", utils.translate("invalid_wind_speed_units")) + if pro: payload["owm"] = OWM(api_key, subscription_type="pro") else: @@ -40,7 +50,7 @@ def wrapper_acquire_weather(payload): if item["entity"] == "city": utils.output("inter", "acquiring", utils.translate("acquiring")) - payload["city"] = item["sourceText"] + payload["city"] = item["sourceText"].title() payload["observation"] = payload["owm"].weather_at_place(payload["city"]) payload["wtr"] = payload["observation"].get_weather() @@ -62,7 +72,7 @@ def current_weather(payload): detailed_status = payload["wtr"].get_detailed_status() temperatures = payload["wtr"].get_temperature(payload["temperature_units"]) # {"temp_max": 10.5, "temp": 9.7, "temp_min": 9.0} humidity = payload["wtr"].get_humidity() - wind = payload["wtr"].get_wind() # {"speed": 4.6, "deg": 330} + #wind = payload["wtr"].get_wind(payload["wind_speed_units"]) # {"speed": 4.6, "deg": 330} return utils.output( "end", @@ -122,6 +132,29 @@ def humidity(payload): ) ) +@load_config +@acquire_weather +def wind(payload): + """ + Get the current wind speed and direction. + """ + + wind = payload["wtr"].get_wind(payload["wind_speed_units"]) + + return utils.output( + "end", + "wind", + utils.translate( + "wind", + { + "city": payload["city"], + "wind_speed": wind["speed"], + "wind_speed_units_response": payload["wind_speed_units_response"], + "wind_direction": wind["deg"] + } + ) + ) + @load_config @acquire_weather def sunrise(payload): diff --git a/packages/weather/README.md b/packages/weather/README.md index fa2002ab7..9fb4e3825 100644 --- a/packages/weather/README.md +++ b/packages/weather/README.md @@ -23,6 +23,7 @@ The weather package contains modules which include getting the latest weather fo (en-US) - "What's the weather like in Milan?" - "When is the sun going to set in Rome?" +- "Is it windy in Chicago?" (it-IT) - "Che tempo fa a Milano?" @@ -33,6 +34,7 @@ The weather package contains modules which include getting the latest weather fo #### Options - `pro`: Set this to `true` if you have a premium subscription. - `temperature_units`: Choose which temperature scale to use. ["celsius", "fahrenheit"] +- `wind_speed_units`: Choose which units to use for the wind speed. ["meters per second", "miles per hour"] More units coming soon. #### Links diff --git a/packages/weather/config/config.sample.json b/packages/weather/config/config.sample.json index d5a2f79c0..04c721a53 100644 --- a/packages/weather/config/config.sample.json +++ b/packages/weather/config/config.sample.json @@ -3,6 +3,7 @@ "api_key": "YOUR_OPEN_WEATHER_MAP_TOKEN", "pro": false, "temperature_units": "celsius", + "wind_speed_units": "meters per seconds", "options": {} } } diff --git a/packages/weather/data/answers/en.json b/packages/weather/data/answers/en.json index 1b37ed13d..fc3e04003 100644 --- a/packages/weather/data/answers/en.json +++ b/packages/weather/data/answers/en.json @@ -13,6 +13,9 @@ "humidity": [ "The humidity in %city% is %humidity% percent." ], + "wind": [ + "The wind in %city% is blowing at %wind_speed% %wind_speed_units_response% at %wind_direction% degree." + ], "sunrise": [ "Today the sun rises at %time% in %city%." ], @@ -20,7 +23,10 @@ "Today the sun sets set at %time% in %city%." ], "invalid_temperature_units": [ - "Invalid temperature unit. Check your config.json file." + "Invalid temperature units. Check your config.json file." + ], + "invalid_wind_speed_units": [ + "Invalid wind speed units. Check your config.json file." ], "settings_error": [ "Please check your settings. An error occured because %reason%. The message is: %message%." diff --git a/packages/weather/data/expressions/en.json b/packages/weather/data/expressions/en.json index 87f44f2bd..3078f87f6 100644 --- a/packages/weather/data/expressions/en.json +++ b/packages/weather/data/expressions/en.json @@ -73,6 +73,28 @@ } ] }, + "wind": { + "expressions": [ + "Is it windy in" + ], + "entities": [ + { + "type": "trim", + "name": "city", + "conditions": [ + { + "type": "after_last", + "from": "in" + }, + { + "type": "between", + "from": "in", + "to": "?" + } + ] + } + ] + }, "sunrise": { "expressions": [ "When is the sun going to rise in", diff --git a/packages/weather/version.txt b/packages/weather/version.txt index 6d7de6e6a..21e8796a0 100644 --- a/packages/weather/version.txt +++ b/packages/weather/version.txt @@ -1 +1 @@ -1.0.2 +1.0.3 From 24bd798c2b99c39188addee029540fce7c993e57 Mon Sep 17 00:00:00 2001 From: SuperHelios <20555025+Imperator26@users.noreply.github.com> Date: Wed, 12 Jun 2019 15:57:21 +0200 Subject: [PATCH 5/7] refactor(package/weather): Refactor code to PEP8 --- packages/weather/OpenWeatherMap.py | 63 ++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/packages/weather/OpenWeatherMap.py b/packages/weather/OpenWeatherMap.py index 86582551b..4b3fbed7e 100644 --- a/packages/weather/OpenWeatherMap.py +++ b/packages/weather/OpenWeatherMap.py @@ -2,12 +2,11 @@ # -*- coding:utf-8 -*- import functools -from datetime import datetime - -from pyowm import OWM import utils +from pyowm import OWM + # Decorators @@ -23,8 +22,15 @@ def wrapper_load_config(string, entities): payload["temperature_units"] = utils.config("temperature_units") payload["wind_speed_units"] = utils.config("wind_speed_units") - if (payload["temperature_units"] != "celsius") and (payload["temperature_units"] != "fahrenheit"): - return utils.output("end", "invalid_temperature_units", utils.translate("invalid_temperature_units")) + if ( + (payload["temperature_units"] != "celsius") + and (payload["temperature_units"] != "fahrenheit") + ): + return utils.output( + "end", + "invalid_temperature_units", + utils.translate("invalid_temperature_units") + ) if payload["wind_speed_units"] == "meters per seconds": payload["wind_speed_units_response"] = payload["wind_speed_units"] @@ -33,7 +39,11 @@ def wrapper_load_config(string, entities): payload["wind_speed_units_response"] = payload["wind_speed_units"] payload["wind_speed_units"] = "miles_hour" else: - return utils.output("end", "invalid_wind_speed_units", utils.translate("invalid_wind_speed_units")) + return utils.output( + "end", + "invalid_wind_speed_units", + utils.translate("invalid_wind_speed_units") + ) if pro: payload["owm"] = OWM(api_key, subscription_type="pro") @@ -43,19 +53,28 @@ def wrapper_load_config(string, entities): return func(payload) return wrapper_load_config + def acquire_weather(func): @functools.wraps(func) def wrapper_acquire_weather(payload): for item in payload["entities"]: if item["entity"] == "city": - utils.output("inter", "acquiring", utils.translate("acquiring")) + utils.output( + "inter", + "acquiring", + utils.translate("acquiring") + ) payload["city"] = item["sourceText"].title() payload["observation"] = payload["owm"].weather_at_place(payload["city"]) payload["wtr"] = payload["observation"].get_weather() return func(payload) - return utils.output("end", "request_error", utils.translate("request_error")) + return utils.output( + "end", + "request_error", + utils.translate("request_error") + ) return wrapper_acquire_weather @@ -70,9 +89,8 @@ def current_weather(payload): """ detailed_status = payload["wtr"].get_detailed_status() - temperatures = payload["wtr"].get_temperature(payload["temperature_units"]) # {"temp_max": 10.5, "temp": 9.7, "temp_min": 9.0} + temperatures = payload["wtr"].get_temperature(payload["temperature_units"]) humidity = payload["wtr"].get_humidity() - #wind = payload["wtr"].get_wind(payload["wind_speed_units"]) # {"speed": 4.6, "deg": 330} return utils.output( "end", @@ -89,6 +107,7 @@ def current_weather(payload): ) ) + @load_config @acquire_weather def temperature(payload): @@ -96,7 +115,7 @@ def temperature(payload): Get the current temperature. """ - temperatures = payload["wtr"].get_temperature(payload["temperature_units"]) # {"temp_max": 10.5, "temp": 9.7, "temp_min": 9.0} + temperatures = payload["wtr"].get_temperature(payload["temperature_units"]) return utils.output( "end", @@ -111,6 +130,7 @@ def temperature(payload): ) ) + @load_config @acquire_weather def humidity(payload): @@ -132,6 +152,7 @@ def humidity(payload): ) ) + @load_config @acquire_weather def wind(payload): @@ -155,6 +176,7 @@ def wind(payload): ) ) + @load_config @acquire_weather def sunrise(payload): @@ -164,7 +186,15 @@ def sunrise(payload): dt = payload["wtr"].get_sunrise_time("date") - return utils.output("end", "sunrise", utils.translate("sunrise", {"time": dt.strftime("%H:%M:%S"), "city": payload["city"]})) + return utils.output( + "end", + "sunrise", + utils.translate( + "sunrise", + {"time": dt.strftime("%H:%M:%S"), "city": payload["city"]} + ) + ) + @load_config @acquire_weather @@ -175,4 +205,11 @@ def sunset(payload): dt = payload["wtr"].get_sunset_time("date") - return utils.output("end", "sunset", utils.translate("sunset", {"time": dt.strftime("%H:%M:%S"), "city": payload["city"]})) + return utils.output( + "end", + "sunset", + utils.translate( + "sunset", + {"time": dt.strftime("%H:%M:%S"), "city": payload["city"]} + ) + ) From fa35df96af4e78e436a8db82ca325bfa41dfc38d Mon Sep 17 00:00:00 2001 From: SuperHelios <20555025+Imperator26@users.noreply.github.com> Date: Wed, 12 Jun 2019 15:59:23 +0200 Subject: [PATCH 6/7] perf(package/weather): Now the sunset and sunrise time is given based on the local timezone --- bridges/python/Pipfile | 2 ++ bridges/python/Pipfile.lock | 17 ++++++++++++++++- packages/weather/OpenWeatherMap.py | 3 +++ packages/weather/README.md | 3 ++- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/bridges/python/Pipfile b/bridges/python/Pipfile index 615e70bf5..9248ba366 100644 --- a/bridges/python/Pipfile +++ b/bridges/python/Pipfile @@ -9,5 +9,7 @@ pytube = "==9.5.0" tinydb = "==3.9.0" beautifulsoup4 = "==4.7.1" pyowm = "*" +pytz = "*" +tzlocal = "*" [dev-packages] diff --git a/bridges/python/Pipfile.lock b/bridges/python/Pipfile.lock index 38ad1a757..3dcfd811b 100644 --- a/bridges/python/Pipfile.lock +++ b/bridges/python/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "a1854c42058dfa8c1cc04c1b87b2e08615bde6fb3a55b7603a00b995c0bea35e" + "sha256": "c414207c953ddc3c5324996d81a1453f9185c7d112cfe8c704da6032badd1895" }, "pipfile-spec": 6, "requires": {}, @@ -67,6 +67,14 @@ "index": "pypi", "version": "==9.5.0" }, + "pytz": { + "hashes": [ + "sha256:303879e36b721603cc54604edcac9d20401bdbe31e1e4fdee5b9f98d5d31dfda", + "sha256:d747dd3d23d77ef44c6a3526e274af6efeb0a6f1afd5a69ba4d5be4098c8e141" + ], + "index": "pypi", + "version": "==2019.1" + }, "requests": { "hashes": [ "sha256:502a824f31acdacb3a35b6690b5fbf0bc41d63a24a45c4004352b0242707598e", @@ -90,6 +98,13 @@ "index": "pypi", "version": "==3.9.0" }, + "tzlocal": { + "hashes": [ + "sha256:4ebeb848845ac898da6519b9b31879cf13b6626f7184c496037b818e238f2c4e" + ], + "index": "pypi", + "version": "==1.5.1" + }, "urllib3": { "hashes": [ "sha256:2393a695cd12afedd0dcb26fe5d50d0cf248e5a66f75dbd89a3d4eb333a61af4", diff --git a/packages/weather/OpenWeatherMap.py b/packages/weather/OpenWeatherMap.py index 4b3fbed7e..ad0e7435a 100644 --- a/packages/weather/OpenWeatherMap.py +++ b/packages/weather/OpenWeatherMap.py @@ -5,6 +5,7 @@ import utils +from tzlocal import get_localzone from pyowm import OWM @@ -185,6 +186,7 @@ def sunrise(payload): """ dt = payload["wtr"].get_sunrise_time("date") + dt = dt.astimezone(get_localzone()) return utils.output( "end", @@ -204,6 +206,7 @@ def sunset(payload): """ dt = payload["wtr"].get_sunset_time("date") + dt = dt.astimezone(get_localzone()) return utils.output( "end", diff --git a/packages/weather/README.md b/packages/weather/README.md index 9fb4e3825..1e032897a 100644 --- a/packages/weather/README.md +++ b/packages/weather/README.md @@ -7,6 +7,7 @@ The weather package contains modules which include getting the latest weather fo ### OpenWeatherMap #### Requirements +- tzlocal - PyOWM #### Usage @@ -16,7 +17,7 @@ The weather package contains modules which include getting the latest weather fo 3. Duplicate the file `packages/weather/config/config.sample.json` and rename it `config.json`. 4. Copy the API key in `packages/weather/config/config.json` and set the other options to your liking. 5. This package uses PyOWM. To install it: from inside leon directory `cd bridges/python` -6. `pipenv install pyowm` +6. `pipenv install tzlocal pyowm` 7. Done! ``` From 833144a9549ddc7e3f00f2a1ed7addc58f83ae67 Mon Sep 17 00:00:00 2001 From: SuperHelios <20555025+Imperator26@users.noreply.github.com> Date: Sun, 16 Jun 2019 13:23:12 +0200 Subject: [PATCH 7/7] fix(package/weather): Set a specific version for additional libraries --- bridges/python/Pipfile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bridges/python/Pipfile b/bridges/python/Pipfile index 9248ba366..6c34a0665 100644 --- a/bridges/python/Pipfile +++ b/bridges/python/Pipfile @@ -8,8 +8,7 @@ requests = "==2.21.0" pytube = "==9.5.0" tinydb = "==3.9.0" beautifulsoup4 = "==4.7.1" -pyowm = "*" -pytz = "*" -tzlocal = "*" +pyowm = "==2.10.0" +tzlocal = "==1.5.1" [dev-packages]