### #Library to collect meteodata from srg weather api. #Before the API can be accessed you need to create a user and an application on ### #Author: Simon Huber ### import json import requests import base64 def get_forecast(LatLon_Tuple, access_token): """ Function queries the geolocation forecast from SRG developer API. Code created with the postman project from davosian. Thanks for the help! Check the documentation to get more information on the API: args: LatLon_Tuple: Geolocation which is covered in the SRG API (check out documentation) in the format: 46.9471,7.4441" access_token: Access Token previously got from "get_srg_acces_token" as string returns: The functions returns the json payload of the query. """ url = "https://api.srgssr.ch/srf-meteo/forecast/{LatLon_Tuple}".format(LatLon_Tuple = LatLon_Tuple) payload={} headers = { 'Authorization': 'Bearer {}'.format(access_token) } response = requests.request("GET", url, headers=headers, data=payload) return response.json() def get_srg_acces_token(ConsumerKey, ConsumerSecret): """ Function queries an access token from SRG developer API. Before you can query an access token, you have to create an app on NOTE: The token has a life span of one hour. args: ConsumerKey: CONSUMER KEY as string. ConsumerSecret: CONSUMER SECRET as string. returns: The function returns an access token as string. """ #URL to access token url = 'https://api.srgssr.ch/oauth/v1/accesstoken?grant_type=client_credentials' #convert the credentials to the required format Credentials_str = '{ConsumerKey}:{ConsumerSecret}'.format( ConsumerKey = ConsumerKey, ConsumerSecret = ConsumerSecret ) Credentials_base64_bytes = base64.b64encode(bytes(Credentials_str, 'utf-8')).decode('ascii') Credentials = 'Basic {Credentials_base64_bytes}'.format(Credentials_base64_bytes = Credentials_base64_bytes) #create the headers headers = { 'Authorization': Credentials, 'Cache-Control': 'no-cache', 'Content-Length': '0', 'Postman-Token': '24264e32-2de0-f1e3-f3f8-eab014bb6d76' } #make request response = requests.request("POST", url, headers=headers) if str(response) == '': access_token = response.json()['access_token'] else: raise Exception('Could not get access token. API response was: {}'.format(str(response))) return access_token def geolocation_forecast(LatLon_Tuple, ConsumerKey, ConsumerSecret): """ Function queries the geolocation forecast from SRG developer API. Code created with the postman project from davosian. Thanks for the help! Check the documentation to get more information on the API: args: LatLon_Tuple: Geolocation which is covered in the SRG API (check out documentation) in the format: "46.9471,7.4441" ConsumerKey: CONSUMER KEY as string. ConsumerSecret: CONSUMER SECRET as string. returns: The functions returns the json payload of the query. """ #get access token access_token = get_srg_acces_token(ConsumerKey = ConsumerKey, ConsumerSecret = ConsumerSecret) #get forecast forecast = get_forecast(LatLon_Tuple = LatLon_Tuple, access_token = access_token) return forecast