-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherForecast.py
51 lines (42 loc) · 1.39 KB
/
weatherForecast.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from pushover import init, Client
import json
import pprint
import sys
import urllib2
userKey = ""
apiKey = ""
def get_data(url):
try:
response = urllib2.urlopen(url)
except urllib2.HTTPError:
return None
return json.loads(response.read())
def send_message(message):
init(apiKey)
Client(userKey).send_message(message, title="Weather Forecast")
def load_settings():
with open("weather.conf") as f:
for line in f:
split = line.split("=")
if split[0] == "apiKey":
global apiKey
apiKey = split[1].rstrip()
elif split[0] == "userKey":
global userKey
userKey = split[1].rstrip()
def main():
load_settings()
url = "http://metservice.com/publicData/localforecast"
city = "Hamilton"
if len(sys.argv) == 2:
city = sys.argv[1]
weather = get_data(url + city)
if weather is None:
send_message("HTTP error has occured with weather forecast")
sys.exit()
today = weather['days'][0]
tomorrow = weather['days'][1]
message = "The weather for {tomoro} the {date} is {forecast} with a high of {high}, today's overnight low is {low}".format(tomoro=tomorrow['dow'], date=tomorrow['date'], forecast=tomorrow['forecast'], high=tomorrow['max'], low=today['min'])
send_message(message)
if __name__ == "__main__":
main()