ChandimaPrabath commited on
Commit
2b2a835
·
1 Parent(s): 0cbbc1a

try 0.0.2.6 V Beta

Browse files
Files changed (1) hide show
  1. LoadBalancer.py +77 -23
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.4 V Beta"
23
  self.instances = []
24
  self.instances_health = {}
25
  self.polling_interval = polling_interval
@@ -33,13 +33,15 @@ 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.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,25 +95,22 @@ class LoadBalancer:
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,36 +151,44 @@ class LoadBalancer:
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,7 +199,7 @@ class LoadBalancer:
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}.')
@@ -230,12 +237,59 @@ class LoadBalancer:
230
  r'(?:/?|[/?]\S+)$', re.IGNORECASE)
231
  return re.match(regex, url) is not None
232
 
233
- #################################################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
- def update_instances_health(self, instance, cache_size):
236
- self.instances_health[instance] = {"used":cache_size["cache_size"],
237
- "total": "50 GB"}
238
- logging.info(f"Updated instance {instance} with cache size {cache_size}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
 
240
 
241
  def download_film_to_best_instance(self, title):
 
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.6 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.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, self.TEMP_FILM_STORE_JSON_PATH, self.TEMP_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
  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, temp=True)
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, temp=True)
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
  print(f"Error getting system proxies: {e}")
152
  return {}
153
 
154
+ def update_film_store_json(self, title, url, temp=False):
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
+ file_path = self.TEMP_FILM_STORE_JSON_PATH if temp else self.FILM_STORE_JSON_PATH
164
 
165
  film_store_data = {}
166
+ if os.path.exists(file_path):
167
+ with open(file_path, 'r') as json_file:
168
  film_store_data = json.load(json_file)
169
 
170
  film_store_data[title] = url
171
 
172
+ with open(file_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, temp=False):
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
+ file_path = self.TEMP_TV_STORE_JSON_PATH if temp else self.TV_STORE_JSON_PATH
188
 
189
  tv_store_data = {}
190
+ if os.path.exists(file_path):
191
+ with open(file_path, 'r') as json_file:
192
  tv_store_data = json.load(json_file)
193
 
194
  if title not in tv_store_data:
 
199
 
200
  tv_store_data[title][season][episode] = url
201
 
202
+ with open(file_path, 'w') as json_file:
203
  json.dump(tv_store_data, json_file, indent=2)
204
 
205
  print(f'TV store updated with {title}, {season}, {episode}.')
 
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
  def download_film_to_best_instance(self, title):