123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #!/usr/bin/python
- import urllib
- from xml.etree.cElementTree import parse
- from datetime import datetime, timedelta
- import os
- from os.path import join
- from sys import argv
- try:
- import cPickle as pickle
- except ImportError:
- import pickle
- #Usage: yweather.py AYXX0001 Celsius
- if len(argv) != 3:
- raise Exception('Usage: yweather.py zip_code units. zip_code is your city code in Yahoo Weather, units can be Celsius or Fahrenheit.')
- else:
- zip_code = argv[1]
- if argv[2] == 'Fahrenheit' or argv[2] == 'fahrenheit':
- units = 'f'
- else:
- units = 'c'
- CACHE_HOURS = 6
- #http://weather.yahooapis.com/forecastrss
- WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss?p=%s&u=%s'
- WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'
- def weather_for_zip(zip_code, units):
- url = WEATHER_URL % (zip_code, units)
- rss = parse(urllib.urlopen(url)).getroot()
- forecasts = []
- for element in rss.findall('channel/item/{%s}forecast' % WEATHER_NS):
- forecasts.append(dict(element.items()))
- ycondition = rss.find('channel/item/{%s}condition' % WEATHER_NS)
- return {
- 'current_condition': dict(ycondition.items()),
- 'forecasts': forecasts,
- 'title': rss.findtext('channel/title'),
- 'pubDate': rss.findtext('channel/item/pubDate'), #rss.findtext('channel/lastBuildDate'),
- 'location': dict(rss.find('channel/{%s}location' % WEATHER_NS).items()),
- 'wind': dict(rss.find('channel/{%s}wind' % WEATHER_NS).items()),
- 'atmosphere': dict(rss.find('channel/{%s}atmosphere' % WEATHER_NS).items()),
- 'astronomy': dict(rss.find('channel/{%s}astronomy' % WEATHER_NS).items()),
- 'units': dict(rss.find('channel/{%s}units' % WEATHER_NS).items())
- }
- def print_openbox_pipe_menu(weather):
- print '<openbox_pipe_menu>'
- print '<separator label="%s %s" />' % (weather['location']['city'],weather['pubDate'])
- print '<separator label="Current conditions" />'
- print '<item label="Weather: %s" />' % weather['current_condition']['text']
- print '<item label="Temperature: %s %s" />' % ( weather['current_condition']['temp'],
- weather['units']['temperature'] )
- print '<item label="Humidity: %s%%" />' % weather['atmosphere']['humidity']
- print '<item label="Visibility: %s %s" />' % ( weather['atmosphere']['visibility'],
- weather['units']['distance'] )
-
- #pressure: steady (0), rising (1), or falling (2)
- if weather['atmosphere']['rising'] == 0:
- pressure_state = 'steady'
- elif weather['atmosphere']['rising'] == 1:
- pressure_state = 'rising'
- else:
- pressure_state = 'falling'
- print '<item label="Pressure: %s %s (%s)" />' % ( weather['atmosphere']['pressure'],
- weather['units']['pressure'], pressure_state )
- print '<item label="Wind chill: %s %s" />' % ( weather['wind']['chill'],
- weather['units']['temperature'] )
- print '<item label="Wind direction: %s degrees" />' % weather['wind']['direction']
- print '<item label="Wind speed: %s %s" />' % ( weather['wind']['speed'],
- weather['units']['speed'] )
- print '<item label="Sunrise: %s" />' % weather['astronomy']['sunrise']
- print '<item label="Sunset: %s" />' % weather['astronomy']['sunset']
- for forecast in weather['forecasts']:
- print '<separator label="Forecast: %s" />' % forecast['day']
- print '<item label="Weather: %s" />' % forecast['text']
- print '<item label="Min temperature: %s %s" />' % ( forecast['low'],
- weather['units']['temperature'] )
- print '<item label="Max temperature: %s %s" />' % ( forecast['high'],
- weather['units']['temperature'] )
- print '</openbox_pipe_menu>'
- cache_file = join(os.getenv("HOME"), '.yweather.cache')
- try:
- f = open(cache_file,'rb')
- cache = pickle.load(f)
- f.close()
- except IOError:
- cache = None
- if cache == None or (zip_code, units) not in cache or (
- cache[(zip_code, units)]['date'] + timedelta(hours=CACHE_HOURS) < datetime.utcnow()):
- # The cache is outdated
- weather = weather_for_zip(zip_code, units)
- if cache == None:
- cache = dict()
- cache[(zip_code, units)] = {'date': datetime.utcnow(), 'weather': weather}
-
- #Save the data in the cache
- try:
- f = open(cache_file, 'wb')
- cache = pickle.dump(cache, f, -1)
- f.close()
- except IOError:
- raise
- else:
- weather = cache[(zip_code, units)]['weather']
- print_openbox_pipe_menu(weather)
|