def __init__(self, bot): self.__bot = bot config_dict = get_config_from(str(pathlib.Path.cwd()) + '/' + sys.argv[0] + '/climabot/conf/pyowm.conf') self.__owm = pyowm.OWM(os.getenv('OPENWEATHER_KEY'), config_dict) self.__jsonFilename = "weather_users.json" botlanguage.addListener(self)
def test_get_config_from(self): config_file_name = 'test_config.json' path = (pathlib.Path(__file__).parent / config_file_name).absolute() result = config.get_config_from(path) self.assertIsInstance(result, dict) config_file_name = 'non_json' path = (pathlib.Path(__file__).parent / config_file_name).absolute() self.assertRaises(pyowm.commons.exceptions.ConfigurationParseError, config.get_config_from, path)
def test_call_api_behind_socks_proxy(self): # fetch config and overwrite API Key as per env variable config_file_name = 'proxy_socks.json' path = (pathlib.Path() / config_file_name).absolute() cfg = config.get_config_from(path) cfg['api_key'] = self._api_key # go owm = OWM(cfg['api_key'], cfg) wm = owm.weather_manager() result = wm.weather_at_place('London,GB') self.assertIsInstance(result, Observation)
# https://pyowm.readthedocs.io/en/latest/v3/code-recipes.html from datetime import datetime from json import dump from pyowm import OWM from pyowm.utils.config import get_config_from ANS_FILE = "./task3a_answer_{date}.json" if __name__ == "__main__": config = get_config_from("./task3a.json") owm = OWM(config["key"], config) reg = owm.city_id_registry() locations = reg.locations_for("Moscow", country="RU") mgr = owm.weather_manager() lat, lon = locations[0].lat, locations[0].lon one_call = mgr.one_call(lat, lon, exclude="minutely,hourly") week = one_call.forecast_daily[:5] # current + 4 days pressures, temperatures = [], [] for day in week: pressures.append(day.pressure["press"]) temp = day.temperature("celsius")