-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyweather.py
executable file
·116 lines (100 loc) · 4.63 KB
/
yweather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/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)