ChandimaPrabath commited on
Commit
703d0ec
·
1 Parent(s): 0102c69

0.0.2.5 V Beta fix

Browse files
Files changed (1) hide show
  1. LoadBalancer.py +33 -44
LoadBalancer.py CHANGED
@@ -12,14 +12,13 @@ import logging
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.4 V Beta"
23
  self.instances = []
24
  self.instances_health = {}
25
  self.polling_interval = polling_interval
@@ -33,13 +32,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,36 +94,40 @@ 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.")
118
- while not self.stop_event.is_set():
119
- self.get_reports()
120
- time.sleep(self.polling_interval)
121
- logging.info("Polling stopped.")
 
 
 
 
122
 
123
- def stop_polling(self):
124
- logging.info("Stopping polling.")
125
- self.stop_event.set()
126
 
127
  ######################################################################
128
 
@@ -152,37 +157,26 @@ 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:
188
  tv_store_data[title] = {}
@@ -192,10 +186,10 @@ 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}.')
199
 
200
  @staticmethod
201
  def clear_json_file(file_path):
@@ -204,22 +198,17 @@ class LoadBalancer:
204
 
205
  :param file_path: Path to the JSON file to be cleared
206
  """
207
- # Create an empty dictionary to replace the existing data
208
  empty_data = {}
209
 
210
  try:
211
- # Open the file in write mode
212
  with open(file_path, 'w') as json_file:
213
- # Write the empty dictionary to the file
214
  json.dump(empty_data, json_file)
215
  print(f"Successfully cleared the data in {file_path}")
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)
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
  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
 
32
  self.REPO = repo
33
  self.FILM_STORE_JSON_PATH = os.path.join(cache_dir, "film_store.json")
34
  self.TV_STORE_JSON_PATH = os.path.join(cache_dir, "tv_store.json")
35
+ self.TEMP_FILM_STORE_JSON_PATH = os.path.join(cache_dir, "temp_film_store.json")
36
+ self.TEMP_TV_STORE_JSON_PATH = os.path.join(cache_dir, "temp_tv_store.json")
37
  self.file_structure = None
38
 
39
  # Ensure CACHE_DIR exists
40
  if not os.path.exists(self.CACHE_DIR):
41
  os.makedirs(self.CACHE_DIR)
42
 
43
+ 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]:
44
  if not os.path.exists(path):
45
  with open(path, 'w') as json_file:
46
  json.dump({}, json_file)
 
94
  cache_size = report.get('cache_size')
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
  ######################################################################
133
 
 
157
  print(f"Error getting system proxies: {e}")
158
  return {}
159
 
160
+ def update_temp_film_store_json(self, title, url):
161
  """
162
+ Updates the temporary film store JSON with the new file.
163
 
164
  Args:
165
  title (str): The title of the film.
166
  url (str): The url.
167
  """
168
+ film_store_data = self.read_json(self.TEMP_FILM_STORE_JSON_PATH)
 
 
 
 
 
 
169
  film_store_data[title] = url
170
 
171
+ with open(self.TEMP_FILM_STORE_JSON_PATH, 'w') as json_file:
172
  json.dump(film_store_data, json_file, indent=2)
173
+ print(f'Temporary film store updated with {title}.')
174
 
175
+ def update_temp_tv_store_json(self, title, season, episode, url):
176
  """
177
+ Updates the temporary TV store JSON with the new file, organizing by title, season, and episode.
178
  """
179
+ tv_store_data = self.read_json(self.TEMP_TV_STORE_JSON_PATH)
 
 
 
 
 
180
 
181
  if title not in tv_store_data:
182
  tv_store_data[title] = {}
 
186
 
187
  tv_store_data[title][season][episode] = url
188
 
189
+ with open(self.TEMP_TV_STORE_JSON_PATH, 'w') as json_file:
190
  json.dump(tv_store_data, json_file, indent=2)
191
 
192
+ print(f'Temporary TV store updated with {title}, {season}, {episode}.')
193
 
194
  @staticmethod
195
  def clear_json_file(file_path):
 
198
 
199
  :param file_path: Path to the JSON file to be cleared
200
  """
 
201
  empty_data = {}
202
 
203
  try:
 
204
  with open(file_path, 'w') as json_file:
 
205
  json.dump(empty_data, json_file)
206
  print(f"Successfully cleared the data in {file_path}")
207
  except Exception as e:
208
  print(f"An error occurred while clearing the data in {file_path}: {e}")
209
 
 
210
  @staticmethod
211
  def is_valid_url(url):
 
212
  regex = re.compile(
213
  r'^(?:http|ftp)s?://' # http:// or https://
214
  r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...