Skip to content

Commit

Permalink
Merge pull request #26 from ianpaul/fixWeather
Browse files Browse the repository at this point in the history
Modified weather.py to use an Open Weather Map API key.
  • Loading branch information
cmdln committed Mar 13, 2017
2 parents 33f0055 + 084ce2c commit fc12aaa
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/flashbake/plugins/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, plugin_spec):
AbstractMessagePlugin.__init__(self, plugin_spec, True)
self.define_property('city')
self.define_property('units', required=False, default='imperial')
self.define_property('appid', required=False)
self.share_property('tz', plugin_spec=timezone.PLUGIN_SPEC)
## plugin uses location_location from Location plugin
self.share_property('location_location')
Expand All @@ -60,8 +61,12 @@ def addcontext(self, message_file, config):
message_file.write('Couldn\'t determine city to fetch weather.\n')
return False

if self.appid == None:
message_file.write('Open Weather Map requires an API key. For more information see: https://github.com/commandline/flashbake/wiki/Plugins')
return False

# call the open weather map API with the city
weather = self.__getweather(city, self.units)
weather = self.__getweather(city, self.appid, self.units)

if len(weather) > 0:
message_file.write('Current weather for %(city)s: %(description)s. %(temp)i%(temp_units)s. %(humidity)s%% humidity\n'
Expand All @@ -70,12 +75,12 @@ def addcontext(self, message_file, config):
message_file.write('Couldn\'t fetch current weather for city, %s.\n' % city)
return len(weather) > 0

def __getweather(self, city, units='imperial'):
def __getweather(self, city, appid, units='imperial'):
""" This relies on Open Weather Map's API which may change without notice. """

baseurl = 'http://api.openweathermap.org/data/2.5/weather?'
# encode the parameters
for_city = baseurl + urllib.urlencode({'q': city, 'units': units})
for_city = baseurl + urllib.urlencode({'q': city, 'units': units, 'appid': appid})

try:
logging.debug('Requesting page for %s.' % for_city)
Expand Down

0 comments on commit fc12aaa

Please sign in to comment.