-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpywapi.py
executable file
·299 lines (232 loc) · 11.1 KB
/
pywapi.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#Copyright (c) 2009 Eugene Kaznacheev <[email protected]>
#Permission is hereby granted, free of charge, to any person
#obtaining a copy of this software and associated documentation
#files (the "Software"), to deal in the Software without
#restriction, including without limitation the rights to use,
#copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the
#Software is furnished to do so, subject to the following
#conditions:
#The above copyright notice and this permission notice shall be
#included in all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#OTHER DEALINGS IN THE SOFTWARE.
"""
Fetches weather reports from Google Weather, Yahoo Wheather and NOAA
"""
import urllib2, string
from xml.dom import minidom
GOOGLE_WEATHER_URL = 'http://www.google.com/ig/api?weather=%s&hl=%s'
YAHOO_WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss?p=%s&u=%s'
YAHOO_WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'
NOAA_WEATHER_URL = 'http://www.weather.gov/xml/current_obs/%s.xml'
GISMETEO_WEATHER_URL = 'http://informer.gismeteo.ru/xml/%s.xml'
def get_weather_from_google(location_id, hl = ''):
"""
Fetches weather report from Google
Parameters
location_id: a zip code (10001); city name, state (weather=woodland,PA); city name, country (weather=london,england); or possibly others.
hl: the language parameter (language code)
Returns:
weather_data: a dictionary of weather data that exists in XML feed.
"""
url = GOOGLE_WEATHER_URL % (location_id, hl)
handler = urllib2.urlopen(url)
dom = minidom.parse(handler)
handler.close()
weather_data = {}
weather_dom = dom.getElementsByTagName('weather')[0]
data_structure = {
'forecast_information': ('city', 'postal_code', 'latitude_e6', 'longitude_e6', 'forecast_date', 'current_date_time', 'unit_system'),
'current_conditions': ('condition','temp_f', 'temp_c', 'humidity', 'wind_condition', 'icon')
}
for (tag, list_of_tags2) in data_structure.iteritems():
tmp_conditions = {}
for tag2 in list_of_tags2:
tmp_conditions[tag2] = weather_dom.getElementsByTagName(tag)[0].getElementsByTagName(tag2)[0].getAttribute('data')
weather_data[tag] = tmp_conditions
forecast_conditions = ('day_of_week', 'low', 'high', 'icon', 'condition')
forecasts = []
for forecast in dom.getElementsByTagName('forecast_conditions'):
tmp_forecast = {}
for tag in forecast_conditions:
tmp_forecast[tag] = forecast.getElementsByTagName(tag)[0].getAttribute('data')
forecasts.append(tmp_forecast)
weather_data['forecasts'] = forecasts
dom.unlink()
return weather_data
def get_weather_from_yahoo(location_id, units = 'metric'):
"""
Fetches weather report from Yahoo!
Parameters
location_id: A five digit US zip code or location ID. To find your location ID,
browse or search for your city from the Weather home page(http://weather.yahoo.com/)
The weather ID is in the URL for the forecast page for that city. You can also get the location ID by entering your zip code on the home page. For example, if you search for Los Angeles on the Weather home page, the forecast page for that city is http://weather.yahoo.com/forecast/USCA0638.html. The location ID is USCA0638.
units: type of units. 'metric' for metric and '' 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.
Returns:
weather_data: a dictionary of weather data that exists in XML feed. See http://developer.yahoo.com/weather/#channel
"""
if units == 'metric':
unit = 'c'
else:
unit = 'f'
url = YAHOO_WEATHER_URL % (location_id, unit)
handler = urllib2.urlopen(url)
dom = minidom.parse(handler)
handler.close()
weather_data = {}
weather_data['title'] = dom.getElementsByTagName('title')[0].firstChild.data
weather_data['link'] = dom.getElementsByTagName('link')[0].firstChild.data
ns_data_structure = {
'location': ('city', 'region', 'country'),
'units': ('temperature', 'distance', 'pressure', 'speed'),
'wind': ('chill', 'direction', 'speed'),
'atmosphere': ('humidity', 'visibility', 'pressure', 'rising'),
'astronomy': ('sunrise', 'sunset'),
'condition': ('text', 'code', 'temp', 'date')
}
for (tag, attrs) in ns_data_structure.iteritems():
weather_data[tag] = xml_get_ns_yahoo_tag(dom, YAHOO_WEATHER_NS, tag, attrs)
weather_data['geo'] = {}
weather_data['geo']['lat'] = dom.getElementsByTagName('geo:lat')[0].firstChild.data
weather_data['geo']['long'] = dom.getElementsByTagName('geo:long')[0].firstChild.data
weather_data['condition']['title'] = dom.getElementsByTagName('item')[0].getElementsByTagName('title')[0].firstChild.data
weather_data['html_description'] = dom.getElementsByTagName('item')[0].getElementsByTagName('description')[0].firstChild.data
forecasts = []
for forecast in dom.getElementsByTagNameNS(YAHOO_WEATHER_NS, 'forecast'):
forecasts.append(xml_get_attrs(forecast,('date', 'low', 'high', 'text', 'code')))
weather_data['forecasts'] = forecasts
dom.unlink()
return weather_data
def get_weather_from_noaa(station_id):
"""
Fetches weather report from NOAA: National Oceanic and Atmospheric Administration (United States)
Parameter:
station_id: the ID of the weather station near the necessary location
To find your station ID, perform the following steps:
1. Open this URL: http://www.weather.gov/xml/current_obs/seek.php?state=az&Find=Find
2. Select the necessary state state. Click 'Find'.
3. Find the necessary station in the 'Observation Location' column.
4. The station ID is in the URL for the weather page for that station.
For example if the weather page is http://weather.noaa.gov/weather/current/KPEO.html -- the station ID is KPEO.
Other way to get the station ID: use this library: http://code.google.com/p/python-weather/ and 'Weather.location2station' function.
Returns:
weather_data: a dictionary of weather data that exists in XML feed.
(useful icons: http://www.weather.gov/xml/current_obs/weather.php)
"""
url = NOAA_WEATHER_URL % (station_id)
handler = urllib2.urlopen(url)
dom = minidom.parse(handler)
handler.close()
data_structure = ('suggested_pickup',
'suggested_pickup_period',
'location',
'station_id',
'latitude',
'longitude',
'observation_time',
'observation_time_rfc822',
'weather',
'temperature_string',
'temp_f',
'temp_c',
'relative_humidity',
'wind_string',
'wind_dir',
'wind_degrees',
'wind_mph',
'wind_gust_mph',
'pressure_string',
'pressure_mb',
'pressure_in',
'dewpoint_string',
'dewpoint_f',
'dewpoint_c',
'heat_index_string',
'heat_index_f',
'heat_index_c',
'windchill_string',
'windchill_f',
'windchill_c',
'icon_url_base',
'icon_url_name',
'two_day_history_url',
'ob_url'
)
weather_data = {}
current_observation = dom.getElementsByTagName('current_observation')[0]
for tag in data_structure:
weather_data[tag] = current_observation.getElementsByTagName(tag)[0].firstChild.data
dom.unlink()
return weather_data
def get_weather_from_gismeteo(location_id):
"""
Fetches weather report from GisMeteo (russian weather service)
Parameters
location_id: A location ID of the necessary place. To find your location ID,
browse or search for your city from the Gismeteo page: http://informer.gismeteo.ru/xml.html?index=27612%CC%EE%F1%EA%E2%E0&&lang=ru (the page is in Russian)
The location ID is in the URL for the XML URL for that city.
For example, if you search for New York on the GisMeteo page, the XML URLfor that city is
http://informer.gismeteo.ru/xml/72503_1.xml. The location ID is 72503_1.
Returns:
weather_data: a dictionary of weather data(forecasts only, not current weather) that exists in XML feed. See http://informer.gismeteo.ru/xml.html?index=27612%CC%EE%F1%EA%E2%E0&&lang=ru (at the page bottom)
"""
url = GISMETEO_WEATHER_URL % (location_id)
handler = urllib2.urlopen(url)
dom = minidom.parse(handler)
handler.close()
forecast_data_structure = {
'PHENOMENA': ('cloudiness','precipitation', 'rpower', 'spower'),
'PRESSURE': ('max','min'),
'TEMPERATURE': ('max','min'),
'WIND': ('max','min', 'direction'),
'RELWET': ('max','min'),
'HEAT': ('max','min'),
}
town_tag_attr = ('index', 'sname', 'latitude','longitude')
forecast_tag_attr = ('day', 'month', 'year', 'hour', 'tod', 'predict', 'weekday')
weather_data = {}
weather_data['town'] = xml_get_attrs(dom.getElementsByTagName('TOWN')[0], town_tag_attr)
forecasts = []
xml_forecats_node = dom.getElementsByTagName('FORECAST')
for i in range(xml_forecats_node.length):
forecast = xml_forecats_node.item(i)
_tmp_forecast = {}
_tmp_forecast.update(xml_get_attrs(forecast, forecast_tag_attr))
for (tag, attrs) in forecast_data_structure.iteritems():
_tmp_forecast[string.lower(tag)] = xml_get_attrs(forecast.getElementsByTagName(tag)[0], attrs)
forecasts.append(_tmp_forecast)
dom.unlink()
weather_data['forecasts'] = forecasts
return weather_data
def xml_get_ns_yahoo_tag(dom, YAHOO_WEATHER_NS, tag, attrs):
"""
Parses the necessary tag and returns the dictionary with values
Parameters:
dom - DOM
YAHOO_WEATHER_NS - namespace
tag - necessary tag
attrs - tuple of attributes
Returns: a dictionary of elements
"""
element = dom.getElementsByTagNameNS(YAHOO_WEATHER_NS, tag)[0]
return xml_get_attrs(element,attrs)
def xml_get_attrs(xml_element, attrs):
"""
Returns the list of necessary attributes
Parameters:
element: xml element
attrs: tuple of attributes
Return: a dictionary of elements
"""
result = {}
for attr in attrs:
result[attr] = xml_element.getAttribute(attr)
return result