bots: Update weather bot to use get_config_info().

This commit is contained in:
Robert Hönig 2017-06-18 17:24:30 +02:00 committed by showell
parent 651a21810d
commit a48da1e6ca
3 changed files with 4 additions and 21 deletions

View File

@ -10,7 +10,7 @@ city that does not exist, the bot displays a "Sorry, city not found"
message.
* Before using this bot, you have to generate an OpenWeatherMap API
key and replace the dummy value in .weather_config.
key and replace the dummy value in weather.conf.
![Example Usage](assets/screen1.png)
![Wrong City](assets/screen2.png)

View File

@ -2,21 +2,11 @@
from __future__ import print_function
import requests
import json
import os
import sys
from six.moves.configparser import SafeConfigParser
class WeatherHandler(object):
def __init__(self):
self.directory = os.path.dirname(os.path.realpath(__file__)) + '/'
self.config_name = '.weather_config'
def initialize(self, bot_handler):
self.api_key = bot_handler.get_config_info('weather', 'weather-config')['key']
self.response_pattern = 'Weather in {}, {}:\n{} F / {} C\n{}'
if not os.path.exists(self.directory + self.config_name):
print('Weather bot config file not found, please set it up in {} file in this bot main directory'
'\n\nUsing format:\n\n[weather-config]\nkey=<OpenWeatherMap API key here>\n\n'.format(self.config_name))
sys.exit(1)
super(WeatherHandler, self).__init__()
def usage(self):
return '''
@ -38,7 +28,7 @@ class WeatherHandler(object):
response = help_content
else:
url = 'http://api.openweathermap.org/data/2.5/weather?q=' + message['content'] + '&APPID='
r = requests.get(url + get_weather_api_key_from_config(self.directory, self.config_name))
r = requests.get(url + self.api_key)
if "city not found" in r.text:
response = "Sorry, city not found"
else:
@ -65,11 +55,4 @@ def to_celsius(temp_kelvin):
def to_fahrenheit(temp_kelvin):
return int(temp_kelvin) * 9 / 5 - 459.67
def get_weather_api_key_from_config(directory, config_name):
config = SafeConfigParser()
with open(directory + config_name, 'r') as config_file:
config.readfp(config_file)
return config.get("weather-config", "key")
handler_class = WeatherHandler