#!/usr/bin/python # -*- coding: utf-8 import xml.parsers.expat import urllib from os import execvp ignore_elements = [ 'guid', 'geo:lat', 'geo:long', 'link', 'title', 'ttl', 'rss', 'channel', 'lastBuildDate', 'pubDate', 'width', 'height', 'item'] in_element = "" wdata = {} ferr = 0 icon = '/usr/share/pixmaps/gnome-question.png' save_image_filename = "/tmp/weather-notify-temp.gif" def start_element(name, attrs): global in_element, wdata in_element += "." + name if name not in ignore_elements: if (in_element == ".rss.channel.yweather:location"): wdata['city'] = attrs['city'] elif (in_element == ".rss.channel.yweather:units"): wdata['units'] = attrs elif (in_element == ".rss.channel.yweather:wind"): wdata['wind'] = attrs elif (in_element == ".rss.channel.yweather:atmosphere"): wdata['atmosphere'] = attrs elif (in_element == ".rss.channel.yweather:astronomy"): wdata['astronomy'] = attrs elif (in_element == ".rss.channel.item.yweather:condition"): wdata['condition'] = attrs else: #print 'Start element:', in_element, attrs pass def end_element(name): global in_element in_element = in_element[:-(name.__len__() + 1)] if in_element not in ignore_elements: #print 'End element:', in_element + "." + name pass def char_data(data): global wdata, ferr if in_element not in ignore_elements: if (in_element == ".rss.channel.item.description"): for line in data.split("\n"): if (line != ""): for attr in line.split("\""): if attr.count("yimg.com") > 0: wdata['image_url'] = attr elif (in_element == ".rss.channel.title" and data == "Yahoo! Weather - Error"): ferr = 1 elif (data != "\n" and data != " "): #print 'Character data:', repr(data) pass p = xml.parsers.expat.ParserCreate() p.StartElementHandler = start_element p.EndElementHandler = end_element p.CharacterDataHandler = char_data xwyc = """ Yahoo! Weather - Stockholm, SW http://us.rd.yahoo.com/dailynews/rss/weather/Stockholm__SW/*http://xml.weather.yahoo.com/forecast/SWXX0031_c.html Yahoo! Weather for Stockholm, SW en-us Sat, 25 Mar 2006 2:20 pm CET 60 Yahoo! Weather 142 18 http://weather.yahoo.com/ http://us.i1.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif Conditions for Stockholm, SW at 2:20 pm CET 59.35 17.95 http://us.rd.yahoo.com/dailynews/rss/weather/Stockholm__SW/*http://xml.weather.yahoo.com/forecast/SWXX0031_c.html Sat, 25 Mar 2006 2:20 pm CET
Current Conditions:
Fair, 2 C

Forecast:
Sat - Mostly Clear. High: 1 Low: -7
Sun - Partly Cloudy. High: 3 Low: -3

Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
]]>
SWXX0031_2006_03_25_14_20_CET
""" urlfd1 = urllib.urlopen("http://xml.weather.yahoo.com/forecastrss?p=SWXX0031&u=c") p.Parse(urlfd1.read(), 1) #p.Parse(xwyc, 1) if (ferr == 1): title = "XML fetch error" message = "Unable to retrieve weather information" else: title = wdata['city'] + ": " + wdata['condition']['temp'] + u'°' + wdata['units']['temperature'] + " (" + wdata['condition']['text'] + ")" message = "\tWindspeed:\t\t\t" + wdata['wind']['speed'] + " " + wdata['units']['speed'] + "\n" message += "\tWindchill:\t\t\t" + wdata['wind']['chill'] + u'°' + wdata['units']['temperature'] + "\n" message += "\tSunrise/Sunset:\t\t" + wdata['astronomy']['sunrise'] + "/" + wdata['astronomy']['sunset'] + "\n" message += "\tHumidity:\t\t\t" + wdata['atmosphere']['humidity'] + "%\n" message += "\tPressure:\t\t\t" + wdata['atmosphere']['pressure'] + " " + wdata['units']['pressure'] + "\n" message += "\tVisibility:\t\t\t" + wdata['atmosphere']['visibility'] + " " + wdata['units']['distance'] urlfn, urlhdrs = urllib.urlretrieve(wdata['image_url'], save_image_filename) if not urlhdrs.type == "text/html": icon = save_image_filename args = [ 'notify-send', '--icon='+icon, title, message] execvp("notify-send", args)