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
=== modified file 'bin/indicator-weather'
--- bin/indicator-weather 2012-07-30 04:02:24 +0000
+++ bin/indicator-weather 2013-03-09 03:28:21 +0000
@@ -4,6 +4,7 @@
4# Copyright (C) 2010 Sebastian MacDonald Sebas310@gmail.com4# Copyright (C) 2010 Sebastian MacDonald Sebas310@gmail.com
5# Copyright (C) 2010 Mehdi Rejraji mehd36@gmail.com5# Copyright (C) 2010 Mehdi Rejraji mehd36@gmail.com
6# Copyright (C) 2011 Vadim Rutkovsky roignac@gmail.com6# Copyright (C) 2011 Vadim Rutkovsky roignac@gmail.com
7# Copyright (C) 2013 Joshua Tasker jtasker@gmail.com
7# This program is free software: you can redistribute it and/or modify it8# This program is free software: you can redistribute it and/or modify it
8# under the terms of the GNU General Public License version 3, as published9# under the terms of the GNU General Public License version 3, as published
9# by the Free Software Foundation.10# by the Free Software Foundation.
@@ -55,7 +56,7 @@
55 sys.path.insert(0, PROJECT_ROOT_DIRECTORY)56 sys.path.insert(0, PROJECT_ROOT_DIRECTORY)
56 os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses57 os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses
5758
58VERSION = "12.07.30 'Cloudy 10'"59VERSION = "13.03.08 'Cloudy 11'"
5960
60from indicator_weather.helpers import *61from indicator_weather.helpers import *
6162
@@ -364,7 +365,7 @@
364 s=f.read()365 s=f.read()
365 null = None366 null = None
366 yahoo_woeid_result = eval(s)367 yahoo_woeid_result = eval(s)
367 if (yahoo_woeid_result['ResultSet']['Error'] != 0) and (yahoo_woeid_result['ResultSet']['Results'] != None):368 if (yahoo_woeid_result['ResultSet']['Error'] != '0') and (yahoo_woeid_result['ResultSet']['Results'] != None):
368 log.error("Location: Yahoo woeid return error. Full response:\n %s" % str(yahoo_woeid_result))369 log.error("Location: Yahoo woeid return error. Full response:\n %s" % str(yahoo_woeid_result))
369 return370 return
370 else:371 else:
@@ -400,7 +401,7 @@
400 except Exception, e:401 except Exception, e:
401 log.error(e)402 log.error(e)
402403
403 # Return lcoation code and location details404 # Return location code and location details
404 def export_location_details(self):405 def export_location_details(self):
405 return (self.location_code, self.location_details)406 return (self.location_code, self.location_details)
406407
@@ -448,38 +449,36 @@
448class Forecast:449class Forecast:
449 """ Class to get forecast information """450 """ Class to get forecast information """
450451
451 # Initialize a class with metric system, wind units, location and current user locale452 # Initialize a class with metric system, wind units, location code and current user locale
452 def __init__ (self, units, lat, lon, locale):453 def __init__ (self, units, location_id, locale):
453 self.metric_system = units454 self.metric_system = units
454 self.lat = self.convert_coordinate_for_google(lat)455 self.location_id = location_id
455 self.lon = self.convert_coordinate_for_google(lon)
456 self.locale = locale456 self.locale = locale
457457
458 # Convert coordinate for google458 # Get and store forecast data. Now using Yahoo only
459 def convert_coordinate_for_google(self, value):
460 value = float(value) * 1e6
461 return int(round(value))
462
463 # Get and store forecast data. For now using Google only
464 def prepare_forecast_data(self):459 def prepare_forecast_data(self):
465 self.daysofweek = []460 self.daysofweek = []
466 self.icons = []461 self.icons = []
467 self.conditions = []462 self.conditions = []
468 self.error_message = None463 self.error_message = None
469 try:464 try:
470 # Generate a fake location by current coordinates465 # Check units, default to imperial
471 location_name = ",,,%s,%s" % (self.lat, self.lon)466 if self.metric_system == MetricSystem.SI:
472 self.forecast = pywapi.get_weather_from_google (location_name, hl = self.locale)467 self.unitsystem = 'c'
473 self.unitsystem = self.forecast['forecast_information']['unit_system']468 elif self.metric_system == MetricSystem.IMPERIAL:
469 self.unitsystem = 'f'
470 else:
471 self.unitsystem = 'f'
472
473 self.forecast = pywapi.get_weather_from_yahoo (self.location_id, self.unitsystem)
474474
475 for forecast in self.forecast['forecasts']:475 for forecast in self.forecast['forecasts']:
476 self.daysofweek.append(forecast["day_of_week"])476 self.daysofweek.append(forecast["day"])
477 forecast_icon = str(forecast["icon"])477
478 if "/ig/images/weather/" in forecast_icon:478 # Yahoo forecast icon URL is "http://l.yimg.com/a/i/us/we/52/<code>.gif"
479 self.icons.append(forecast_icon.split("/ig/images/weather/")[-1].split(".gif")[0])479 self.icons.append(forecast["code"])
480 elif "http://g0.gstatic.com/images/icons/onebox" in forecast_icon:480 self.conditions.append(forecast["text"])
481 self.icons.append(forecast_icon.split("http://g0.gstatic.com/images/icons/onebox/weather_")[-1].split("-40.gif")[0])481
482 self.conditions.append(forecast["condition"])
483 self.error_message = None482 self.error_message = None
484483
485 except urllib2.URLError:484 except urllib2.URLError:
@@ -493,27 +492,11 @@
493 self.highdata = []492 self.highdata = []
494 self.lowdata = []493 self.lowdata = []
495494
496 if not hasattr(self, 'unitsystem'):495 # Since we are now using Yahoo, forecast will always be in correct units
497 return None496 for forecast in self.forecast['forecasts']:
498
499 if ((self.unitsystem == 'SI') and (self.metric_system == MetricSystem.SI)) or ((self.unitsystem == 'US') and (self.metric_system == MetricSystem.IMPERIAL)):
500 #correct scale selected
501 for forecast in self.forecast['forecasts']:
502 self.highdata.append(forecast["high"])497 self.highdata.append(forecast["high"])
503 self.lowdata.append(forecast["low"])498 self.lowdata.append(forecast["low"])
504499
505 elif ((self.unitsystem == 'SI') and (self.metric_system == MetricSystem.IMPERIAL)):
506 #convert from SI to imperial
507 for forecast in self.forecast['forecasts']:
508 self.highdata.append(int(((int(forecast["high"])*9)/5)+32))
509 self.lowdata.append(int(((int(forecast["low"])*9)/5)+32))
510
511 elif ((self.unitsystem == 'US') and (self.metric_system == MetricSystem.SI)):
512 #convert from imperial to SI
513 for forecast in self.forecast['forecasts']:
514 self.highdata.append(int((((int(forecast["high"]))-32)*5)/9))
515 self.lowdata.append(int((((int(forecast["low"]))-32)*5)/9))
516
517 return (self.highdata, self.lowdata)500 return (self.highdata, self.lowdata)
518501
519 # Parse a list of days of week with forecast data502 # Parse a list of days of week with forecast data
@@ -530,7 +513,7 @@
530513
531class Weather:514class Weather:
532 """515 """
533 Data object to parse weather data with unit convertion516 Data object to parse weather data with unit conversion
534 """517 """
535518
536 #Available conditions by google icon519 #Available conditions by google icon
@@ -1714,9 +1697,9 @@
17141697
1715 # Get forecast data using Forecast object1698 # Get forecast data using Forecast object
1716 log.debug("ExtendedForecast: chosen place: %s (code %s)" % (wi.places[wi.placechosen][1], wi.places[wi.placechosen][0]))1699 log.debug("ExtendedForecast: chosen place: %s (code %s)" % (wi.places[wi.placechosen][1], wi.places[wi.placechosen][0]))
1717 self.builder.get_object('extended_forecast').set_title("%s %s" % (_('Weather Forecast for '), wi.places[wi.placechosen][1]))1700 self.builder.get_object('extended_forecast').set_title("%s %s" % (_('Weather Forecast for'), wi.places[wi.placechosen][1]))
1718 log.debug("ExtendedForecast: getting forecast data")1701 log.debug("ExtendedForecast: getting forecast data")
1719 forecast = Forecast(wi.metric_system, wi.current_location.location_details['latitude'], wi.current_location.location_details['longitude'], locale_name)1702 forecast = Forecast(wi.metric_system, wi.current_location.location_details['yahoo id'], locale_name)
1720 forecast.prepare_forecast_data()1703 forecast.prepare_forecast_data()
1721 if forecast.error_message != None:1704 if forecast.error_message != None:
1722 #Error occurred while getting forecast data1705 #Error occurred while getting forecast data
@@ -1745,18 +1728,18 @@
17451728
1746 # Fill in icons1729 # Fill in icons
1747 for i in xrange(1,5):1730 for i in xrange(1,5):
1748 # Get icon name from dictionary in Weather object for Google icons1731 # Get icon name from dictionary in Weather object for Yahoo condition codes
1749 try:1732 try:
1750 conds = Weather._GoogleConditions.get(icons[i-1])1733 conds = Weather._YahooConditions.get(icons[i-1])
1751 if conds != None:1734 if conds != None:
1752 google_icon = conds[0]1735 yahoo_icon = conds[0]
1753 else:1736 else:
1754 log.error("ExtendedForecast: unknown Google weather condition '%s'" % icons[i-1])1737 log.error("ExtendedForecast: unknown Yahoo weather condition code '%s'" % icons[i-1])
1755 log.error(forecast.forecast)1738 log.error(forecast.forecast)
1756 google_icon = 'weather-indicator-unknown'1739 yahoo_icon = 'weather-indicator-unknown'
1757 self.builder.get_object('day%simage' % str(i)).set_from_icon_name(google_icon,gtk.ICON_SIZE_BUTTON)1740 self.builder.get_object('day%simage' % str(i)).set_from_icon_name(yahoo_icon,gtk.ICON_SIZE_BUTTON)
1758 except IndexError:1741 except IndexError:
1759 log.error("ExtendedForecast: Google didn't return condition for %s days" % i-1)1742 log.error("ExtendedForecast: Yahoo didn't return condition for %s days" % i-1)
1760 log.error(forecast.forecast)1743 log.error(forecast.forecast)
17611744
1762 # Fill in condition labels1745 # Fill in condition labels

Subscribers

People subscribed via source and target branches

to all changes: