pywapi.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 = {
  92. "error": dom.getElementsByTagName("error")[0]
  93. .getElementsByTagName("err")[0]
  94. .firstChild.data
  95. }
  96. dom.unlink()
  97. return error_data
  98. key_map = {
  99. "head": "units",
  100. "ut": "temperature",
  101. "ud": "distance",
  102. "us": "speed",
  103. "up": "pressure",
  104. "ur": "rainfall",
  105. "loc": "location",
  106. "dnam": "name",
  107. "lat": "lat",
  108. "lon": "lon",
  109. "cc": "current_conditions",
  110. "lsup": "last_updated",
  111. "obst": "station",
  112. "tmp": "temperature",
  113. "flik": "feels_like",
  114. "t": "text",
  115. "icon": "icon",
  116. "bar": "barometer",
  117. "r": "reading",
  118. "d": "direction",
  119. "wind": "wind",
  120. "s": "speed",
  121. "gust": "gust",
  122. "hmid": "humidity",
  123. "vis": "visibility",
  124. "uv": "uv",
  125. "i": "index",
  126. "dewp": "dewpoint",
  127. "moon": "moon_phase",
  128. "hi": "high",
  129. "low": "low",
  130. "sunr": "sunrise",
  131. "suns": "sunset",
  132. "bt": "brief_text",
  133. "ppcp": "chance_precip",
  134. }
  135. data_structure = {
  136. "head": ("ut", "ud", "us", "up", "ur"),
  137. "loc": ("dnam", "lat", "lon"),
  138. "cc": ("lsup", "obst", "tmp", "flik", "t", "icon", "hmid", "vis", "dewp"),
  139. }
  140. cc_structure = {
  141. "bar": ("r", "d"),
  142. "wind": ("s", "gust", "d", "t"),
  143. "uv": ("i", "t"),
  144. "moon": ("icon", "t"),
  145. }
  146. weather_data = {}
  147. for (tag, list_of_tags2) in data_structure.items():
  148. key = key_map[tag]
  149. weather_data[key] = {}
  150. for tag2 in list_of_tags2:
  151. key2 = key_map[tag2]
  152. weather_data[key][key2] = (
  153. weather_dom.getElementsByTagName(tag)[0]
  154. .getElementsByTagName(tag2)[0]
  155. .firstChild.data
  156. )
  157. cc_dom = weather_dom.getElementsByTagName("cc")[0]
  158. for (tag, list_of_tags2) in cc_structure.items():
  159. key = key_map[tag]
  160. weather_data["current_conditions"][key] = {}
  161. for tag2 in list_of_tags2:
  162. key2 = key_map[tag2]
  163. weather_data["current_conditions"][key][key2] = (
  164. cc_dom.getElementsByTagName(tag)[0]
  165. .getElementsByTagName(tag2)[0]
  166. .firstChild.data
  167. )
  168. forecasts = []
  169. time_of_day_map = {"d": "day", "n": "night"}
  170. for forecast in weather_dom.getElementsByTagName("dayf")[0].getElementsByTagName(
  171. "day"
  172. ):
  173. tmp_forecast = {}
  174. tmp_forecast["day_of_week"] = forecast.getAttribute("t")
  175. tmp_forecast["date"] = forecast.getAttribute("dt")
  176. for tag in ("hi", "low", "sunr", "suns"):
  177. key = key_map[tag]
  178. tmp_forecast[key] = forecast.getElementsByTagName(tag)[0].firstChild.data
  179. for part in forecast.getElementsByTagName("part"):
  180. time_of_day = time_of_day_map[part.getAttribute("p")]
  181. tmp_forecast[time_of_day] = {}
  182. for tag2 in ("icon", "t", "bt", "ppcp", "hmid"):
  183. key2 = key_map[tag2]
  184. tmp_forecast[time_of_day][key2] = part.getElementsByTagName(tag2)[
  185. 0
  186. ].firstChild.data
  187. tmp_forecast[time_of_day]["wind"] = {}
  188. for tag2 in ("s", "gust", "d", "t"):
  189. key2 = key_map[tag2]
  190. tmp_forecast[time_of_day]["wind"][key2] = (
  191. part.getElementsByTagName("wind")[0]
  192. .getElementsByTagName(tag2)[0]
  193. .firstChild.data
  194. )
  195. forecasts.append(tmp_forecast)
  196. weather_data["forecasts"] = forecasts
  197. dom.unlink()
  198. return weather_data
  199. def get_countries_from_google(hl=""):
  200. """
  201. Get list of countries in specified language from Google
  202. Parameters:
  203. hl: the language parameter (language code). Default value is empty string, in this case Google will use English.
  204. Returns:
  205. countries: a list of elements(all countries that exists in XML feed). Each element is a dictionary with 'name' and 'iso_code' keys.
  206. For example: [{'iso_code': 'US', 'name': 'USA'}, {'iso_code': 'FR', 'name': 'France'}]
  207. """
  208. url = GOOGLE_COUNTRIES_URL % hl
  209. try:
  210. handler = urlopen(url)
  211. except URLError:
  212. return [{"error": "Could not connect to Google"}]
  213. if sys.version > "3":
  214. # Python 3
  215. content_type = dict(handler.getheaders())["Content-Type"]
  216. else:
  217. # Python 2
  218. content_type = handler.info().dict["content-type"]
  219. charset = re.search("charset\=(.*)", content_type).group(1)
  220. if not charset:
  221. charset = "utf-8"
  222. if charset.lower() != "utf-8":
  223. xml_response = handler.read().decode(charset).encode("utf-8")
  224. else:
  225. xml_response = handler.read()
  226. dom = minidom.parseString(xml_response)
  227. handler.close()
  228. countries = []
  229. countries_dom = dom.getElementsByTagName("country")
  230. for country_dom in countries_dom:
  231. country = {}
  232. country["name"] = country_dom.getElementsByTagName("name")[0].getAttribute(
  233. "data"
  234. )
  235. country["iso_code"] = country_dom.getElementsByTagName("iso_code")[
  236. 0
  237. ].getAttribute("data")
  238. countries.append(country)
  239. dom.unlink()
  240. return countries
  241. def get_cities_from_google(country_code, hl=""):
  242. """
  243. Get list of cities of necessary country in specified language from Google
  244. Parameters:
  245. country_code: code of the necessary country. For example 'de' or 'fr'.
  246. hl: the language parameter (language code). Default value is empty string, in this case Google will use English.
  247. Returns:
  248. 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'}]
  249. """
  250. url = GOOGLE_CITIES_URL % (country_code.lower(), hl)
  251. try:
  252. handler = urlopen(url)
  253. except URLError:
  254. return [{"error": "Could not connect to Google"}]
  255. if sys.version > "3":
  256. # Python 3
  257. content_type = dict(handler.getheaders())["Content-Type"]
  258. else:
  259. # Python 2
  260. content_type = handler.info().dict["content-type"]
  261. charset = re.search("charset\=(.*)", content_type).group(1)
  262. if not charset:
  263. charset = "utf-8"
  264. if charset.lower() != "utf-8":
  265. xml_response = handler.read().decode(charset).encode("utf-8")
  266. else:
  267. xml_response = handler.read()
  268. dom = minidom.parseString(xml_response)
  269. handler.close()
  270. cities = []
  271. cities_dom = dom.getElementsByTagName("city")
  272. for city_dom in cities_dom:
  273. city = {}
  274. city["name"] = city_dom.getElementsByTagName("name")[0].getAttribute("data")
  275. city["latitude_e6"] = city_dom.getElementsByTagName("latitude_e6")[
  276. 0
  277. ].getAttribute("data")
  278. city["longitude_e6"] = city_dom.getElementsByTagName("longitude_e6")[
  279. 0
  280. ].getAttribute("data")
  281. cities.append(city)
  282. dom.unlink()
  283. return cities
  284. def get_weather_from_yahoo(location_id, units="metric"):
  285. """
  286. Fetches weather report from Yahoo! Weather
  287. Parameters:
  288. location_id: A five digit US zip code or location ID. To find your location ID,
  289. browse or search for your city from the Yahoo! Weather home page (http://weather.yahoo.com/)
  290. The weather ID is in the URL for the forecast page for that city. You can also get
  291. the location ID by entering your zip code on the home page. For example, if you
  292. search for Los Angeles on the Weather home page, the forecast page for that city
  293. is http://weather.yahoo.com/forecast/USCA0638.html. The location ID is USCA0638.
  294. units: type of units. 'metric' for metric and '' for non-metric
  295. Note that choosing metric units changes all the weather units to metric,
  296. for example, wind speed will be reported as kilometers per hour and
  297. barometric pressure as millibars.
  298. Returns:
  299. weather_data: a dictionary of weather data that exists in XML feed.
  300. See http://developer.yahoo.com/weather/#channel
  301. """
  302. location_id = quote(location_id)
  303. if units == "metric":
  304. unit = "c"
  305. else:
  306. unit = "f"
  307. url = YAHOO_WEATHER_URL % (location_id, unit)
  308. try:
  309. handler = urlopen(url)
  310. except URLError:
  311. return {"error": "Could not connect to Yahoo! Weather"}
  312. dom = minidom.parse(handler)
  313. handler.close()
  314. weather_data = {}
  315. try:
  316. weather_data["title"] = dom.getElementsByTagName("title")[0].firstChild.data
  317. weather_data["link"] = dom.getElementsByTagName("link")[0].firstChild.data
  318. except IndexError:
  319. error_data = {
  320. "error": dom.getElementsByTagName("item")[0]
  321. .getElementsByTagName("title")[0]
  322. .firstChild.data
  323. }
  324. dom.unlink()
  325. return error_data
  326. ns_data_structure = {
  327. "location": ("city", "region", "country"),
  328. "units": ("temperature", "distance", "pressure", "speed"),
  329. "wind": ("chill", "direction", "speed"),
  330. "atmosphere": ("humidity", "visibility", "pressure", "rising"),
  331. "astronomy": ("sunrise", "sunset"),
  332. "condition": ("text", "code", "temp", "date"),
  333. }
  334. for (tag, attrs) in ns_data_structure.items():
  335. weather_data[tag] = xml_get_ns_yahoo_tag(dom, YAHOO_WEATHER_NS, tag, attrs)
  336. weather_data["geo"] = {}
  337. weather_data["geo"]["lat"] = dom.getElementsByTagName("geo:lat")[0].firstChild.data
  338. weather_data["geo"]["long"] = dom.getElementsByTagName("geo:long")[
  339. 0
  340. ].firstChild.data
  341. weather_data["condition"]["title"] = (
  342. dom.getElementsByTagName("item")[0]
  343. .getElementsByTagName("title")[0]
  344. .firstChild.data
  345. )
  346. weather_data["html_description"] = (
  347. dom.getElementsByTagName("item")[0]
  348. .getElementsByTagName("description")[0]
  349. .firstChild.data
  350. )
  351. forecasts = []
  352. for forecast in dom.getElementsByTagNameNS(YAHOO_WEATHER_NS, "forecast"):
  353. forecasts.append(
  354. xml_get_attrs(forecast, ("day", "date", "low", "high", "text", "code"))
  355. )
  356. weather_data["forecasts"] = forecasts
  357. dom.unlink()
  358. return weather_data
  359. def get_everything_from_yahoo(country_code, cities):
  360. """
  361. Get all weather data from yahoo for a specific country.
  362. Parameters:
  363. country_code: A four letter code of the necessary country. For example 'GMXX' or 'FRXX'.
  364. cities: The number of cities for which to get data
  365. Returns:
  366. weather_reports: A dictionary containing weather data for each city
  367. """
  368. city_codes = yield_all_country_city_codes_yahoo(country_code, cities)
  369. weather_reports = {}
  370. for city_c in city_codes:
  371. weather_data = get_weather_from_yahoo(city_c)
  372. if "error" in weather_data:
  373. return weather_data
  374. city = weather_data["location"]["city"]
  375. weather_reports[city] = weather_data
  376. return weather_reports
  377. def yield_all_country_city_codes_yahoo(country_code, cities):
  378. """
  379. Yield all cities codes for a specific country.
  380. Parameters:
  381. country_code: A four letter code of the necessary country. For example 'GMXX' or 'FRXX'.
  382. cities: The number of cities to yield
  383. Returns:
  384. country_city_codes: A generator containing the city codes
  385. """
  386. # cities stands for the number of available cities
  387. for i in range(1, cities + 1):
  388. yield "".join([country_code, (4 - len(str(i))) * "0", str(i)])
  389. def get_weather_from_noaa(station_id):
  390. """
  391. Fetches weather report from NOAA: National Oceanic and Atmospheric Administration (United States)
  392. Parameter:
  393. station_id: the ID of the weather station near the necessary location
  394. To find your station ID, perform the following steps:
  395. 1. Open this URL: http://www.weather.gov/xml/current_obs/seek.php?state=az&Find=Find
  396. 2. Select the necessary state state. Click 'Find'.
  397. 3. Find the necessary station in the 'Observation Location' column.
  398. 4. The station ID is in the URL for the weather page for that station.
  399. For example if the weather page is http://weather.noaa.gov/weather/current/KPEO.html -- the station ID is KPEO.
  400. Other way to get the station ID: use this library: http://code.google.com/p/python-weather/ and 'Weather.location2station' function.
  401. Returns:
  402. weather_data: a dictionary of weather data that exists in XML feed.
  403. (useful icons: http://www.weather.gov/xml/current_obs/weather.php)
  404. """
  405. station_id = quote(station_id)
  406. url = NOAA_WEATHER_URL % (station_id)
  407. try:
  408. handler = urlopen(url)
  409. except URLError:
  410. return {"error": "Could not connect to NOAA"}
  411. dom = minidom.parse(handler)
  412. handler.close()
  413. data_structure = (
  414. "suggested_pickup",
  415. "suggested_pickup_period",
  416. "location",
  417. "station_id",
  418. "latitude",
  419. "longitude",
  420. "observation_time",
  421. "observation_time_rfc822",
  422. "weather",
  423. "temperature_string",
  424. "temp_f",
  425. "temp_c",
  426. "relative_humidity",
  427. "wind_string",
  428. "wind_dir",
  429. "wind_degrees",
  430. "wind_mph",
  431. "wind_gust_mph",
  432. "pressure_string",
  433. "pressure_mb",
  434. "pressure_in",
  435. "dewpoint_string",
  436. "dewpoint_f",
  437. "dewpoint_c",
  438. "heat_index_string",
  439. "heat_index_f",
  440. "heat_index_c",
  441. "windchill_string",
  442. "windchill_f",
  443. "windchill_c",
  444. "icon_url_base",
  445. "icon_url_name",
  446. "two_day_history_url",
  447. "ob_url",
  448. )
  449. weather_data = {}
  450. current_observation = dom.getElementsByTagName("current_observation")[0]
  451. for tag in data_structure:
  452. try:
  453. weather_data[tag] = current_observation.getElementsByTagName(tag)[
  454. 0
  455. ].firstChild.data
  456. except IndexError:
  457. pass
  458. dom.unlink()
  459. return weather_data
  460. def xml_get_ns_yahoo_tag(dom, ns, tag, attrs):
  461. """
  462. Parses the necessary tag and returns the dictionary with values
  463. Parameters:
  464. dom: DOM
  465. ns: namespace
  466. tag: necessary tag
  467. attrs: tuple of attributes
  468. Returns:
  469. a dictionary of elements
  470. """
  471. element = dom.getElementsByTagNameNS(ns, tag)[0]
  472. return xml_get_attrs(element, attrs)
  473. def xml_get_attrs(xml_element, attrs):
  474. """
  475. Returns the list of necessary attributes
  476. Parameters:
  477. element: xml element
  478. attrs: tuple of attributes
  479. Returns:
  480. a dictionary of elements
  481. """
  482. result = {}
  483. for attr in attrs:
  484. result[attr] = xml_element.getAttribute(attr)
  485. return result
  486. def wind_direction(degrees):
  487. """ Convert wind degrees to direction """
  488. try:
  489. degrees = int(degrees)
  490. except ValueError:
  491. return ""
  492. if degrees < 23 or degrees >= 338:
  493. return "N"
  494. elif degrees < 68:
  495. return "NE"
  496. elif degrees < 113:
  497. return "E"
  498. elif degrees < 158:
  499. return "SE"
  500. elif degrees < 203:
  501. return "S"
  502. elif degrees < 248:
  503. return "SW"
  504. elif degrees < 293:
  505. return "W"
  506. elif degrees < 338:
  507. return "NW"
  508. def wind_beaufort_scale(km_per_hour):
  509. """ Convert km/h to beaufort """
  510. try:
  511. km_per_hour = int(km_per_hour)
  512. except ValueError:
  513. return ""
  514. if km_per_hour < 1:
  515. return "0"
  516. elif km_per_hour <= 5.5:
  517. return "1"
  518. elif km_per_hour <= 11:
  519. return "2"
  520. elif km_per_hour <= 19:
  521. return "3"
  522. elif km_per_hour <= 28:
  523. return "4"
  524. elif km_per_hour <= 38:
  525. return "5"
  526. elif km_per_hour <= 49:
  527. return "6"
  528. elif km_per_hour <= 61:
  529. return "7"
  530. elif km_per_hour <= 74:
  531. return "8"
  532. elif km_per_hour <= 88:
  533. return "9"
  534. elif km_per_hour <= 102:
  535. return "10"
  536. elif km_per_hour <= 117:
  537. return "11"
  538. else:
  539. return "12"
  540. def getText(nodelist):
  541. rc = ""
  542. for node in nodelist:
  543. if node.nodeType == node.TEXT_NODE:
  544. rc = rc + node.data
  545. return rc