duongve commited on
Commit
3067763
·
verified ·
1 Parent(s): 320a5a6

Update weather_api.py

Browse files
Files changed (1) hide show
  1. weather_api.py +79 -53
weather_api.py CHANGED
@@ -1,54 +1,80 @@
1
- import requests
2
- from datetime import datetime, timedelta
3
-
4
-
5
- def is_time_difference_greater_than_one_hour(time1, time2):
6
- datetime1 = datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
7
- datetime2 = datetime.strptime(time2, "%Y-%m-%d %H:%M:%S")
8
-
9
- difference = abs(datetime1 - datetime2)
10
-
11
- return difference >= timedelta(hours=1)
12
-
13
- API_KEY = '0eab9492e3024ce4969105357241507'
14
- weather_cache = {}
15
- date_check = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
16
-
17
- def get_weather_data(city = "",lat = "",lon = ""):
18
- if city !="":
19
- url = f'http://api.weatherapi.com/v1/forecast.json?key={API_KEY}&q={city}&days=5'
20
- response = requests.get(url)
21
- if response.status_code == 200:
22
- return response.json()
23
- else:
24
- return None
25
- elif lat != "" and lon != "":
26
- url = f'http://api.weatherapi.com/v1/forecast.json?key={API_KEY}&q={lat},{lon}&days=5'
27
- response = requests.get(url)
28
- if response.status_code == 200:
29
- return response.json()
30
- else:
31
- return None
32
- return None
33
-
34
- def save_weather_data(city, data):
35
- global weather_cache
36
- timestamp = datetime.now()
37
- weather_cache[city] = {
38
- 'data': data,
39
- 'timestamp': timestamp
40
- }
41
-
42
- def get_cached_weather_data(city):
43
- global weather_cache, date_check
44
- #Remove all weather_cache when more than 50 caches or There's a one-hour time difference.
45
- if len(weather_cache) >= 50 or is_time_difference_greater_than_one_hour(date_check,datetime.now().strftime("%Y-%m-%d %H:%M:%S")):
46
- weather_cache = {}
47
- date_check = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
48
-
49
- cached_data = weather_cache.get(city)
50
- if cached_data:
51
- # Check if the cached data is from today
52
- if cached_data['timestamp'].date() == datetime.now().date():
53
- return cached_data['data']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  return None
 
1
+ import requests
2
+ from datetime import datetime, timedelta
3
+
4
+
5
+ def is_time_difference_greater_than_one_hour(time1, time2):
6
+ datetime1 = datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
7
+ datetime2 = datetime.strptime(time2, "%Y-%m-%d %H:%M:%S")
8
+
9
+ difference = abs(datetime1 - datetime2)
10
+
11
+ return difference >= timedelta(hours=1)
12
+
13
+ API_KEY = '0eab9492e3024ce4969105357241507'
14
+ weather_cache = {}
15
+ date_check = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
16
+
17
+ def get_weather_data(city = "",lat = "",lon = "",day = 5):
18
+ if city !="":
19
+ url = f'http://api.weatherapi.com/v1/forecast.json?key={API_KEY}&q={city}&days={day}'
20
+ response = requests.get(url)
21
+ if response.status_code == 200:
22
+ return response.json()
23
+ else:
24
+ return None
25
+ elif lat != "" and lon != "":
26
+ url = f'http://api.weatherapi.com/v1/forecast.json?key={API_KEY}&q={lat},{lon}&days={day}'
27
+ response = requests.get(url)
28
+ if response.status_code == 200:
29
+ return response.json()
30
+ else:
31
+ return None
32
+ return None
33
+
34
+ def more_weather_data(city = '',lat = '',lon = ''):
35
+ global weather_cache
36
+ #print(weather_cache[lat+lon])
37
+ if city != '':
38
+ wanted_day = weather_cache[city]['days'] + 4
39
+ weather_cache.pop(city,None)
40
+ data = get_weather_data(city = city, day = wanted_day)
41
+ weather_cache[city] ={
42
+ 'data': data,
43
+ 'timestamp': datetime.now(),
44
+ 'days' : wanted_day
45
+ }
46
+ return data
47
+ elif lat != '' and lon != '':
48
+ wanted_day = weather_cache[lat+lon]['days'] + 4
49
+ weather_cache.pop(lat+lon,None)
50
+ data = get_weather_data(lat = lat, lon = lon, day = wanted_day)
51
+ weather_cache[lat+lon] ={
52
+ 'data': data,
53
+ 'timestamp': datetime.now(),
54
+ 'days' : wanted_day
55
+ }
56
+ return data
57
+ return None
58
+
59
+ def save_weather_data(city, data):
60
+ global weather_cache
61
+ timestamp = datetime.now()
62
+ weather_cache[city] = {
63
+ 'data': data,
64
+ 'timestamp': timestamp,
65
+ 'days' : 5
66
+ }
67
+
68
+ def get_cached_weather_data(city):
69
+ global weather_cache, date_check
70
+ #Remove all weather_cache when more than 50 caches or There's a one-hour time difference.
71
+ if len(weather_cache) >= 50 or is_time_difference_greater_than_one_hour(date_check,datetime.now().strftime("%Y-%m-%d %H:%M:%S")):
72
+ weather_cache = {}
73
+ date_check = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
74
+
75
+ cached_data = weather_cache.get(city)
76
+ if cached_data:
77
+ # Check if the cached data is from today
78
+ if cached_data['timestamp'].date() == datetime.now().date():
79
+ return cached_data['data']
80
  return None