Skip to content

Commit fc12aaa

Browse files
authored
Merge pull request #26 from ianpaul/fixWeather
Modified weather.py to use an Open Weather Map API key.
2 parents 33f0055 + 084ce2c commit fc12aaa

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/flashbake/plugins/weather.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(self, plugin_spec):
3636
AbstractMessagePlugin.__init__(self, plugin_spec, True)
3737
self.define_property('city')
3838
self.define_property('units', required=False, default='imperial')
39+
self.define_property('appid', required=False)
3940
self.share_property('tz', plugin_spec=timezone.PLUGIN_SPEC)
4041
## plugin uses location_location from Location plugin
4142
self.share_property('location_location')
@@ -60,8 +61,12 @@ def addcontext(self, message_file, config):
6061
message_file.write('Couldn\'t determine city to fetch weather.\n')
6162
return False
6263

64+
if self.appid == None:
65+
message_file.write('Open Weather Map requires an API key. For more information see: https://github.com/commandline/flashbake/wiki/Plugins')
66+
return False
67+
6368
# call the open weather map API with the city
64-
weather = self.__getweather(city, self.units)
69+
weather = self.__getweather(city, self.appid, self.units)
6570

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

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

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

8085
try:
8186
logging.debug('Requesting page for %s.' % for_city)

0 commit comments

Comments
 (0)