pywapi.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. #Copyright (c) 2009 Eugene Kaznacheev <qetzal@gmail.com>
  2. #Copyright (c) 2013 Joshua Tasker <jtasker@gmail.com>
  3. #Permission is hereby granted, free of charge, to any person
  4. #obtaining a copy of this software and associated documentation
  5. #files (the "Software"), to deal in the Software without
  6. #restriction, including without limitation the rights to use,
  7. #copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. #copies of the Software, and to permit persons to whom the
  9. #Software is furnished to do so, subject to the following
  10. #conditions:
  11. #The above copyright notice and this permission notice shall be
  12. #included in all copies or substantial portions of the Software.
  13. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. #EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. #OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. #NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. #WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. #FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. #OTHER DEALINGS IN THE SOFTWARE.
  21. """
  22. Fetches weather reports from Google Weather, Yahoo! Weather, Weather.com and NOAA
  23. """
  24. try:
  25. # Python 3 imports
  26. from urllib.request import urlopen
  27. from urllib.parse import quote
  28. from urllib.error import URLError
  29. except ImportError:
  30. # Python 2 imports
  31. from urllib2 import urlopen
  32. from urllib import quote
  33. from urllib2 import URLError
  34. import sys
  35. import re
  36. from xml.dom import minidom
  37. GOOGLE_COUNTRIES_URL = 'http://www.google.com/ig/countries?output=xml&hl=%s'
  38. GOOGLE_CITIES_URL = 'http://www.google.com/ig/cities?output=xml&country=%s&hl=%s'
  39. YAHOO_WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss/%s_%s.xml'
  40. YAHOO_WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'
  41. NOAA_WEATHER_URL = 'http://www.weather.gov/xml/current_obs/%s.xml'
  42. WEATHER_COM_URL = 'http://xml.weather.com/weather/local/%s?par=1138276742&key=15ee9c789ccd70f5&unit=%s&dayf=5&cc=*'
  43. #WEATHER_COM_SEARCH_URL = 'http://xml.weather.com/search/search?where=%s'
  44. #WUNDERGROUND_URL = 'http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=%s'
  45. def get_weather_from_weather_com(location_id, units = 'metric'):
  46. """
  47. Fetches weather report from Weather.com
  48. Parameters:
  49. location_id: A five digit US zip code or location ID. To find your location ID,
  50. browse or search for your city from the Weather.com home page (http://www.weather.com/)
  51. The weather ID is in the URL for the forecast page for that city. You can also get
  52. the location ID by entering your zip code on the home page. For example, if you
  53. search for Los Angeles on the Weather home page, the forecast page for that city
  54. is http://www.weather.com/weather/today/Los+Angeles+CA+USCA0638:1:US. The location
  55. ID is USCA0638.
  56. units: type of units. 'metric' for metric and '' for non-metric
  57. Note that choosing metric units changes all the weather units to metric,
  58. for example, wind speed will be reported as kilometers per hour and
  59. barometric pressure as millibars.
  60. Returns:
  61. weather_data: a dictionary of weather data that exists in XML feed.
  62. """
  63. location_id = quote(location_id)
  64. if units == 'metric':
  65. unit = 'm'
  66. else:
  67. unit = ''
  68. url = WEATHER_COM_URL % (location_id, unit)
  69. try:
  70. handler = urlopen(url)
  71. except URLError:
  72. return {'error': 'Could not connect to Weather.com'}
  73. if sys.version > '3':
  74. # Python 3
  75. content_type = dict(handler.getheaders())['Content-Type']
  76. else:
  77. # Python 2
  78. content_type = handler.info().dict['content-type']
  79. charset = re.search('charset\=(.*)',content_type).group(1)
  80. if not charset:
  81. charset = 'utf-8'
  82. if charset.lower() != 'utf-8':
  83. xml_response = handler.read().decode(charset).encode('utf-8')
  84. else:
  85. xml_response = handler.read()
  86. dom = minidom.parseString(xml_response)
  87. handler.close()
  88. try:
  89. weather_dom = dom.getElementsByTagName('weather')[0]
  90. except IndexError:
  91. error_data = {'error': dom.getElementsByTagName('error')[0].getElementsByTagName('err')[0].firstChild.data}
  92. dom.unlink()
  93. return error_data
  94. key_map = {'head':'units', 'ut':'temperature', 'ud':'distance', 'us':'speed', 'up':'pressure',
  95. 'ur':'rainfall', 'loc':'location', 'dnam':'name', 'lat':'lat', 'lon':'lon',
  96. 'cc':'current_conditions', 'lsup':'last_updated', 'obst':'station', 'tmp':'temperature',
  97. 'flik':'feels_like', 't':'text', 'icon':'icon', 'bar':'barometer', 'r':'reading',
  98. 'd':'direction', 'wind':'wind', 's':'speed', 'gust':'gust', 'hmid':'humidity',
  99. 'vis':'visibility', 'uv':'uv', 'i':'index', 'dewp':'dewpoint', 'moon':'moon_phase',
  100. 'hi':'high', 'low':'low', 'sunr':'sunrise', 'suns':'sunset', 'bt':'brief_text',
  101. 'ppcp':'chance_precip'}
  102. data_structure = {'head': ('ut', 'ud', 'us', 'up', 'ur'),
  103. 'loc': ('dnam', 'lat', 'lon'),
  104. 'cc': ('lsup', 'obst', 'tmp', 'flik', 't', 'icon', 'hmid', 'vis', 'dewp')}
  105. cc_structure = {'bar': ('r','d'),
  106. 'wind': ('s','gust','d','t'),
  107. 'uv': ('i','t'),
  108. 'moon': ('icon','t')}
  109. weather_data = {}
  110. for (tag, list_of_tags2) in data_structure.items():
  111. key = key_map[tag]
  112. weather_data[key] = {}
  113. for tag2 in list_of_tags2:
  114. key2 = key_map[tag2]
  115. weather_data[key][key2] = weather_dom.getElementsByTagName(tag)[0].getElementsByTagName(tag2)[0].firstChild.data
  116. cc_dom = weather_dom.getElementsByTagName('cc')[0]
  117. for (tag, list_of_tags2) in cc_structure.items():
  118. key = key_map[tag]
  119. weather_data['current_conditions'][key] = {}
  120. for tag2 in list_of_tags2:
  121. key2 = key_map[tag2]
  122. weather_data['current_conditions'][key][key2] = cc_dom.getElementsByTagName(tag)[0].getElementsByTagName(tag2)[0].firstChild.data
  123. forecasts = []
  124. time_of_day_map = {'d':'day', 'n':'night'}
  125. for forecast in weather_dom.getElementsByTagName('dayf')[0].getElementsByTagName('day'):
  126. tmp_forecast = {}
  127. tmp_forecast['day_of_week'] = forecast.getAttribute('t')
  128. tmp_forecast['date'] = forecast.getAttribute('dt')
  129. for tag in ('hi', 'low', 'sunr', 'suns'):
  130. key = key_map[tag]
  131. tmp_forecast[key] = forecast.getElementsByTagName(tag)[0].firstChild.data
  132. for part in forecast.getElementsByTagName('part'):
  133. time_of_day = time_of_day_map[part.getAttribute('p')]
  134. tmp_forecast[time_of_day] = {}
  135. for tag2 in ('icon', 't', 'bt', 'ppcp', 'hmid'):
  136. key2 = key_map[tag2]
  137. tmp_forecast[time_of_day][key2] = part.getElementsByTagName(tag2)[0].firstChild.data
  138. tmp_forecast[time_of_day]['wind'] = {}
  139. for tag2 in ('s', 'gust', 'd', 't'):
  140. key2 = key_map[tag2]
  141. tmp_forecast[time_of_day]['wind'][key2] = part.getElementsByTagName('wind')[0].getElementsByTagName(tag2)[0].firstChild.data
  142. forecasts.append(tmp_forecast)
  143. weather_data['forecasts'] = forecasts
  144. dom.unlink()
  145. return weather_data
  146. def get_countries_from_google(hl = ''):
  147. """
  148. Get list of countries in specified language from Google
  149. Parameters:
  150. hl: the language parameter (language code). Default value is empty string, in this case Google will use English.
  151. Returns:
  152. countries: a list of elements(all countries that exists in XML feed). Each element is a dictionary with 'name' and 'iso_code' keys.
  153. For example: [{'iso_code': 'US', 'name': 'USA'}, {'iso_code': 'FR', 'name': 'France'}]
  154. """
  155. url = GOOGLE_COUNTRIES_URL % hl
  156. try:
  157. handler = urlopen(url)
  158. except URLError:
  159. return [{'error':'Could not connect to Google'}]
  160. if sys.version > '3':
  161. # Python 3
  162. content_type = dict(handler.getheaders())['Content-Type']
  163. else:
  164. # Python 2
  165. content_type = handler.info().dict['content-type']
  166. charset = re.search('charset\=(.*)',content_type).group(1)
  167. if not charset:
  168. charset = 'utf-8'
  169. if charset.lower() != 'utf-8':
  170. xml_response = handler.read().decode(charset).encode('utf-8')
  171. else:
  172. xml_response = handler.read()
  173. dom = minidom.parseString(xml_response)
  174. handler.close()
  175. countries = []
  176. countries_dom = dom.getElementsByTagName('country')
  177. for country_dom in countries_dom:
  178. country = {}
  179. country['name'] = country_dom.getElementsByTagName('name')[0].getAttribute('data')
  180. country['iso_code'] = country_dom.getElementsByTagName('iso_code')[0].getAttribute('data')
  181. countries.append(country)
  182. dom.unlink()
  183. return countries
  184. def get_cities_from_google(country_code, hl = ''):
  185. """
  186. Get list of cities of necessary country in specified language from Google
  187. Parameters:
  188. country_code: code of the necessary country. For example 'de' or 'fr'.
  189. hl: the language parameter (language code). Default value is empty string, in this case Google will use English.
  190. Returns:
  191. cities: a list of elements(all cities that exists in XML feed). Each element is a dictionary with 'name', 'latitude_e6' and 'longitude_e6' keys. For example: [{'longitude_e6': '1750000', 'name': 'Bourges', 'latitude_e6': '47979999'}]
  192. """
  193. url = GOOGLE_CITIES_URL % (country_code.lower(), hl)
  194. try:
  195. handler = urlopen(url)
  196. except URLError:
  197. return [{'error':'Could not connect to Google'}]
  198. if sys.version > '3':
  199. # Python 3
  200. content_type = dict(handler.getheaders())['Content-Type']
  201. else:
  202. # Python 2
  203. content_type = handler.info().dict['content-type']
  204. charset = re.search('charset\=(.*)',content_type).group(1)
  205. if not charset:
  206. charset = 'utf-8'
  207. if charset.lower() != 'utf-8':
  208. xml_response = handler.read().decode(charset).encode('utf-8')
  209. else:
  210. xml_response = handler.read()
  211. dom = minidom.parseString(xml_response)
  212. handler.close()
  213. cities = []
  214. cities_dom = dom.getElementsByTagName('city')
  215. for city_dom in cities_dom:
  216. city = {}
  217. city['name'] = city_dom.getElementsByTagName('name')[0].getAttribute('data')
  218. city['latitude_e6'] = city_dom.getElementsByTagName('latitude_e6')[0].getAttribute('data')
  219. city['longitude_e6'] = city_dom.getElementsByTagName('longitude_e6')[0].getAttribute('data')
  220. cities.append(city)
  221. dom.unlink()
  222. return cities
  223. def get_weather_from_yahoo(location_id, units = 'metric'):
  224. """
  225. Fetches weather report from Yahoo! Weather
  226. Parameters:
  227. location_id: A five digit US zip code or location ID. To find your location ID,
  228. browse or search for your city from the Yahoo! Weather home page (http://weather.yahoo.com/)
  229. The weather ID is in the URL for the forecast page for that city. You can also get
  230. the location ID by entering your zip code on the home page. For example, if you
  231. search for Los Angeles on the Weather home page, the forecast page for that city
  232. is http://weather.yahoo.com/forecast/USCA0638.html. The location ID is USCA0638.
  233. units: type of units. 'metric' for metric and '' for non-metric
  234. Note that choosing metric units changes all the weather units to metric,
  235. for example, wind speed will be reported as kilometers per hour and
  236. barometric pressure as millibars.
  237. Returns:
  238. weather_data: a dictionary of weather data that exists in XML feed.
  239. See http://developer.yahoo.com/weather/#channel
  240. """
  241. location_id = quote(location_id)
  242. if units == 'metric':
  243. unit = 'c'
  244. else:
  245. unit = 'f'
  246. url = YAHOO_WEATHER_URL % (location_id, unit)
  247. try:
  248. handler = urlopen(url)
  249. except URLError:
  250. return {'error': 'Could not connect to Yahoo! Weather'}
  251. dom = minidom.parse(handler)
  252. handler.close()
  253. weather_data = {}
  254. try:
  255. weather_data['title'] = dom.getElementsByTagName('title')[0].firstChild.data
  256. weather_data['link'] = dom.getElementsByTagName('link')[0].firstChild.data
  257. except IndexError:
  258. error_data = {'error': dom.getElementsByTagName('item')[0].getElementsByTagName('title')[0].firstChild.data}
  259. dom.unlink()
  260. return error_data
  261. ns_data_structure = {
  262. 'location': ('city', 'region', 'country'),
  263. 'units': ('temperature', 'distance', 'pressure', 'speed'),
  264. 'wind': ('chill', 'direction', 'speed'),
  265. 'atmosphere': ('humidity', 'visibility', 'pressure', 'rising'),
  266. 'astronomy': ('sunrise', 'sunset'),
  267. 'condition': ('text', 'code', 'temp', 'date')
  268. }
  269. for (tag, attrs) in ns_data_structure.items():
  270. weather_data[tag] = xml_get_ns_yahoo_tag(dom, YAHOO_WEATHER_NS, tag, attrs)
  271. weather_data['geo'] = {}
  272. weather_data['geo']['lat'] = dom.getElementsByTagName('geo:lat')[0].firstChild.data
  273. weather_data['geo']['long'] = dom.getElementsByTagName('geo:long')[0].firstChild.data
  274. weather_data['condition']['title'] = dom.getElementsByTagName('item')[0].getElementsByTagName('title')[0].firstChild.data
  275. weather_data['html_description'] = dom.getElementsByTagName('item')[0].getElementsByTagName('description')[0].firstChild.data
  276. forecasts = []
  277. for forecast in dom.getElementsByTagNameNS(YAHOO_WEATHER_NS, 'forecast'):
  278. forecasts.append(xml_get_attrs(forecast,('day', 'date', 'low', 'high', 'text', 'code')))
  279. weather_data['forecasts'] = forecasts
  280. dom.unlink()
  281. return weather_data
  282. def get_everything_from_yahoo(country_code, cities):
  283. """
  284. Get all weather data from yahoo for a specific country.
  285. Parameters:
  286. country_code: A four letter code of the necessary country. For example 'GMXX' or 'FRXX'.
  287. cities: The number of cities for which to get data
  288. Returns:
  289. weather_reports: A dictionary containing weather data for each city
  290. """
  291. city_codes = yield_all_country_city_codes_yahoo(country_code, cities)
  292. weather_reports = {}
  293. for city_c in city_codes:
  294. weather_data = get_weather_from_yahoo(city_c)
  295. if ('error' in weather_data):
  296. return weather_data
  297. city = weather_data['location']['city']
  298. weather_reports[city] = weather_data
  299. return weather_reports
  300. def yield_all_country_city_codes_yahoo(country_code, cities):
  301. """
  302. Yield all cities codes for a specific country.
  303. Parameters:
  304. country_code: A four letter code of the necessary country. For example 'GMXX' or 'FRXX'.
  305. cities: The number of cities to yield
  306. Returns:
  307. country_city_codes: A generator containing the city codes
  308. """
  309. # cities stands for the number of available cities
  310. for i in range(1, cities + 1):
  311. yield ''.join([country_code, (4 - len(str(i))) * '0', str(i)])
  312. def get_weather_from_noaa(station_id):
  313. """
  314. Fetches weather report from NOAA: National Oceanic and Atmospheric Administration (United States)
  315. Parameter:
  316. station_id: the ID of the weather station near the necessary location
  317. To find your station ID, perform the following steps:
  318. 1. Open this URL: http://www.weather.gov/xml/current_obs/seek.php?state=az&Find=Find
  319. 2. Select the necessary state state. Click 'Find'.
  320. 3. Find the necessary station in the 'Observation Location' column.
  321. 4. The station ID is in the URL for the weather page for that station.
  322. For example if the weather page is http://weather.noaa.gov/weather/current/KPEO.html -- the station ID is KPEO.
  323. Other way to get the station ID: use this library: http://code.google.com/p/python-weather/ and 'Weather.location2station' function.
  324. Returns:
  325. weather_data: a dictionary of weather data that exists in XML feed.
  326. (useful icons: http://www.weather.gov/xml/current_obs/weather.php)
  327. """
  328. station_id = quote(station_id)
  329. url = NOAA_WEATHER_URL % (station_id)
  330. try:
  331. handler = urlopen(url)
  332. except URLError:
  333. return {'error': 'Could not connect to NOAA'}
  334. dom = minidom.parse(handler)
  335. handler.close()
  336. data_structure = ('suggested_pickup',
  337. 'suggested_pickup_period',
  338. 'location',
  339. 'station_id',
  340. 'latitude',
  341. 'longitude',
  342. 'observation_time',
  343. 'observation_time_rfc822',
  344. 'weather',
  345. 'temperature_string',
  346. 'temp_f',
  347. 'temp_c',
  348. 'relative_humidity',
  349. 'wind_string',
  350. 'wind_dir',
  351. 'wind_degrees',
  352. 'wind_mph',
  353. 'wind_gust_mph',
  354. 'pressure_string',
  355. 'pressure_mb',
  356. 'pressure_in',
  357. 'dewpoint_string',
  358. 'dewpoint_f',
  359. 'dewpoint_c',
  360. 'heat_index_string',
  361. 'heat_index_f',
  362. 'heat_index_c',
  363. 'windchill_string',
  364. 'windchill_f',
  365. 'windchill_c',
  366. 'icon_url_base',
  367. 'icon_url_name',
  368. 'two_day_history_url',
  369. 'ob_url'
  370. )
  371. weather_data = {}
  372. current_observation = dom.getElementsByTagName('current_observation')[0]
  373. for tag in data_structure:
  374. try:
  375. weather_data[tag] = current_observation.getElementsByTagName(tag)[0].firstChild.data
  376. except IndexError:
  377. pass
  378. dom.unlink()
  379. return weather_data
  380. def xml_get_ns_yahoo_tag(dom, ns, tag, attrs):
  381. """
  382. Parses the necessary tag and returns the dictionary with values
  383. Parameters:
  384. dom: DOM
  385. ns: namespace
  386. tag: necessary tag
  387. attrs: tuple of attributes
  388. Returns:
  389. a dictionary of elements
  390. """
  391. element = dom.getElementsByTagNameNS(ns, tag)[0]
  392. return xml_get_attrs(element,attrs)
  393. def xml_get_attrs(xml_element, attrs):
  394. """
  395. Returns the list of necessary attributes
  396. Parameters:
  397. element: xml element
  398. attrs: tuple of attributes
  399. Returns:
  400. a dictionary of elements
  401. """
  402. result = {}
  403. for attr in attrs:
  404. result[attr] = xml_element.getAttribute(attr)
  405. return result
  406. def wind_direction(degrees):
  407. """ Convert wind degrees to direction """
  408. try:
  409. degrees = int(degrees)
  410. except ValueError:
  411. return ''
  412. if degrees < 23 or degrees >= 338:
  413. return 'N'
  414. elif degrees < 68:
  415. return 'NE'
  416. elif degrees < 113:
  417. return 'E'
  418. elif degrees < 158:
  419. return 'SE'
  420. elif degrees < 203:
  421. return 'S'
  422. elif degrees < 248:
  423. return 'SW'
  424. elif degrees < 293:
  425. return 'W'
  426. elif degrees < 338:
  427. return 'NW'
  428. def wind_beaufort_scale(km_per_hour):
  429. """ Convert km/h to beaufort """
  430. try:
  431. km_per_hour = int(km_per_hour)
  432. except ValueError:
  433. return ''
  434. if km_per_hour < 1:
  435. return '0'
  436. elif km_per_hour <= 5.5:
  437. return '1'
  438. elif km_per_hour <= 11:
  439. return '2'
  440. elif km_per_hour <= 19:
  441. return '3'
  442. elif km_per_hour <= 28:
  443. return '4'
  444. elif km_per_hour <= 38:
  445. return '5'
  446. elif km_per_hour <= 49:
  447. return '6'
  448. elif km_per_hour <= 61:
  449. return '7'
  450. elif km_per_hour <= 74:
  451. return '8'
  452. elif km_per_hour <= 88:
  453. return '9'
  454. elif km_per_hour <= 102:
  455. return '10'
  456. elif km_per_hour <= 117:
  457. return '11'
  458. else:
  459. return '12'
  460. def getText(nodelist):
  461. rc = ""
  462. for node in nodelist:
  463. if node.nodeType == node.TEXT_NODE:
  464. rc = rc + node.data
  465. return rc