-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweather.py
29 lines (25 loc) · 844 Bytes
/
weather.py
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
# Author: Alan Cunningham
# Date 30/05/2016
import requests
import json
try:
import configparser
except ImportError:
import ConfigParser as configparser
def get_weather_json():
config = configparser.ConfigParser()
config.read('config.py')
url = "https://api.darksky.net/forecast/"
api_key = config.get('darksky', 'api_key')
lon = config.get('darksky', 'lon')
lat = config.get('darksky', 'lat')
units = config.get('darksky', 'units')
# Make the weather request and save to a json file
request_url = url + api_key + "/" + lat + "," + lon + "?units=" + units
resp = requests.get(url=request_url)
result = json.loads(resp.text)
print("Retrieved weather for {country}".format(country=result["timezone"]))
with open("weather.json", "w") as outfile:
json.dump(result, outfile)
if __name__ == "__main__":
get_weather_json()