6
6
__licence__ = "GPLv2"
7
__copyright__ = "2008, Jamie Bennett"
8
__author__ = "Jamie Bennett <jamie@linuxuk.org>"
7
__copyright__ = ("2008, Jamie Bennett", "2008, Samuel Buffet")
8
__author__ = ("Jamie Bennett <jamie@linuxuk.org>", "Samuel Buffet")
11
13
from xml.dom import minidom
13
15
from entertainerlib.utils.theme import Theme
15
#The lookup url for the rss feed
17
DEFAULT_LOCALE = "en_US"
19
#The lookup url for the google weather API
16
20
WEATHER_LOOKUP_URL = "http://www.google.co.uk/ig/api?weather="
18
WEATHER_IMAGES = {'Mostly Sunny': 'weather-sun-mostly',
19
'Chance of Rain': 'weather-rain-chance',
20
'Partly Sunny': 'weather-sun-clouds',
21
'Rain': 'weather-rain',
22
'Chance of Storm': 'weather-lightning',
24
'Clear': 'weather-sun',
26
'Cloudy': 'weather-clouds',
27
'Snow': 'weather-snow'
22
#use the icon instead of condition to make it work with any languages
23
WEATHER_IMAGES = {'/images/weather/mostly_sunny.gif': 'weather-sun-mostly',
24
'/images/weather/chance_of_rain.gif': 'weather-rain-chance',
25
#'/images/weather/partly_sunny.gif': 'weather-sun-clouds',
26
# no dedicated gif provided by google for that weather
28
# it's a *mostly_sunny* gif wich is given instead :(
29
'/images/weather/rain.gif': 'weather-rain',
30
'/images/weather/storm.gif': 'weather-lightning',
31
'/images/weather/fog.gif': 'weather-fog',
32
'/images/weather/sunny.gif': 'weather-sun',
33
'/images/weather/icy.gif': 'weather-ice',
34
'/images/weather/cloudy.gif': 'weather-clouds',
35
'/images/weather/snow.gif': 'weather-snow'
45
53
self.theme = Theme(test_dir)
46
54
self.set_location(location)
47
self.set_metric(metric)
55
(self.languagename, self.locale) = self.__get_locale ()
58
def __get_locale (self):
59
'''Get locale information from user\'s machine'''
60
# adapted from *albumart_downloader.py*
61
# list of supported languages by google weather API to be completed
63
# still remains a display issue with arabic, greek, hebrew, japanese...
64
# wich is probably a Clutter issue. So let's wait Clutter 0.8 to see
65
# if it makes a difference
67
"en_US" : ("ENGLISH","en-us"),
68
"ar" : ("ARABIC","ar"),
69
"bg" : ("BULGARIAN","bg"),
70
"zh" : ("CHINESE","zh"),
71
"cs" : ("CZECH","cs"),
72
"da" : ("DANISH","da"),
73
"nl" : ("DUTCH","nl"),
74
"et" : ("ESTONIAN","et"),
75
"fi" : ("FINNISH","fi"),
76
"fr" : ("FRENCH","fr"),
77
"de" : ("GERMAN","de"),
78
"el" : ("GREEK","el"),
79
"he" : ("HEBREW","he"),
80
"hi" : ("HINDI","hi"),
81
"hu" : ("HUNGARIAN","hu"),
82
"it" : ("ITALIAN","it"),
83
"ja" : ("JAPANESE","ja"),
84
"ko" : ("KOREAN","ko"),
85
"pl" : ("POLISH","pl"),
86
"pt" : ("PORTUGUESE","pt"),
87
"ro" : ("ROMANIAN","ro"),
88
"ru" : ("RUSSIAN","ru"),
89
"hr" : ("CROATIAN","hr"),
90
"sk" : ("SLOVAK","sk"),
91
"sl" : ("SLOVENIAN","sl"),
92
"es" : ("SPANISH","es"),
93
"sv" : ("SWEDISH","sv"),
94
"tr" : ("TURKISH","tr")
97
lc_id = DEFAULT_LOCALE
98
default = locale.getdefaultlocale ()[0]
100
if supported_locales.has_key (default):
103
lang = default.split("_")[0]
104
if supported_locales.has_key (lang):
107
return supported_locales[lc_id]
50
110
def find_forecast(self, search):
52
112
Returns the search results page for the search
56
116
url = WEATHER_LOOKUP_URL + search
57
117
url = url.replace(" ", "%20")
58
data = urllib.urlopen(url).read()
60
dom = minidom.parseString(data.encode( "utf-8" ))
61
except UnicodeDecodeError:
62
data = data.decode("Latin-1")
63
dom = minidom.parseString(data.encode( "utf-8" ))
120
'Accept-Language': self.locale
123
req = urllib2.Request(url, None, txheaders)
124
rep = urllib2.urlopen(req)
127
# if this encoding is not specified for the xml file, parsing will
128
# fail for some special characters
129
data = data.replace('<?xml version="1.0"?>',
130
'<?xml version="1.0" encoding="iso-8859-1"?>')
132
dom = minidom.parseString(data)
72
142
for node in dom.getElementsByTagNameNS('*', '*'):
73
143
# a problem occured so bail out
74
144
if (node.nodeName == 'problem_cause'):
75
print 'Location not found or network problem'
145
print _('Location not found or network problem')
78
148
if (node.nodeName == 'forecast_conditions'):
89
159
converted_high = self.weather_units (int(high))
90
160
if node.nodeName == 'condition':
91
161
condition = node.getAttribute('data')
92
image = self.set_image (condition)
162
if node.nodeName == 'icon':
163
imagename = node.getAttribute('data')
164
image = self.set_image (imagename)
94
166
# condition is the last element of the forecast we are
95
167
# interested in so write out the forecast if this is set.