Commit
·
0cbbc1a
1
Parent(s):
7ddeef5
revert back to 0.0.2.4 V Beta
Browse files- LoadBalancer.py +19 -25
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)
|
@@ -95,22 +93,25 @@ class LoadBalancer:
|
|
95 |
cache_size = report.get('cache_size')
|
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 |
-
self.update_film_store_json(title, url
|
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 |
-
self.update_tv_store_json(title, season, episode, url
|
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 +152,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 +192,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}.')
|
@@ -223,6 +216,7 @@ class LoadBalancer:
|
|
223 |
except Exception as e:
|
224 |
print(f"An error occurred while clearing the data in {file_path}: {e}")
|
225 |
|
|
|
226 |
@staticmethod
|
227 |
def is_valid_url(url):
|
228 |
# Simple URL validation (could be more complex if needed)
|
|
|
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.4 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)
|
|
|
93 |
cache_size = report.get('cache_size')
|
94 |
|
95 |
logging.info(f"Processing report from {instance_url}")
|
96 |
+
# logging.info(f"Film Store: {film_store}")
|
97 |
+
# logging.info(f"TV Store: {tv_store}")
|
98 |
+
# logging.info(f"Cache Size: {cache_size}")
|
99 |
|
100 |
# Process films
|
101 |
for title, path in film_store.items():
|
102 |
url = f"{instance_url}/api/film/{title.replace(' ', '%20')}"
|
103 |
+
self.update_film_store_json(title, url)
|
104 |
|
105 |
+
|
106 |
# Process TV shows
|
107 |
for title, seasons in tv_store.items():
|
108 |
for season, episodes in seasons.items():
|
109 |
for episode, path in episodes.items():
|
110 |
url = f"{instance_url}/api/tv/{title.replace(' ', '%20')}/{season.replace(' ', '%20')}/{episode.replace(' ', '%20')}"
|
111 |
+
self.update_tv_store_json(title, season, episode, url)
|
112 |
|
113 |
logging.info("Film and TV Stores processed successfully.")
|
114 |
self.update_instances_health(instance=instance_url, cache_size=cache_size)
|
|
|
115 |
|
116 |
def start_polling(self):
|
117 |
logging.info("Starting polling.")
|
|
|
152 |
print(f"Error getting system proxies: {e}")
|
153 |
return {}
|
154 |
|
155 |
+
def update_film_store_json(self,title, url):
|
156 |
"""
|
157 |
Updates the film store JSON with the new file.
|
158 |
|
159 |
Args:
|
160 |
title (str): The title of the film.
|
161 |
url (str): The url.
|
|
|
162 |
"""
|
163 |
+
FILM_STORE_JSON_PATH = self.FILM_STORE_JSON_PATH
|
164 |
|
165 |
film_store_data = {}
|
166 |
+
if os.path.exists(FILM_STORE_JSON_PATH):
|
167 |
+
with open(FILM_STORE_JSON_PATH, 'r') as json_file:
|
168 |
film_store_data = json.load(json_file)
|
169 |
|
170 |
film_store_data[title] = url
|
171 |
|
172 |
+
with open(FILM_STORE_JSON_PATH, 'w') as json_file:
|
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 |
+
TV_STORE_JSON_PATH = self.TV_STORE_JSON_PATH
|
181 |
|
182 |
tv_store_data = {}
|
183 |
+
if os.path.exists(TV_STORE_JSON_PATH):
|
184 |
+
with open(TV_STORE_JSON_PATH, 'r') as json_file:
|
185 |
tv_store_data = json.load(json_file)
|
186 |
|
187 |
if title not in tv_store_data:
|
|
|
192 |
|
193 |
tv_store_data[title][season][episode] = url
|
194 |
|
195 |
+
with open(TV_STORE_JSON_PATH, 'w') as json_file:
|
196 |
json.dump(tv_store_data, json_file, indent=2)
|
197 |
|
198 |
print(f'TV store updated with {title}, {season}, {episode}.')
|
|
|
216 |
except Exception as e:
|
217 |
print(f"An error occurred while clearing the data in {file_path}: {e}")
|
218 |
|
219 |
+
|
220 |
@staticmethod
|
221 |
def is_valid_url(url):
|
222 |
# Simple URL validation (could be more complex if needed)
|