Skip to content

added get_weather #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
.vscode
venv/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
|[Sha1-brutefore](https://github.com/adityaarakeri/super-scripts/tree/master/Sha1-brutefore) | [nitish](https://github.com/nitishsai9) |
|[portScanner](https://github.com/adityaarakeri/super-scripts/tree/master/portScanner) | [nitish](https://github.com/nitishsai9) |
|[loop_command](https://github.com/adityaarakeri/super-scripts/tree/master/loop_command) | [jeancsil](https://github.com/jeancsil) |
Expand Down
26 changes: 26 additions & 0 deletions get_weather/README.md
Original file line number Diff line number Diff line change
@@ -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"
```
53 changes: 53 additions & 0 deletions get_weather/get_weather.py
Original file line number Diff line number Diff line change
@@ -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))
7 changes: 7 additions & 0 deletions get_weather/requirements.txt
Original file line number Diff line number Diff line change
@@ -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