Merge lp:~jtasker/weather-indicator/cloudy into lp:weather-indicator

Proposed by Joshua Tasker
Status: Merged
Approved by: Vadim Rutkovsky
Approved revision: 312
Merged at revision: 312
Proposed branch: lp:~jtasker/weather-indicator/cloudy
Merge into: lp:weather-indicator
Diff against target: 174 lines (+36/-53)
1 file modified
bin/indicator-weather (+36/-53)
To merge this branch: bzr merge lp:~jtasker/weather-indicator/cloudy
Reviewer Review Type Date Requested Status
Joshua Tasker Approve
Vadim Rutkovsky Approve
Review via email: mp+152550@code.launchpad.net

Description of the change

Rewrote the Forecast code to pull from Yahoo Weather instead of the (now-discontinued) Google Weather API.
Requires an updated version of pywapi (the current Ubuntu package is out of date) which is available from here:
http://code.google.com/p/python-weather-api/source/browse/#svn%2Ftrunk

To post a comment you must log in.
Revision history for this message
Vadim Rutkovsky (roignac) wrote :

Looks good to me, however I can't verify the fix

review: Approve
Revision history for this message
Joshua Tasker (jtasker) wrote :

pywapi 0.3 should be "officially" posted later today, it uses revision r117 at the above google code link

review: Approve
Revision history for this message
Joshua Tasker (jtasker) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/indicator-weather'
2--- bin/indicator-weather 2012-07-30 04:02:24 +0000
3+++ bin/indicator-weather 2013-03-09 03:28:21 +0000
4@@ -4,6 +4,7 @@
5 # Copyright (C) 2010 Sebastian MacDonald Sebas310@gmail.com
6 # Copyright (C) 2010 Mehdi Rejraji mehd36@gmail.com
7 # Copyright (C) 2011 Vadim Rutkovsky roignac@gmail.com
8+# Copyright (C) 2013 Joshua Tasker jtasker@gmail.com
9 # This program is free software: you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License version 3, as published
11 # by the Free Software Foundation.
12@@ -55,7 +56,7 @@
13 sys.path.insert(0, PROJECT_ROOT_DIRECTORY)
14 os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses
15
16-VERSION = "12.07.30 'Cloudy 10'"
17+VERSION = "13.03.08 'Cloudy 11'"
18
19 from indicator_weather.helpers import *
20
21@@ -364,7 +365,7 @@
22 s=f.read()
23 null = None
24 yahoo_woeid_result = eval(s)
25- if (yahoo_woeid_result['ResultSet']['Error'] != 0) and (yahoo_woeid_result['ResultSet']['Results'] != None):
26+ if (yahoo_woeid_result['ResultSet']['Error'] != '0') and (yahoo_woeid_result['ResultSet']['Results'] != None):
27 log.error("Location: Yahoo woeid return error. Full response:\n %s" % str(yahoo_woeid_result))
28 return
29 else:
30@@ -400,7 +401,7 @@
31 except Exception, e:
32 log.error(e)
33
34- # Return lcoation code and location details
35+ # Return location code and location details
36 def export_location_details(self):
37 return (self.location_code, self.location_details)
38
39@@ -448,38 +449,36 @@
40 class Forecast:
41 """ Class to get forecast information """
42
43- # Initialize a class with metric system, wind units, location and current user locale
44- def __init__ (self, units, lat, lon, locale):
45+ # Initialize a class with metric system, wind units, location code and current user locale
46+ def __init__ (self, units, location_id, locale):
47 self.metric_system = units
48- self.lat = self.convert_coordinate_for_google(lat)
49- self.lon = self.convert_coordinate_for_google(lon)
50+ self.location_id = location_id
51 self.locale = locale
52
53- # Convert coordinate for google
54- def convert_coordinate_for_google(self, value):
55- value = float(value) * 1e6
56- return int(round(value))
57-
58- # Get and store forecast data. For now using Google only
59+ # Get and store forecast data. Now using Yahoo only
60 def prepare_forecast_data(self):
61 self.daysofweek = []
62 self.icons = []
63 self.conditions = []
64 self.error_message = None
65 try:
66- # Generate a fake location by current coordinates
67- location_name = ",,,%s,%s" % (self.lat, self.lon)
68- self.forecast = pywapi.get_weather_from_google (location_name, hl = self.locale)
69- self.unitsystem = self.forecast['forecast_information']['unit_system']
70+ # Check units, default to imperial
71+ if self.metric_system == MetricSystem.SI:
72+ self.unitsystem = 'c'
73+ elif self.metric_system == MetricSystem.IMPERIAL:
74+ self.unitsystem = 'f'
75+ else:
76+ self.unitsystem = 'f'
77+
78+ self.forecast = pywapi.get_weather_from_yahoo (self.location_id, self.unitsystem)
79
80 for forecast in self.forecast['forecasts']:
81- self.daysofweek.append(forecast["day_of_week"])
82- forecast_icon = str(forecast["icon"])
83- if "/ig/images/weather/" in forecast_icon:
84- self.icons.append(forecast_icon.split("/ig/images/weather/")[-1].split(".gif")[0])
85- elif "http://g0.gstatic.com/images/icons/onebox" in forecast_icon:
86- self.icons.append(forecast_icon.split("http://g0.gstatic.com/images/icons/onebox/weather_")[-1].split("-40.gif")[0])
87- self.conditions.append(forecast["condition"])
88+ self.daysofweek.append(forecast["day"])
89+
90+ # Yahoo forecast icon URL is "http://l.yimg.com/a/i/us/we/52/<code>.gif"
91+ self.icons.append(forecast["code"])
92+ self.conditions.append(forecast["text"])
93+
94 self.error_message = None
95
96 except urllib2.URLError:
97@@ -493,27 +492,11 @@
98 self.highdata = []
99 self.lowdata = []
100
101- if not hasattr(self, 'unitsystem'):
102- return None
103-
104- if ((self.unitsystem == 'SI') and (self.metric_system == MetricSystem.SI)) or ((self.unitsystem == 'US') and (self.metric_system == MetricSystem.IMPERIAL)):
105- #correct scale selected
106- for forecast in self.forecast['forecasts']:
107+ # Since we are now using Yahoo, forecast will always be in correct units
108+ for forecast in self.forecast['forecasts']:
109 self.highdata.append(forecast["high"])
110 self.lowdata.append(forecast["low"])
111-
112- elif ((self.unitsystem == 'SI') and (self.metric_system == MetricSystem.IMPERIAL)):
113- #convert from SI to imperial
114- for forecast in self.forecast['forecasts']:
115- self.highdata.append(int(((int(forecast["high"])*9)/5)+32))
116- self.lowdata.append(int(((int(forecast["low"])*9)/5)+32))
117-
118- elif ((self.unitsystem == 'US') and (self.metric_system == MetricSystem.SI)):
119- #convert from imperial to SI
120- for forecast in self.forecast['forecasts']:
121- self.highdata.append(int((((int(forecast["high"]))-32)*5)/9))
122- self.lowdata.append(int((((int(forecast["low"]))-32)*5)/9))
123-
124+
125 return (self.highdata, self.lowdata)
126
127 # Parse a list of days of week with forecast data
128@@ -530,7 +513,7 @@
129
130 class Weather:
131 """
132- Data object to parse weather data with unit convertion
133+ Data object to parse weather data with unit conversion
134 """
135
136 #Available conditions by google icon
137@@ -1714,9 +1697,9 @@
138
139 # Get forecast data using Forecast object
140 log.debug("ExtendedForecast: chosen place: %s (code %s)" % (wi.places[wi.placechosen][1], wi.places[wi.placechosen][0]))
141- self.builder.get_object('extended_forecast').set_title("%s %s" % (_('Weather Forecast for '), wi.places[wi.placechosen][1]))
142+ self.builder.get_object('extended_forecast').set_title("%s %s" % (_('Weather Forecast for'), wi.places[wi.placechosen][1]))
143 log.debug("ExtendedForecast: getting forecast data")
144- forecast = Forecast(wi.metric_system, wi.current_location.location_details['latitude'], wi.current_location.location_details['longitude'], locale_name)
145+ forecast = Forecast(wi.metric_system, wi.current_location.location_details['yahoo id'], locale_name)
146 forecast.prepare_forecast_data()
147 if forecast.error_message != None:
148 #Error occurred while getting forecast data
149@@ -1745,18 +1728,18 @@
150
151 # Fill in icons
152 for i in xrange(1,5):
153- # Get icon name from dictionary in Weather object for Google icons
154+ # Get icon name from dictionary in Weather object for Yahoo condition codes
155 try:
156- conds = Weather._GoogleConditions.get(icons[i-1])
157+ conds = Weather._YahooConditions.get(icons[i-1])
158 if conds != None:
159- google_icon = conds[0]
160+ yahoo_icon = conds[0]
161 else:
162- log.error("ExtendedForecast: unknown Google weather condition '%s'" % icons[i-1])
163+ log.error("ExtendedForecast: unknown Yahoo weather condition code '%s'" % icons[i-1])
164 log.error(forecast.forecast)
165- google_icon = 'weather-indicator-unknown'
166- self.builder.get_object('day%simage' % str(i)).set_from_icon_name(google_icon,gtk.ICON_SIZE_BUTTON)
167+ yahoo_icon = 'weather-indicator-unknown'
168+ self.builder.get_object('day%simage' % str(i)).set_from_icon_name(yahoo_icon,gtk.ICON_SIZE_BUTTON)
169 except IndexError:
170- log.error("ExtendedForecast: Google didn't return condition for %s days" % i-1)
171+ log.error("ExtendedForecast: Yahoo didn't return condition for %s days" % i-1)
172 log.error(forecast.forecast)
173
174 # Fill in condition labels

Subscribers

People subscribed via source and target branches

to all changes: