Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Show a one-line weather forecast on the desktop' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Show a one-line weather forecast on the desktop
Authored by: anika123 on Jan 03, '10 03:38:03PM

It has been a good run but the weather underground printing Web site seems to have died. When it happened I figured I would just transition my weather scripts over to their official xml api. Long story short, I am now using python and weather undergrounds xml api to get my weather into geektool, nerdtool. It is not as good because they do not have cool stuff like conditions, sunset or lunar stuff in their xml data. Maybe someone else can shed more lite on how they scrape the WU mobile site.

Here is the code.

<code>-------------------------cut here
#! /usr/bin/env python

# cobbled together by Lee Loncosky 2009 from readings on the web

import sys
import urllib
# from optparse import OptionParser
from xml.dom.minidom import parse

# get the url for my local station and put it in variable 'url'

#first we need to select the closest online pws station from the location area api
# parse the url with minidom looking in this order for the closest pws which
# will always be the first 'id' element that is run across, the sequence goes
# <location>
# <nearby _weather_stations>
# <pws>
# <id>
# </id>,</pws>,</nearby_weather_stations>,</location>
wu_urlarea = 'http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query=21719'
urlarea = wu_urlarea
domarea = parse(urllib.urlopen(urlarea))
wu_idpws = domarea.getElementsByTagName('id')[0]
wu_idpws = wu_idpws.toprettyxml()
wu_idpws = str(wu_idpws)
wu_idpws = wu_idpws.strip("<id>\n")
wu_idpws = wu_idpws.strip("</id>\n")
wu_idpws = wu_idpws.replace("![CDATA[","")
wu_idpws = wu_idpws.replace("]]","")
wu_idpws = wu_idpws.strip()
wu_url = 'http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID='
wu_url = wu_url+wu_idpws
wu_url.strip()
# finally pass the current closest pws station to wu_url and then to url to get the current observation api
url = wu_url
# parse the url (xml) with minidom
dom = parse(urllib.urlopen(url))

# get location first
# grab the location which is a child element of current_observation and put into variable
current_node = dom.firstChild
location_node = current_node.childNodes[7]
wu_location = location_node.childNodes[1]

# grab the xml elements that we want to use and put into variables
wu_time = dom.getElementsByTagName('observation_time')[0]
wu_temp = dom.getElementsByTagName('temperature_string')[0]
wu_wind = dom.getElementsByTagName('wind_string')[0]
wu_pressure = dom.getElementsByTagName('pressure_in')[0]

# start with first variable and turn into type string and then strip the xml <tags/>

wu_location = wu_location.toprettyxml()
wu_location = str(wu_location)
wu_location = wu_location.strip("<full>\n")
wu_location = wu_location.strip("</full>\n")
# replace the crazy charaters with white space. Only needed on location and pressure_string
wu_location = wu_location.replace("&quot;","")
wu_location = wu_location.strip()

wu_time = wu_time.toprettyxml()
wu_time = str(wu_time)
wu_time = wu_time.strip("<observation_time>\n")
wu_time = wu_time.strip("</observation_time>\n")
wu_time = wu_time.strip()

wu_temp = wu_temp.toprettyxml()
wu_temp = str(wu_temp)
wu_temp = wu_temp.strip("<temperature_string>\n")
wu_temp = wu_temp.strip("</temperature_string>\n")

wu_wind = wu_wind.toprettyxml()
wu_wind = str(wu_wind)
wu_wind = wu_wind.strip("<wind_string>\n")
wu_wind = wu_wind.strip("/<wind_string>\n")
wu_wind = wu_wind.strip()

wu_pressure = wu_pressure.toprettyxml()
wu_pressure = str(wu_pressure)
wu_pressure = wu_pressure.strip("<pressure_in>\n")
wu_pressure = wu_pressure.strip("/<pressure_in>\n")

# print everything to screen with labels

print wu_location
print wu_time
print 'Temp',wu_temp
print wu_wind
print 'Pressure',wu_pressure
<code>--------------------------------------------cut here

You will have to change the following line to reflect your zipcode.

wu_urlarea = 'http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query=21719'

Hope it works. If not please modify to suit your needs.

All the standard stuff applies... make it executable, make sure python is in your path, make sure to give whole path in geektool, nerdtool ... etc...
















[ Reply to This | # ]