From a011662a4d0e794745dcd95fc9d3640ebb03fa71 Mon Sep 17 00:00:00 2001 From: Miupro Date: Sun, 31 Oct 2021 03:13:46 +0100 Subject: [PATCH] Weather API --- apis/sbb.py | 7 ++++++- apis/weather.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/apis/sbb.py b/apis/sbb.py index 2ea3b28..b8ceee1 100644 --- a/apis/sbb.py +++ b/apis/sbb.py @@ -1,6 +1,5 @@ import tortilla import requests -import pandas as pd class SBBWrapper: @@ -27,3 +26,9 @@ class SBBWrapper: } return auth + +#Example +query = {'name':'Kreuzlingen'} + +sbb = SBBWrapper() +print(sbb.wrapper.locations.get(params=query)) diff --git a/apis/weather.py b/apis/weather.py index 123d6ee..ef8ffd9 100644 --- a/apis/weather.py +++ b/apis/weather.py @@ -1,5 +1,8 @@ import tortilla import requests +import numpy as np +import datetime + class WeatherWrapper: def __init__(self) -> None: @@ -13,7 +16,6 @@ class WeatherWrapper: 'client_secret': '59603134536c874e47245b4de403e3d3' } - #response = requests.post('https://sso-int.sbb.ch/auth/realms/SBB_Public/protocol/openid-connect/token', data=token_query).json() response = requests.post('https://sso.sbb.ch/auth/realms/SBB_Public/protocol/openid-connect/token', data=token_query).json() token = response["access_token"] auth = { @@ -23,6 +25,50 @@ class WeatherWrapper: } return auth + +class WeatherScoreCalculator: + def calc_weather_score(self, event): + now = datetime.datetime.now().replace(microsecond=0).isoformat() + location = event.location_coordinates + location_string = f'{location[0]}'+f','+f'{location[1]}' + + weather = WeatherWrapper() + raw_weather_data = weather.wrapper.get( now + 'ZP1D:PT3H/weather_code_6h:idx/'+ location_string + '/json') + raw_extracted_weather = raw_weather_data['data'][0]['coordinates'][0]['dates'] + + # Weather Score Calculation + weather_score = [] + for weather_dict in raw_extracted_weather: + weather_score.append(weather_dict['value']) + weather_score = np.array(weather_score) + mean_weather_score = weather_score.mean() + + return mean_weather_score + + +# Example. Event is an event object, (not defined in this file). +""" +wcalc = WeatherScoreCalculator() +print(wcalc.calc_weather_score(event)) +""" + + +# Obsolete +""" +now = datetime.datetime.now().replace(microsecond=0).isoformat() + weather = WeatherWrapper() -print('TEST') -print(weather.wrapper.get('2021-07-12T00:00ZP50D:PT30M/sfc_pressure:hPa,msl_pressure:hPa/47.36669,8.54858+47.35209,7.90779/json')) \ No newline at end of file +raw_weather_data = weather.wrapper.get( now + 'ZP1D:PT3H/weather_code_6h:idx/47.36669,8.54858/json') +raw_extracted_weather = raw_weather_data['data'][0]['coordinates'][0]['dates'] + +print(now) +print(raw_extracted_weather) + +# Weather Score Calculation +weather_score = [] +for weather_dict in raw_extracted_weather: + weather_score.append(weather_dict['value']) +weather_score = np.array(weather_score) +mean_weather_score = weather_score.mean() +print(mean_weather_score) +""" \ No newline at end of file