From 12ade665af9590407f71b8bb1315e4717d253544 Mon Sep 17 00:00:00 2001 From: Zachary Christman Date: Thu, 28 Jul 2022 22:14:32 -0400 Subject: [PATCH 1/4] Adding current progress on mini capstone. Completed generating the web token and working on performing requests on the WeatherKit API now. --- code/zach/mini-capstone.py | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 code/zach/mini-capstone.py diff --git a/code/zach/mini-capstone.py b/code/zach/mini-capstone.py new file mode 100644 index 00000000..56b4ecb0 --- /dev/null +++ b/code/zach/mini-capstone.py @@ -0,0 +1,46 @@ +from authlib.jose import jwt # https://docs.authlib.org/en/latest/jose/index.html +import time # https://www.systutorials.com/how-to-get-the-epoch-timestamp-in-python/ +import requests + + +def create_key(signature_alg, key_id, team_id, service_id, iat, key_path): + """This function takes the required information as arguments and returns an encoded key to pass as a parameter for the Apple WeatherKit API.""" + with open(key_path, 'rb') as f: + key = f.read() + + header = { + "alg": signature_alg, + "kid": key_id, + "id": f"{team_id}.{service_id}", + } + + payload = { + "iss": team_id, + "iat": iat, + "exp": iat + 100, + "sub": service_id + } + + s = jwt.encode(header, payload, key) + + return s + +def get_weather(language='en', latitude=0, longitude=0, web_token): + response = requests.get(f"https://weatherkit.apple.com/api/v1/weather/{language}/{latitude}/{longitude}", params={ + 'Authorization': f'Bearer {web_token}' + }) + print(response.json()) + +def main(): + web_token = create_key("ES256", + "74B4NT7KNA", + "AJAJ8VTADD", + "com.homedashboard.test", + int(time.time()), + "C:/Users/zacha/OneDrive/Coding/Keys/Home_Dashboard/WeatherKit/AuthKey_74B4NT7KNA.p8" + ) + + + + +main() \ No newline at end of file From 28528bcfa5dff66a99b11acf21fe5e1794abb343 Mon Sep 17 00:00:00 2001 From: Zachary Christman Date: Fri, 29 Jul 2022 00:26:42 -0400 Subject: [PATCH 2/4] Wrote code for the get weather portion, but receiving a 500 HTTP response so need to debug. --- code/zach/mini-capstone.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/code/zach/mini-capstone.py b/code/zach/mini-capstone.py index 56b4ecb0..5a75ada6 100644 --- a/code/zach/mini-capstone.py +++ b/code/zach/mini-capstone.py @@ -17,7 +17,7 @@ def create_key(signature_alg, key_id, team_id, service_id, iat, key_path): payload = { "iss": team_id, "iat": iat, - "exp": iat + 100, + "exp": iat + 3600, "sub": service_id } @@ -25,11 +25,18 @@ def create_key(signature_alg, key_id, team_id, service_id, iat, key_path): return s -def get_weather(language='en', latitude=0, longitude=0, web_token): - response = requests.get(f"https://weatherkit.apple.com/api/v1/weather/{language}/{latitude}/{longitude}", params={ - 'Authorization': f'Bearer {web_token}' +def get_weather(language, latitude, longitude, web_token, timezone): + response = requests.get(f"https://weatherkit.apple.com/api/v1/weather/{language}/{latitude}/{longitude}", headers = { + 'Authorization': f'Bearer {web_token}', + 'Accept': 'application/json' + }, + params = { + 'timezone': timezone, + 'dataSets': 'currentWeather' }) - print(response.json()) + print(response) + #current_weather = response.json() + #return current_weather def main(): web_token = create_key("ES256", @@ -40,7 +47,6 @@ def main(): "C:/Users/zacha/OneDrive/Coding/Keys/Home_Dashboard/WeatherKit/AuthKey_74B4NT7KNA.p8" ) - - + get_weather('en', 38.933868, -77.177261, web_token, 'America/New_York') main() \ No newline at end of file From 889a1ed4672c12f079e59f36fec4eb504b350479 Mon Sep 17 00:00:00 2001 From: Zachary Christman Date: Fri, 29 Jul 2022 01:05:37 -0400 Subject: [PATCH 3/4] Completed the get weather API call and added a temp converter. --- code/zach/mini-capstone.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/code/zach/mini-capstone.py b/code/zach/mini-capstone.py index 5a75ada6..a5af7496 100644 --- a/code/zach/mini-capstone.py +++ b/code/zach/mini-capstone.py @@ -1,15 +1,13 @@ -from authlib.jose import jwt # https://docs.authlib.org/en/latest/jose/index.html +import jwt import time # https://www.systutorials.com/how-to-get-the-epoch-timestamp-in-python/ import requests - def create_key(signature_alg, key_id, team_id, service_id, iat, key_path): """This function takes the required information as arguments and returns an encoded key to pass as a parameter for the Apple WeatherKit API.""" with open(key_path, 'rb') as f: key = f.read() header = { - "alg": signature_alg, "kid": key_id, "id": f"{team_id}.{service_id}", } @@ -21,11 +19,12 @@ def create_key(signature_alg, key_id, team_id, service_id, iat, key_path): "sub": service_id } - s = jwt.encode(header, payload, key) + s = jwt.encode(payload, key, algorithm=signature_alg, headers=header) return s def get_weather(language, latitude, longitude, web_token, timezone): + """This function takes the location data, web token from create_key(), and the timezone to return the current weather condition and temperature from the Apple WeatherKit API.""" response = requests.get(f"https://weatherkit.apple.com/api/v1/weather/{language}/{latitude}/{longitude}", headers = { 'Authorization': f'Bearer {web_token}', 'Accept': 'application/json' @@ -34,9 +33,16 @@ def get_weather(language, latitude, longitude, web_token, timezone): 'timezone': timezone, 'dataSets': 'currentWeather' }) - print(response) - #current_weather = response.json() - #return current_weather + + weather = response.json() + + current_condition = weather['currentWeather']['conditionCode'] + current_temp = weather['currentWeather']['temperature'] + + return current_condition, current_temp + +def temp_converter(temp_celsius): + return temp_celsius * 1.8 + 32 def main(): web_token = create_key("ES256", @@ -47,6 +53,9 @@ def main(): "C:/Users/zacha/OneDrive/Coding/Keys/Home_Dashboard/WeatherKit/AuthKey_74B4NT7KNA.p8" ) - get_weather('en', 38.933868, -77.177261, web_token, 'America/New_York') + current_condition, current_temp = get_weather('en', 38.933868, -77.177261, web_token, 'America/New_York') + temp_fahrenheit = temp_converter(current_temp) + + print(f'It is currently {current_condition} and {round(temp_fahrenheit, 2)} degrees Fahrenheit ({current_temp} degrees Celsius).') main() \ No newline at end of file From 77c92f75afd89968c3e482e464d6c91194c62cd0 Mon Sep 17 00:00:00 2001 From: Zachary Christman Date: Sat, 30 Jul 2022 00:14:51 -0400 Subject: [PATCH 4/4] Completed weather request app using Apple WeatherKit API. --- code/zach/mini-capstone.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/zach/mini-capstone.py b/code/zach/mini-capstone.py index a5af7496..3c56e448 100644 --- a/code/zach/mini-capstone.py +++ b/code/zach/mini-capstone.py @@ -52,7 +52,7 @@ def main(): int(time.time()), "C:/Users/zacha/OneDrive/Coding/Keys/Home_Dashboard/WeatherKit/AuthKey_74B4NT7KNA.p8" ) - + print(web_token) current_condition, current_temp = get_weather('en', 38.933868, -77.177261, web_token, 'America/New_York') temp_fahrenheit = temp_converter(current_temp)