-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
155 lines (114 loc) · 4.09 KB
/
Main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
Created on 8 Feb 2014
@author: AbbyTheRat
"""
from Cli import Cli
from Cli import option
from Cli import CliHelpError
from oauth2client.client import AccessTokenRefreshError
import GoogleServices
import Flags
import CalendarSelection
import EventListing
from twisted.internet import reactor
from twisted.web.client import getPage
import WeatherParser
import xml.etree.ElementTree as et
import traceback
import sys
WEATHER_COM_URL = 'http://wxdata.weather.com/wxdata/weather/local/%s?unit=%s&cc=*'
def get_weather(location_id, units = 'metric'):
"""Fetch weather report from Weather.com
Parameters:
location_id: A location ID or 5 digit US zip code.
units: type of units. 'metric' for metric and 'imperial' for non-metric.
Note that choosing metric units changes all the weather units to metric.
For example, wind speed will be reported as kilometers per hour and
barometric pressure as millibars.
"""
if units == 'm':
unit = 'm'
elif units == 'i' or units == '': # for backwards compatibility
unit = ''
else:
unit = 'm' # fallback to metric
url = WEATHER_COM_URL % (location_id, unit)
#set up a callback for weather data
getPage(url).addCallbacks(
callback=fetch_xml_data,
errback=fetch_error)
def fetch_xml_data(data):
"""
this pulls the data from twisted once it has fetched the XML data
"""
xml_root = et.fromstring(data)
try:
#Attempt to parse the xml data into English instead of short tags.
wp = WeatherParser.weather_parser(xml_root)
#For some reasons that I have yet to learn. Twisted likes to get stuck in a loop if there's an error in the code
# unless exceptions are caught outside. Hence the current broad catch.
except:
traceback.print_exc()
#done! Close down twisted event loop
shutdownwebapp()
def fetch_error(err):
print "error ", err
#found an error, shutdown the event loop to avoid app hanging.
shutdownwebapp()
def shutdownwebapp():
reactor.stop()
class MyOptions(object):
"""
Define options for command line parameters.
"""
@option(default='localhost')
def getauth_host_name(self):
pass
@option(multiValued=True, default=[8080, 8090])
def getauth_host_port(self):
pass
@option(default='ERROR')
def getlogging_level(self):
pass
@option()
def isnoauth_local_webserver(self):
pass
@option(default='client_secret.json')
def getclient_secret(self):
pass
@option(default='CAXX0343:1:CA')
def getweather_location(self):
pass
@option(default='m')
def getmetric_unit(self):
pass
def main():
try:
#parse the commandline
myoptions = Cli(MyOptions).parseArguments()
except CliHelpError as helpError:
print helpError
return 0
#weather functions
get_weather(myoptions.getweather_location(), myoptions.getmetric_unit())
#create flags for Google Services
flags = Flags.Flags(myoptions.getauth_host_name(),
myoptions.getauth_host_port(),
myoptions.isnoauth_local_webserver(),
myoptions.getlogging_level())
#Create google services object. This get carried around for google api functions.
google_services = GoogleServices.GoogleServices(myoptions.getclient_secret(), flags)
try:
#connect, auth and return a connected service object
google_services = google_services.startgoogleservices()
except AccessTokenRefreshError:
print("The credentials have been revoked or expired, please re-run the application to re-authorize")
return 0
#returns a selected calendar - either from one stored in user file or return an option
select_calendar = CalendarSelection.calendarlisting(google_services)
calendarid = select_calendar.return_calendar_id()
EventListing.event_listing(google_services, calendarid)
#start twisted event loop for fetching weather data.
reactor.run()
if __name__ == '__main__':
main()