forked from linuxacademy/content-python3-sysadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather
More file actions
executable file
·29 lines (19 loc) · 782 Bytes
/
weather
File metadata and controls
executable file
·29 lines (19 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/home/user/venvs/experiment/bin/python
import os
import requests
import sys
from argparse import ArgumentParser
parser = ArgumentParser(description='Get the current weather information for your zipcode')
parser.add_argument('zip', help='zip/postal code to get weather for')
parser.add_argument('--country', default='us', help='country zip/postal belongs to, default is "us"')
args = parser.parse_args()
api_key = os.getenv("OWM_API_KEY")
if not api_key:
print("Error: no 'OWM_API_KEY' provided")
sys.exit(1)
url = f"http://api.openweathermap.org/data/2.5/weather?zip={args.zip},{args.country}&appid={api_key}"
res = requests.get(url)
if res.status_code != 200:
print(f"Error talking to weather provider: {res.status_code}")
sys.exit(1)
print(res.json())