From 5cdf564082db0bcf0a5aedcac6ee6b80d6c6c52b Mon Sep 17 00:00:00 2001 From: Naomi Stuart Date: Mon, 14 Oct 2019 21:36:22 +1100 Subject: [PATCH 1/2] added get_weather --- .gitignore | 3 +- get_weather/README.md | 26 ++++++++++++++++++ get_weather/get_weather.py | 53 ++++++++++++++++++++++++++++++++++++ get_weather/requirements.txt | 7 +++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 get_weather/README.md create mode 100644 get_weather/get_weather.py create mode 100644 get_weather/requirements.txt diff --git a/.gitignore b/.gitignore index 600d2d3..04d8af2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.vscode \ No newline at end of file +.vscode +venv/ \ No newline at end of file diff --git a/get_weather/README.md b/get_weather/README.md new file mode 100644 index 0000000..48849b7 --- /dev/null +++ b/get_weather/README.md @@ -0,0 +1,26 @@ +# :sunny: get_weather +A python script which can be run in the command line to print out a city's current weather. + +## Pre-requisites +- Install all required packages: +```sh +$ pip install -r requirements.txt +``` +- [Sign up](https://home.openweathermap.org/users/sign_up) for an API key from OpenWeatherMap (note an API key can take up to 2 hours to activate), then set up an environment variable: +```sh +$ export API_KEY +$ API_KEY="YOUR_API_KEY_HERE" +``` + +## Usage +```sh +$ python get_weather.py "CITY_NAME" +``` + +### Examples +```sh +$ python get_weather.py chicago +$ python get_weather.py "hong kong" +$ python get_weather.py "taupo, new zealand" +$ python get_weather.py "london, GB" +``` \ No newline at end of file diff --git a/get_weather/get_weather.py b/get_weather/get_weather.py new file mode 100644 index 0000000..98f2e25 --- /dev/null +++ b/get_weather/get_weather.py @@ -0,0 +1,53 @@ +import argparse +import os +from pyowm import OWM, exceptions + +parser = argparse.ArgumentParser(description='Latest weather for a city') +parser.add_argument('city', type=str, help='name of city to display current weather conditions') +city = parser.parse_args().city + +owm = OWM(os.environ.get("API_KEY")) + +try: + observation = owm.weather_at_place(city) + weather = observation.get_weather() + +except exceptions.api_response_error.UnauthorizedError: + error_msg = "Invalid OpenWeatherMap API key" + error_msg += "\nSign up for an API key at https://home.openweathermap.org/users/sign_up" + error_msg += "\nNote a new API can take up to 2 hours to be activated" + print(error_msg) + +except exceptions.api_response_error.NotFoundError: + error_msg = "Cannot fetch weather for {}".format(city) + error_msg += "\nPlease enter a valid city name, e.g." + error_msg += "\n - chicago" + error_msg += "\n - \"hong kong\"" + error_msg += "\n - \"sydney, australia\"" + error_msg += "\n - \"London, GB\"" + print(error_msg) + +else: + weather_status = weather.get_detailed_status() + temperature_F = weather.get_temperature(unit="fahrenheit") + temperature_C = weather.get_temperature(unit="celsius") + cloud = weather.get_clouds() + humidity = weather.get_humidity() + rain = weather.get_rain() + snow = weather.get_snow() + + weather_summary = { + 'weather': weather_status if bool(weather_status) else {}, + 'temperature': "{}C / {}F".format(round(temperature_C['temp']), round(temperature_F['temp'])) if bool(temperature_F) else {}, + 'cloud coverage': "{}%".format(cloud) if bool(cloud) else {}, + 'humidity': "{}%".format(humidity) if bool(humidity) else {}, + 'rain': "{}mm in the last hour".format(rain['1h']) if (bool(rain) and '1h' in rain.keys()) else {}, + 'snow': "{}mm in the last hour".format(snow['1h']) if (bool(snow) and '1h' in snow.keys()) else {} + } + + heading = "{} weather provided by OpenWeatherMap".format(observation.get_location().get_name()) + print(heading) + print("-"*len(heading)) + for key, value in weather_summary.items(): + if bool(value): + print("{}: {}".format(key, value)) \ No newline at end of file diff --git a/get_weather/requirements.txt b/get_weather/requirements.txt new file mode 100644 index 0000000..67de608 --- /dev/null +++ b/get_weather/requirements.txt @@ -0,0 +1,7 @@ +certifi==2019.9.11 +chardet==3.0.4 +geojson==2.5.0 +idna==2.8 +pyowm==2.10.0 +requests==2.22.0 +urllib3==1.25.6 From f57bde8757bfc980b1c965d10e2a43a47bf1f058 Mon Sep 17 00:00:00 2001 From: Naomi Stuart Date: Mon, 21 Oct 2019 20:06:46 +1100 Subject: [PATCH 2/2] Updated root readme with get_weather --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 83c7fdb..965bca5 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ A list of super awesome scripts which help automate the boring tasks. | [arch_linux](https://github.com/adityaarakeri/super-scripts/tree/master/arch_linux) | [crouther](https://github.com/crouther) | [wifi_to_eth](https://github.com/adityaarakeri/super-scripts/tree/master/wifi_to_eth) | [crouther](https://github.com/crouther) |[file_finder](https://github.com/adityaarakeri/super-scripts/tree/master/file_finder) | [poszy](https://github.com/poszy) | -| [makere](https://github.com/adityaarakeri/super-scripts/tree/master/makere) | [aksJain0](https://github.com/aksJain0) +| [makere](https://github.com/adityaarakeri/super-scripts/tree/master/makere) | [aksJain0](https://github.com/aksJain0) +| [get_weather](https://github.com/adityaarakeri/super-scripts/tree/master/get_weather) | [naomistuart](https://github.com/naomistuart) ## Contribuition Guidelines - Make a **separate folder** for your script.