diff --git a/bots/weather/readme.md b/bots/weather/readme.md index d16cbba..68f84a9 100644 --- a/bots/weather/readme.md +++ b/bots/weather/readme.md @@ -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) diff --git a/bots/weather/.weather_config b/bots/weather/weather.conf similarity index 100% rename from bots/weather/.weather_config rename to bots/weather/weather.conf diff --git a/bots/weather/weather.py b/bots/weather/weather.py index 6cddb0f..bc5891a 100644 --- a/bots/weather/weather.py +++ b/bots/weather/weather.py @@ -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=\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