1
- from authlib . jose import jwt # https://docs.authlib.org/en/latest/jose/index.html
1
+ import jwt
2
2
import time # https://www.systutorials.com/how-to-get-the-epoch-timestamp-in-python/
3
3
import requests
4
4
5
-
6
5
def create_key (signature_alg , key_id , team_id , service_id , iat , key_path ):
7
6
"""This function takes the required information as arguments and returns an encoded key to pass as a parameter for the Apple WeatherKit API."""
8
7
with open (key_path , 'rb' ) as f :
9
8
key = f .read ()
10
9
11
10
header = {
12
- "alg" : signature_alg ,
13
11
"kid" : key_id ,
14
12
"id" : f"{ team_id } .{ service_id } " ,
15
13
}
@@ -21,11 +19,12 @@ def create_key(signature_alg, key_id, team_id, service_id, iat, key_path):
21
19
"sub" : service_id
22
20
}
23
21
24
- s = jwt .encode (header , payload , key )
22
+ s = jwt .encode (payload , key , algorithm = signature_alg , headers = header )
25
23
26
24
return s
27
25
28
26
def get_weather (language , latitude , longitude , web_token , timezone ):
27
+ """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."""
29
28
response = requests .get (f"https://weatherkit.apple.com/api/v1/weather/{ language } /{ latitude } /{ longitude } " , headers = {
30
29
'Authorization' : f'Bearer { web_token } ' ,
31
30
'Accept' : 'application/json'
@@ -34,9 +33,16 @@ def get_weather(language, latitude, longitude, web_token, timezone):
34
33
'timezone' : timezone ,
35
34
'dataSets' : 'currentWeather'
36
35
})
37
- print (response )
38
- #current_weather = response.json()
39
- #return current_weather
36
+
37
+ weather = response .json ()
38
+
39
+ current_condition = weather ['currentWeather' ]['conditionCode' ]
40
+ current_temp = weather ['currentWeather' ]['temperature' ]
41
+
42
+ return current_condition , current_temp
43
+
44
+ def temp_converter (temp_celsius ):
45
+ return temp_celsius * 1.8 + 32
40
46
41
47
def main ():
42
48
web_token = create_key ("ES256" ,
@@ -47,6 +53,9 @@ def main():
47
53
"C:/Users/zacha/OneDrive/Coding/Keys/Home_Dashboard/WeatherKit/AuthKey_74B4NT7KNA.p8"
48
54
)
49
55
50
- get_weather ('en' , 38.933868 , - 77.177261 , web_token , 'America/New_York' )
56
+ current_condition , current_temp = get_weather ('en' , 38.933868 , - 77.177261 , web_token , 'America/New_York' )
57
+ temp_fahrenheit = temp_converter (current_temp )
58
+
59
+ print (f'It is currently { current_condition } and { round (temp_fahrenheit , 2 )} degrees Fahrenheit ({ current_temp } degrees Celsius).' )
51
60
52
61
main ()
0 commit comments