~joshuascotton/entertainer/pluginsV2

« back to all changes in this revision

Viewing changes to entertainerlib/utils/weather.py

  • Committer: Paul Hummer
  • Date: 2008-08-06 05:23:49 UTC
  • mfrom: (258.1.19 new-i18n-branch)
  • Revision ID: paul@ubuntu.com-20080806052349-f4spdhg5ixbis64a
Translations are now supported in Entertainer!

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
  4 days.
5
5
"""
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")
9
9
 
 
10
import locale
10
11
import urllib
 
12
import urllib2
11
13
from xml.dom import minidom
12
14
 
13
15
from entertainerlib.utils.theme import Theme
14
16
 
15
 
#The lookup url for the rss feed
 
17
DEFAULT_LOCALE = "en_US"
 
18
 
 
19
#The lookup url for the google weather API
16
20
WEATHER_LOOKUP_URL = "http://www.google.co.uk/ig/api?weather="
17
21
 
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',
23
 
                  'Fog': 'weather-fog',
24
 
                  'Clear': 'weather-sun',
25
 
                  'Icy': 'weather-ice',
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 
 
27
                  # condition.
 
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'
28
36
                  }
29
37
 
30
38
class Weather:
44
52
        self.__metric = ''
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 ()
48
56
        self.refresh()
49
57
 
 
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
 
62
        #
 
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
 
66
        supported_locales = {
 
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")
 
95
        }
 
96
 
 
97
        lc_id = DEFAULT_LOCALE
 
98
        default = locale.getdefaultlocale ()[0]
 
99
        if default:
 
100
            if supported_locales.has_key (default):
 
101
                lc_id = default
 
102
            else:
 
103
                lang = default.split("_")[0]
 
104
                if supported_locales.has_key (lang):
 
105
                    lc_id = lang
 
106
 
 
107
        return supported_locales[lc_id]
 
108
 
 
109
 
50
110
    def find_forecast(self, search):
51
111
        """
52
112
        Returns the search results page for the search
55
115
        """
56
116
        url = WEATHER_LOOKUP_URL + search
57
117
        url = url.replace(" ", "%20")
58
 
        data = urllib.urlopen(url).read()
59
 
        try:
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" ))
 
118
 
 
119
        txheaders = {
 
120
            'Accept-Language': self.locale
 
121
        }
 
122
 
 
123
        req = urllib2.Request(url, None, txheaders)
 
124
        rep = urllib2.urlopen(req)
 
125
        data = rep.read()
 
126
 
 
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"?>')
 
131
       
 
132
        dom = minidom.parseString(data)
64
133
 
65
134
        in_forecast = 0
66
135
        day = ''
67
136
        low = ''
68
137
        high = ''
69
138
        condition = ''
 
139
        imagename = ''
70
140
        image = ''
71
141
 
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')
76
146
                break
77
147
 
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)
93
165
 
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.
105
177
                    low = ''
106
178
                    high = ''
107
179
                    condition = ''
 
180
                    imagename = ''
108
181
                    image = ''
109
182
 
110
183
        return self.__forecasts