This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
83 lines (61 loc) · 2.91 KB
/
app.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import settings
import gcal
import forecastio
import sys
from pushbullet import Pushbullet
from geopy.geocoders import Nominatim
def main(argv):
geolocator = Nominatim()
pb = Pushbullet(settings.pushBulletApiKey)
print(pb.devices)
# push = pb.push_note("Today's Weather Update", "It's so cold. You should wear a jacket.")
location = geolocator.geocode(settings.home)
forecast = forecastio.load_forecast(settings.forecastioKey,location.latitude ,location.longitude )
# Get the upcoming events for the day with location information
events = gcal.get_events(hour_offset=settings.hoursAhead, include_calendars=settings.include_calendars)
precipType = 'None'
precipIntensity = 0
today = processDay(forecast)
# Conditionally set precip type and intensity because they do not exist if
# there is no chance of precipitation
if forecast.daily().data[0].precipProbability is not 0:
precipType = forecast.daily().data[0].precipType
precipIntensity = forecast.daily().data[0].precipIntensity
msg = 'You should wear '
clothingOption = 'summer clothes, it\'s warm today'
for key in sorted(settings.tempPreference, reverse=True):
if today['avgTemp'] < key:
clothingOption = settings.tempPreference[key]
else:
break
msg += clothingOption + '. '
if today['maxPrecipChance'] > settings.precipThreshold:
if precipType is not 'snow':
msg += 'Bring an umbrella, there is a ' + str(today['maxPrecipChance']*100) + '% chance of rain. '
else:
msg += 'You should layer up, there is a ' + str(today['maxPrecipChance']*100) + '% chance of snow. '
if today['avgCloudCover'] < 0.25:
msg += 'Consider some sunscreen/sunglasses, it\'s going to be sunny today.'
msg += '\nIt\'s going to be about ' + str(round(today['avgTemp'])) + '˚F today. (Low: ' + str(round(today['minTemp'])) + ', High: ' + str(round(today['maxTemp'])) +')'
print(msg)
pb.push_note("Today's Update", msg)
def processDay(forecast):
today = forecast.hourly().data
avgTemp = 0;
avgCloudCover = 0;
maxPrecipChance = today[0].precipProbability
maxTemp = today[0].apparentTemperature
minTemp = maxTemp
for i in range(settings.hoursAhead): # look at the coming hours of today, default 12 hours ahead
hr = today[i]
temp = hr.apparentTemperature
maxPrecipChance = hr.precipProbability if hr.precipProbability > maxPrecipChance else maxPrecipChance
avgCloudCover += hr.cloudCover
avgTemp += temp
maxTemp = temp if temp > maxTemp else maxTemp
minTemp = temp if temp < minTemp else minTemp
avgTemp /= settings.hoursAhead
avgCloudCover /= settings.hoursAhead
return {'maxTemp':maxTemp, 'minTemp':minTemp, 'avgTemp':avgTemp, 'avgCloudCover':avgCloudCover, 'maxPrecipChance':maxPrecipChance}
if __name__ == '__main__':
main(sys.argv)