ChandimaPrabath commited on
Commit
7ddeef5
·
1 Parent(s): e01cdb7

0.0.2.6 V Beta fix

Browse files
Files changed (1) hide show
  1. LoadBalancer.py +38 -32
LoadBalancer.py CHANGED
@@ -12,13 +12,14 @@ import logging
12
  from threading import Thread, Event, Timer
13
  from api import InstancesAPI
14
 
 
15
  CACHE_DIR = os.getenv("CACHE_DIR")
16
 
17
  download_progress = {}
18
 
19
  class LoadBalancer:
20
  def __init__(self, cache_dir, index_file, token, repo, polling_interval=10, max_retries=3, initial_delay=1):
21
- self.version = "0.0.2.5 V Beta"
22
  self.instances = []
23
  self.instances_health = {}
24
  self.polling_interval = polling_interval
@@ -95,39 +96,21 @@ class LoadBalancer:
95
 
96
  logging.info(f"Processing report from {instance_url}")
97
 
98
- # Clear temporary JSON files before processing
99
- self.clear_json_file(self.TEMP_FILM_STORE_JSON_PATH)
100
- self.clear_json_file(self.TEMP_TV_STORE_JSON_PATH)
101
-
102
  # Process films
103
  for title, path in film_store.items():
104
  url = f"{instance_url}/api/film/{title.replace(' ', '%20')}"
105
- self.update_temp_film_store_json(title, url)
106
 
107
  # Process TV shows
108
  for title, seasons in tv_store.items():
109
  for season, episodes in seasons.items():
110
  for episode, path in episodes.items():
111
  url = f"{instance_url}/api/tv/{title.replace(' ', '%20')}/{season.replace(' ', '%20')}/{episode.replace(' ', '%20')}"
112
- self.update_temp_tv_store_json(title, season, episode, url)
113
 
114
  logging.info("Film and TV Stores processed successfully.")
115
- self.replace_store_jsons()
116
  self.update_instances_health(instance=instance_url, cache_size=cache_size)
117
-
118
- def replace_store_jsons(self):
119
- """Replace the actual JSON contents with the temporary JSON contents."""
120
- self.replace_json_content(self.TEMP_FILM_STORE_JSON_PATH, self.FILM_STORE_JSON_PATH)
121
- self.replace_json_content(self.TEMP_TV_STORE_JSON_PATH, self.TV_STORE_JSON_PATH)
122
-
123
- @staticmethod
124
- def replace_json_content(temp_file_path, actual_file_path):
125
- """Replace the content of the actual JSON file with the content of the temporary JSON file."""
126
- with open(temp_file_path, 'r') as temp_file:
127
- temp_data = json.load(temp_file)
128
-
129
- with open(actual_file_path, 'w') as actual_file:
130
- json.dump(temp_data, actual_file, indent=2)
131
 
132
  def start_polling(self):
133
  logging.info("Starting polling.")
@@ -168,26 +151,45 @@ class LoadBalancer:
168
  print(f"Error getting system proxies: {e}")
169
  return {}
170
 
171
- def update_temp_film_store_json(self, title, url):
172
  """
173
- Updates the temporary film store JSON with the new file.
174
 
175
  Args:
176
  title (str): The title of the film.
177
  url (str): The url.
 
178
  """
179
- film_store_data = self.read_json(self.TEMP_FILM_STORE_JSON_PATH)
 
 
 
 
 
 
180
  film_store_data[title] = url
181
 
182
- with open(self.TEMP_FILM_STORE_JSON_PATH, 'w') as json_file:
183
  json.dump(film_store_data, json_file, indent=2)
184
- print(f'Temporary film store updated with {title}.')
185
 
186
- def update_temp_tv_store_json(self, title, season, episode, url):
187
  """
188
- Updates the temporary TV store JSON with the new file, organizing by title, season, and episode.
 
 
 
 
 
 
 
189
  """
190
- tv_store_data = self.read_json(self.TEMP_TV_STORE_JSON_PATH)
 
 
 
 
 
191
 
192
  if title not in tv_store_data:
193
  tv_store_data[title] = {}
@@ -197,10 +199,10 @@ class LoadBalancer:
197
 
198
  tv_store_data[title][season][episode] = url
199
 
200
- with open(self.TEMP_TV_STORE_JSON_PATH, 'w') as json_file:
201
  json.dump(tv_store_data, json_file, indent=2)
202
 
203
- print(f'Temporary TV store updated with {title}, {season}, {episode}.')
204
 
205
  @staticmethod
206
  def clear_json_file(file_path):
@@ -209,10 +211,13 @@ class LoadBalancer:
209
 
210
  :param file_path: Path to the JSON file to be cleared
211
  """
 
212
  empty_data = {}
213
 
214
  try:
 
215
  with open(file_path, 'w') as json_file:
 
216
  json.dump(empty_data, json_file)
217
  print(f"Successfully cleared the data in {file_path}")
218
  except Exception as e:
@@ -220,6 +225,7 @@ class LoadBalancer:
220
 
221
  @staticmethod
222
  def is_valid_url(url):
 
223
  regex = re.compile(
224
  r'^(?:http|ftp)s?://' # http:// or https://
225
  r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
 
12
  from threading import Thread, Event, Timer
13
  from api import InstancesAPI
14
 
15
+
16
  CACHE_DIR = os.getenv("CACHE_DIR")
17
 
18
  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.6 V Beta"
23
  self.instances = []
24
  self.instances_health = {}
25
  self.polling_interval = polling_interval
 
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:
195
  tv_store_data[title] = {}
 
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}.')
206
 
207
  @staticmethod
208
  def clear_json_file(file_path):
 
211
 
212
  :param file_path: Path to the JSON file to be cleared
213
  """
214
+ # Create an empty dictionary to replace the existing data
215
  empty_data = {}
216
 
217
  try:
218
+ # Open the file in write mode
219
  with open(file_path, 'w') as json_file:
220
+ # Write the empty dictionary to the file
221
  json.dump(empty_data, json_file)
222
  print(f"Successfully cleared the data in {file_path}")
223
  except Exception as e:
 
225
 
226
  @staticmethod
227
  def is_valid_url(url):
228
+ # Simple URL validation (could be more complex if needed)
229
  regex = re.compile(
230
  r'^(?:http|ftp)s?://' # http:// or https://
231
  r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...