-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.py
125 lines (81 loc) · 3.3 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import time
import requests
import threading
from datetime import datetime
from weather_data import WeatherData
from rate_limiter_for_Ip import Limiter
from CityLock import CityLock
from ModifyDict import ModifyDict
from db_functions import Db
from aes_encryption import Encrypt
class Cache:
def __init__(self,api_key):
self.data = dict()
self.lock = threading.Condition()
self.city_lock = CityLock()
self.modify_dict = ModifyDict(self.data)
self.db = Db()
self.limiter = Limiter()
self.encrypt = Encrypt()
self.api_key = api_key
def log(self, message):
log_txt = f"[{datetime.now().strftime('%H:%M:%S')}] | Thread ID: {threading.get_ident()} {message}"
print(log_txt)
self.db.add_log(log_txt)
def get_weather_data(self, city):
cache_data = self.return_cache_data(city.lower())
if cache_data:
return cache_data
self.log(f"🌞trying to dump {city} in lock dict...")
self.log(f"nothing in cache for {city}")
self.log(f"{city} going to CityLock class")
lock_city_up = self.city_lock.get_city_lock(city)
self.log(f"{lock_city_up}")
if lock_city_up is not None:
lock_city_up.acquire()
self.log(
f"❌ Locking begins... for {city}. Locked status: {lock_city_up.locked()} "
)
self.log(f"Second time checking in cache for {city}")
cache_data = self.return_cache_data(city.lower())
if cache_data:
self.log(f"Second cache check successful for {city}")
lock_city_up.release()
return cache_data
self.log(f"😭trying to fetch weather for {city}")
# api_key = self.encrypt.get_api_key()
self.log(f"💀 API key is {self.api_key}")
response = requests.get(
f"http://api.openweathermap.org/data/2.5/weather?q={city}&units=metric&appid={self.api_key}"
)
self.log(f"🙌 Done fetching weather for {city} ")
self.log("Start sleep for 10s")
time.sleep(10)
self.log("End sleep for 10s")
return self._return_weather_data(response, lock_city_up)
def return_cache_data(self, city):
cache_check = self.modify_dict.check_cache_expiry(city.lower())
if cache_check:
self.log(f"🖨️ Printed from cache for {city} ")
cache_data = WeatherData.create_weather_json(cache_check)
cache_data.headers.add("Access-Control-Allow-Origin", "*")
return cache_data
else:
return False
def _return_weather_data(self, response, lock):
res = response.json()
weather_data = WeatherData(
res["name"],
res["weather"][0]["description"],
str(time.time()),
str(time.time() + (60 * 10)),
res["main"]["temp"],
res["main"]["feels_like"],
res["main"]["humidity"],
)
self.log(f"Adding to cache..for {res['name']}")
self.modify_dict.add_weather_data(weather_data.city.lower(), weather_data)
lock.release()
weather_data = WeatherData.create_weather_json(weather_data)
weather_data.headers.add("Access-Control-Allow-Origin", "*")
return weather_data