Commit
·
feb2ad3
1
Parent(s):
ba775a4
back to 0.0.2.5 V Beta patch
Browse files- LoadBalancer.py +45 -80
LoadBalancer.py
CHANGED
@@ -19,7 +19,7 @@ download_progress = {}
|
|
19 |
|
20 |
class LoadBalancer:
|
21 |
def __init__(self, cache_dir, index_file, token, repo, polling_interval=10, max_retries=3, initial_delay=1):
|
22 |
-
self.version = "0.0.2.
|
23 |
self.instances = []
|
24 |
self.instances_health = {}
|
25 |
self.polling_interval = polling_interval
|
@@ -33,15 +33,13 @@ class LoadBalancer:
|
|
33 |
self.REPO = repo
|
34 |
self.FILM_STORE_JSON_PATH = os.path.join(cache_dir, "film_store.json")
|
35 |
self.TV_STORE_JSON_PATH = os.path.join(cache_dir, "tv_store.json")
|
36 |
-
self.TEMP_FILM_STORE_JSON_PATH = os.path.join(cache_dir, "temp_film_store.json")
|
37 |
-
self.TEMP_TV_STORE_JSON_PATH = os.path.join(cache_dir, "temp_tv_store.json")
|
38 |
self.file_structure = None
|
39 |
|
40 |
# Ensure CACHE_DIR exists
|
41 |
if not os.path.exists(self.CACHE_DIR):
|
42 |
os.makedirs(self.CACHE_DIR)
|
43 |
|
44 |
-
for path in [self.FILM_STORE_JSON_PATH, self.TV_STORE_JSON_PATH
|
45 |
if not os.path.exists(path):
|
46 |
with open(path, 'w') as json_file:
|
47 |
json.dump({}, json_file)
|
@@ -96,21 +94,50 @@ class LoadBalancer:
|
|
96 |
|
97 |
logging.info(f"Processing report from {instance_url}")
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
# Process films
|
100 |
for title, path in film_store.items():
|
101 |
url = f"{instance_url}/api/film/{title.replace(' ', '%20')}"
|
102 |
-
|
103 |
|
104 |
# Process TV shows
|
105 |
for title, seasons in tv_store.items():
|
106 |
for season, episodes in seasons.items():
|
107 |
for episode, path in episodes.items():
|
108 |
url = f"{instance_url}/api/tv/{title.replace(' ', '%20')}/{season.replace(' ', '%20')}/{episode.replace(' ', '%20')}"
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
-
logging.info("Film and TV Stores processed successfully.")
|
112 |
self.update_instances_health(instance=instance_url, cache_size=cache_size)
|
113 |
-
self.replace_store_files()
|
114 |
|
115 |
def start_polling(self):
|
116 |
logging.info("Starting polling.")
|
@@ -151,44 +178,36 @@ class LoadBalancer:
|
|
151 |
print(f"Error getting system proxies: {e}")
|
152 |
return {}
|
153 |
|
154 |
-
def update_film_store_json(self,
|
155 |
"""
|
156 |
Updates the film store JSON with the new file.
|
157 |
|
158 |
Args:
|
159 |
title (str): The title of the film.
|
160 |
url (str): The url.
|
161 |
-
temp (bool): If True, update the temporary JSON file. Defaults to False.
|
162 |
"""
|
163 |
-
|
164 |
|
165 |
film_store_data = {}
|
166 |
-
if os.path.exists(
|
167 |
-
with open(
|
168 |
film_store_data = json.load(json_file)
|
169 |
|
170 |
film_store_data[title] = url
|
171 |
|
172 |
-
with open(
|
173 |
json.dump(film_store_data, json_file, indent=2)
|
174 |
print(f'Film store updated with {title}.')
|
175 |
|
176 |
-
def update_tv_store_json(self, title, season, episode, url
|
177 |
"""
|
178 |
Updates the TV store JSON with the new file, organizing by title, season, and episode.
|
179 |
-
|
180 |
-
Args:
|
181 |
-
title (str): The title of the TV show.
|
182 |
-
season (str): The season of the TV show.
|
183 |
-
episode (str): The episode of the TV show.
|
184 |
-
url (str): The url.
|
185 |
-
temp (bool): If True, update the temporary JSON file. Defaults to False.
|
186 |
"""
|
187 |
-
|
188 |
|
189 |
tv_store_data = {}
|
190 |
-
if os.path.exists(
|
191 |
-
with open(
|
192 |
tv_store_data = json.load(json_file)
|
193 |
|
194 |
if title not in tv_store_data:
|
@@ -199,7 +218,7 @@ class LoadBalancer:
|
|
199 |
|
200 |
tv_store_data[title][season][episode] = url
|
201 |
|
202 |
-
with open(
|
203 |
json.dump(tv_store_data, json_file, indent=2)
|
204 |
|
205 |
print(f'TV store updated with {title}, {season}, {episode}.')
|
@@ -237,60 +256,6 @@ class LoadBalancer:
|
|
237 |
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
|
238 |
return re.match(regex, url) is not None
|
239 |
|
240 |
-
@staticmethod
|
241 |
-
def validate_film_store_data(data):
|
242 |
-
"""
|
243 |
-
Validate the film store data to ensure it contains valid URLs.
|
244 |
-
|
245 |
-
:param data: The film store data to validate
|
246 |
-
:return: True if valid, False otherwise
|
247 |
-
"""
|
248 |
-
for key, value in data.items():
|
249 |
-
if not isinstance(key, str) or not LoadBalancer.is_valid_url(value):
|
250 |
-
return False
|
251 |
-
return True
|
252 |
-
|
253 |
-
@staticmethod
|
254 |
-
def validate_tv_store_data(data):
|
255 |
-
"""
|
256 |
-
Validate the TV store data to ensure it contains valid URLs.
|
257 |
-
|
258 |
-
:param data: The TV store data to validate
|
259 |
-
:return: True if valid, False otherwise
|
260 |
-
"""
|
261 |
-
for show, seasons in data.items():
|
262 |
-
if not isinstance(show, str) or not isinstance(seasons, dict):
|
263 |
-
return False
|
264 |
-
for season, episodes in seasons.items():
|
265 |
-
if not isinstance(season, str) or not isinstance(episodes, dict):
|
266 |
-
return False
|
267 |
-
for episode, url in episodes.items():
|
268 |
-
if not isinstance(episode, str) or not LoadBalancer.is_valid_url(url):
|
269 |
-
return False
|
270 |
-
return True
|
271 |
-
|
272 |
-
def replace_store_files(self):
|
273 |
-
"""
|
274 |
-
Replace the actual store JSON files with the temporary ones if they contain valid data.
|
275 |
-
"""
|
276 |
-
try:
|
277 |
-
# Load the temporary JSON files
|
278 |
-
with open(self.TEMP_FILM_STORE_JSON_PATH, 'r') as film_file, open(self.TEMP_TV_STORE_JSON_PATH, 'r') as tv_file:
|
279 |
-
temp_film_store_data = json.load(film_file)
|
280 |
-
temp_tv_store_data = json.load(tv_file)
|
281 |
-
|
282 |
-
# Validate the temporary JSON data
|
283 |
-
if self.validate_film_store_data(temp_film_store_data) and self.validate_tv_store_data(temp_tv_store_data):
|
284 |
-
# Replace the actual store files with the temporary files
|
285 |
-
with open(self.FILM_STORE_JSON_PATH, 'w') as film_file, open(self.TV_STORE_JSON_PATH, 'w') as tv_file:
|
286 |
-
json.dump(temp_film_store_data, film_file)
|
287 |
-
json.dump(temp_tv_store_data, tv_file)
|
288 |
-
print("Replaced actual store JSON files with temporary ones.")
|
289 |
-
else:
|
290 |
-
print("Temporary JSON data is invalid. Skipping replacement.")
|
291 |
-
except Exception as e:
|
292 |
-
print(f"An error occurred while replacing store files: {e}")
|
293 |
-
|
294 |
#################################################################
|
295 |
|
296 |
def update_instances_health(self, instance, cache_size):
|
|
|
19 |
|
20 |
class LoadBalancer:
|
21 |
def __init__(self, cache_dir, index_file, token, repo, polling_interval=10, max_retries=3, initial_delay=1):
|
22 |
+
self.version = "0.0.2.5 V Beta"
|
23 |
self.instances = []
|
24 |
self.instances_health = {}
|
25 |
self.polling_interval = polling_interval
|
|
|
33 |
self.REPO = repo
|
34 |
self.FILM_STORE_JSON_PATH = os.path.join(cache_dir, "film_store.json")
|
35 |
self.TV_STORE_JSON_PATH = os.path.join(cache_dir, "tv_store.json")
|
|
|
|
|
36 |
self.file_structure = None
|
37 |
|
38 |
# Ensure CACHE_DIR exists
|
39 |
if not os.path.exists(self.CACHE_DIR):
|
40 |
os.makedirs(self.CACHE_DIR)
|
41 |
|
42 |
+
for path in [self.FILM_STORE_JSON_PATH, self.TV_STORE_JSON_PATH]:
|
43 |
if not os.path.exists(path):
|
44 |
with open(path, 'w') as json_file:
|
45 |
json.dump({}, json_file)
|
|
|
94 |
|
95 |
logging.info(f"Processing report from {instance_url}")
|
96 |
|
97 |
+
temp_film_store_path = os.path.join(self.CACHE_DIR, "temp_film_store.json")
|
98 |
+
temp_tv_store_path = os.path.join(self.CACHE_DIR, "temp_tv_store.json")
|
99 |
+
|
100 |
+
temp_film_store_data = {}
|
101 |
+
temp_tv_store_data = {}
|
102 |
+
|
103 |
+
if os.path.exists(temp_film_store_path):
|
104 |
+
with open(temp_film_store_path, 'r') as json_file:
|
105 |
+
temp_film_store_data = json.load(json_file)
|
106 |
+
|
107 |
+
if os.path.exists(temp_tv_store_path):
|
108 |
+
with open(temp_tv_store_path, 'r') as json_file:
|
109 |
+
temp_tv_store_data = json.load(json_file)
|
110 |
+
|
111 |
# Process films
|
112 |
for title, path in film_store.items():
|
113 |
url = f"{instance_url}/api/film/{title.replace(' ', '%20')}"
|
114 |
+
temp_film_store_data[title] = url
|
115 |
|
116 |
# Process TV shows
|
117 |
for title, seasons in tv_store.items():
|
118 |
for season, episodes in seasons.items():
|
119 |
for episode, path in episodes.items():
|
120 |
url = f"{instance_url}/api/tv/{title.replace(' ', '%20')}/{season.replace(' ', '%20')}/{episode.replace(' ', '%20')}"
|
121 |
+
if title not in temp_tv_store_data:
|
122 |
+
temp_tv_store_data[title] = {}
|
123 |
+
if season not in temp_tv_store_data[title]:
|
124 |
+
temp_tv_store_data[title][season] = {}
|
125 |
+
temp_tv_store_data[title][season][episode] = url
|
126 |
+
|
127 |
+
# Write temporary JSON files
|
128 |
+
with open(temp_film_store_path, 'w') as json_file:
|
129 |
+
json.dump(temp_film_store_data, json_file, indent=2)
|
130 |
+
|
131 |
+
with open(temp_tv_store_path, 'w') as json_file:
|
132 |
+
json.dump(temp_tv_store_data, json_file, indent=2)
|
133 |
+
|
134 |
+
logging.info("Temporary Film and TV Stores processed successfully.")
|
135 |
+
|
136 |
+
# Replace actual JSON files with temporary JSON files
|
137 |
+
os.replace(temp_film_store_path, self.FILM_STORE_JSON_PATH)
|
138 |
+
os.replace(temp_tv_store_path, self.TV_STORE_JSON_PATH)
|
139 |
|
|
|
140 |
self.update_instances_health(instance=instance_url, cache_size=cache_size)
|
|
|
141 |
|
142 |
def start_polling(self):
|
143 |
logging.info("Starting polling.")
|
|
|
178 |
print(f"Error getting system proxies: {e}")
|
179 |
return {}
|
180 |
|
181 |
+
def update_film_store_json(self,title, url):
|
182 |
"""
|
183 |
Updates the film store JSON with the new file.
|
184 |
|
185 |
Args:
|
186 |
title (str): The title of the film.
|
187 |
url (str): The url.
|
|
|
188 |
"""
|
189 |
+
FILM_STORE_JSON_PATH = self.FILM_STORE_JSON_PATH
|
190 |
|
191 |
film_store_data = {}
|
192 |
+
if os.path.exists(FILM_STORE_JSON_PATH):
|
193 |
+
with open(FILM_STORE_JSON_PATH, 'r') as json_file:
|
194 |
film_store_data = json.load(json_file)
|
195 |
|
196 |
film_store_data[title] = url
|
197 |
|
198 |
+
with open(FILM_STORE_JSON_PATH, 'w') as json_file:
|
199 |
json.dump(film_store_data, json_file, indent=2)
|
200 |
print(f'Film store updated with {title}.')
|
201 |
|
202 |
+
def update_tv_store_json(self, title, season, episode, url):
|
203 |
"""
|
204 |
Updates the TV store JSON with the new file, organizing by title, season, and episode.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
"""
|
206 |
+
TV_STORE_JSON_PATH = self.TV_STORE_JSON_PATH
|
207 |
|
208 |
tv_store_data = {}
|
209 |
+
if os.path.exists(TV_STORE_JSON_PATH):
|
210 |
+
with open(TV_STORE_JSON_PATH, 'r') as json_file:
|
211 |
tv_store_data = json.load(json_file)
|
212 |
|
213 |
if title not in tv_store_data:
|
|
|
218 |
|
219 |
tv_store_data[title][season][episode] = url
|
220 |
|
221 |
+
with open(TV_STORE_JSON_PATH, 'w') as json_file:
|
222 |
json.dump(tv_store_data, json_file, indent=2)
|
223 |
|
224 |
print(f'TV store updated with {title}, {season}, {episode}.')
|
|
|
256 |
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
|
257 |
return re.match(regex, url) is not None
|
258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
#################################################################
|
260 |
|
261 |
def update_instances_health(self, instance, cache_size):
|