Commit
·
815f274
1
Parent(s):
39c5a48
0.0.2.6 V Beta Patch
Browse files- LoadBalancer.py +5 -99
- TODO.md +1 -0
- app.py +9 -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=4, 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
|
@@ -31,19 +31,14 @@ class LoadBalancer:
|
|
31 |
self.INDEX_FILE = index_file
|
32 |
self.TOKEN = token
|
33 |
self.REPO = repo
|
34 |
-
self.
|
35 |
-
self.
|
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)
|
46 |
-
|
47 |
# Index the file structure
|
48 |
indexer()
|
49 |
|
@@ -58,7 +53,6 @@ class LoadBalancer:
|
|
58 |
prefetch_thread.daemon = True
|
59 |
prefetch_thread.start()
|
60 |
|
61 |
-
|
62 |
def register_instance(self, instance_url):
|
63 |
if instance_url not in self.instances:
|
64 |
self.instances.append(instance_url)
|
@@ -70,8 +64,6 @@ class LoadBalancer:
|
|
70 |
if instance_url in self.instances:
|
71 |
self.instances.remove(instance_url)
|
72 |
self.instances_health.pop(instance_url, None)
|
73 |
-
self.clear_json_file(self.FILM_STORE_JSON_PATH)
|
74 |
-
self.clear_json_file(self.TV_STORE_JSON_PATH)
|
75 |
logging.info(f"Removed instance {instance_url}")
|
76 |
else:
|
77 |
logging.info(f"Instance {instance_url} not found for removal.")
|
@@ -92,10 +84,8 @@ class LoadBalancer:
|
|
92 |
logging.error(f"Failed to get report from {instance_url}. Removing instance.")
|
93 |
self.remove_instance(instance_url)
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
executor.submit(self.update_film_store_json, temp_film_store)
|
98 |
-
executor.submit(self.update_tv_store_json, temp_tv_store)
|
99 |
|
100 |
def process_report(self, instance_url, report, temp_film_store, temp_tv_store):
|
101 |
film_store = report.get('film_store', {})
|
@@ -134,65 +124,7 @@ class LoadBalancer:
|
|
134 |
logging.info("Stopping polling.")
|
135 |
self.stop_event.set()
|
136 |
|
137 |
-
def update_film_store_json(self, temp_film_store, ):
|
138 |
-
# Load current data
|
139 |
-
film_store_data = self.read_json(self.FILM_STORE_JSON_PATH)
|
140 |
-
|
141 |
-
|
142 |
-
# Compare and update film store
|
143 |
-
for title in list(film_store_data.keys()):
|
144 |
-
if title not in temp_film_store:
|
145 |
-
del film_store_data[title]
|
146 |
-
|
147 |
-
for title, url in temp_film_store.items():
|
148 |
-
film_store_data[title] = url
|
149 |
-
|
150 |
-
with open(self.FILM_STORE_JSON_PATH, 'w') as json_file:
|
151 |
-
json.dump(film_store_data, json_file, indent=2)
|
152 |
-
|
153 |
-
print(f'Film store updated.')
|
154 |
-
|
155 |
-
def update_tv_store_json(self, temp_tv_store):
|
156 |
-
"""
|
157 |
-
Updates the TV store JSON with the new file, organizing by title, season, and episode.
|
158 |
-
"""
|
159 |
-
tv_store_data = self.read_json(self.TV_STORE_JSON_PATH)
|
160 |
-
# Compare and update TV store
|
161 |
-
for title in list(tv_store_data.keys()):
|
162 |
-
if title not in temp_tv_store:
|
163 |
-
del tv_store_data[title]
|
164 |
-
else:
|
165 |
-
for season in list(tv_store_data[title].keys()):
|
166 |
-
if season not in temp_tv_store[title]:
|
167 |
-
del tv_store_data[title][season]
|
168 |
-
else:
|
169 |
-
for episode in list(tv_store_data[title][season].keys()):
|
170 |
-
if episode not in temp_tv_store[title][season]:
|
171 |
-
del tv_store_data[title][season][episode]
|
172 |
-
|
173 |
-
for title, seasons in temp_tv_store.items():
|
174 |
-
if title not in tv_store_data:
|
175 |
-
tv_store_data[title] = {}
|
176 |
-
for season, episodes in seasons.items():
|
177 |
-
if season not in tv_store_data[title]:
|
178 |
-
tv_store_data[title][season] = {}
|
179 |
-
for episode, url in episodes.items():
|
180 |
-
tv_store_data[title][season][episode] = url
|
181 |
-
|
182 |
-
with open(self.TV_STORE_JSON_PATH, 'w') as json_file:
|
183 |
-
json.dump(tv_store_data, json_file, indent=2)
|
184 |
-
|
185 |
-
print(f'TV store updated.')
|
186 |
-
|
187 |
######################################################################
|
188 |
-
|
189 |
-
@staticmethod
|
190 |
-
def read_json(file_path):
|
191 |
-
if os.path.exists(file_path):
|
192 |
-
with open(file_path, 'r') as json_file:
|
193 |
-
return json.load(json_file)
|
194 |
-
return {}
|
195 |
-
|
196 |
@staticmethod
|
197 |
def get_system_proxies():
|
198 |
"""
|
@@ -212,27 +144,6 @@ class LoadBalancer:
|
|
212 |
print(f"Error getting system proxies: {e}")
|
213 |
return {}
|
214 |
|
215 |
-
|
216 |
-
@staticmethod
|
217 |
-
def clear_json_file(file_path):
|
218 |
-
"""
|
219 |
-
Clears the data in a JSON file by replacing its contents with an empty JSON object.
|
220 |
-
|
221 |
-
:param file_path: Path to the JSON file to be cleared
|
222 |
-
"""
|
223 |
-
# Create an empty dictionary to replace the existing data
|
224 |
-
empty_data = {}
|
225 |
-
|
226 |
-
try:
|
227 |
-
# Open the file in write mode
|
228 |
-
with open(file_path, 'w') as json_file:
|
229 |
-
# Write the empty dictionary to the file
|
230 |
-
json.dump(empty_data, json_file)
|
231 |
-
print(f"Successfully cleared the data in {file_path}")
|
232 |
-
except Exception as e:
|
233 |
-
print(f"An error occurred while clearing the data in {file_path}: {e}")
|
234 |
-
|
235 |
-
|
236 |
@staticmethod
|
237 |
def is_valid_url(url):
|
238 |
# Simple URL validation (could be more complex if needed)
|
@@ -347,11 +258,6 @@ class LoadBalancer:
|
|
347 |
return float(space_str.split()[0])
|
348 |
|
349 |
#################################################################
|
350 |
-
def load_json(self, file_path):
|
351 |
-
"""Load JSON data from a file."""
|
352 |
-
with open(file_path, 'r') as file:
|
353 |
-
return json.load(file)
|
354 |
-
|
355 |
def find_movie_path(self, title):
|
356 |
"""Find the path of the movie in the JSON data based on the title."""
|
357 |
for directory in self.file_structure:
|
|
|
19 |
|
20 |
class LoadBalancer:
|
21 |
def __init__(self, cache_dir, index_file, token, repo, polling_interval=4, 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
|
|
|
31 |
self.INDEX_FILE = index_file
|
32 |
self.TOKEN = token
|
33 |
self.REPO = repo
|
34 |
+
self.FILM_STORE = {}
|
35 |
+
self.TV_STORE = {}
|
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 |
# Index the file structure
|
43 |
indexer()
|
44 |
|
|
|
53 |
prefetch_thread.daemon = True
|
54 |
prefetch_thread.start()
|
55 |
|
|
|
56 |
def register_instance(self, instance_url):
|
57 |
if instance_url not in self.instances:
|
58 |
self.instances.append(instance_url)
|
|
|
64 |
if instance_url in self.instances:
|
65 |
self.instances.remove(instance_url)
|
66 |
self.instances_health.pop(instance_url, None)
|
|
|
|
|
67 |
logging.info(f"Removed instance {instance_url}")
|
68 |
else:
|
69 |
logging.info(f"Instance {instance_url} not found for removal.")
|
|
|
84 |
logging.error(f"Failed to get report from {instance_url}. Removing instance.")
|
85 |
self.remove_instance(instance_url)
|
86 |
|
87 |
+
self.FILM_STORE = temp_film_store
|
88 |
+
self.TV_STORE = temp_tv_store
|
|
|
|
|
89 |
|
90 |
def process_report(self, instance_url, report, temp_film_store, temp_tv_store):
|
91 |
film_store = report.get('film_store', {})
|
|
|
124 |
logging.info("Stopping polling.")
|
125 |
self.stop_event.set()
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
######################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
@staticmethod
|
129 |
def get_system_proxies():
|
130 |
"""
|
|
|
144 |
print(f"Error getting system proxies: {e}")
|
145 |
return {}
|
146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
@staticmethod
|
148 |
def is_valid_url(url):
|
149 |
# Simple URL validation (could be more complex if needed)
|
|
|
258 |
return float(space_str.split()[0])
|
259 |
|
260 |
#################################################################
|
|
|
|
|
|
|
|
|
|
|
261 |
def find_movie_path(self, title):
|
262 |
"""Find the path of the movie in the JSON data based on the title."""
|
263 |
for directory in self.file_structure:
|
TODO.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# implement a better way to collect store jsons
|
app.py
CHANGED
@@ -31,13 +31,9 @@ def get_movie_api(title):
|
|
31 |
if not title:
|
32 |
return jsonify({"error": "Title parameter is required"}), 400
|
33 |
|
34 |
-
# Load the film store JSON
|
35 |
-
with open(load_balancer.FILM_STORE_JSON_PATH, 'r') as json_file:
|
36 |
-
film_store_data = json.load(json_file)
|
37 |
-
|
38 |
# Check if the film is already cached
|
39 |
-
if title in
|
40 |
-
url =
|
41 |
return jsonify({"url":url})
|
42 |
|
43 |
movie_path = load_balancer.find_movie_path(title)
|
@@ -56,15 +52,11 @@ def get_tv_show_api(title, season, episode):
|
|
56 |
if not title or not season or not episode:
|
57 |
return jsonify({"error": "Title, season, and episode parameters are required"}), 400
|
58 |
|
59 |
-
# Load the TV store JSON
|
60 |
-
with open(load_balancer.TV_STORE_JSON_PATH, 'r') as json_file:
|
61 |
-
tv_store_data = json.load(json_file)
|
62 |
-
|
63 |
# Check if the episode is already cached
|
64 |
-
if title in
|
65 |
-
for ep in
|
66 |
if episode in ep:
|
67 |
-
url =
|
68 |
return jsonify({"url":url})
|
69 |
|
70 |
tv_path = load_balancer.find_tv_path(title)
|
@@ -129,20 +121,14 @@ def clear_cache_api():
|
|
129 |
@app.route('/api/tv/store', methods=['GET'])
|
130 |
def get_tv_store_api():
|
131 |
"""Endpoint to get the TV store JSON."""
|
132 |
-
|
133 |
-
|
134 |
-
tv_store_data = json.load(json_file)
|
135 |
-
return jsonify(tv_store_data)
|
136 |
-
return jsonify({}), 404
|
137 |
|
138 |
@app.route('/api/film/store', methods=['GET'])
|
139 |
def get_film_store_api():
|
140 |
"""Endpoint to get the film store JSON."""
|
141 |
-
|
142 |
-
|
143 |
-
tv_store_data = json.load(json_file)
|
144 |
-
return jsonify(tv_store_data)
|
145 |
-
return jsonify({}), 404
|
146 |
|
147 |
@app.route('/api/film/metadata/<title>', methods=['GET'])
|
148 |
def get_film_metadata_api(title):
|
|
|
31 |
if not title:
|
32 |
return jsonify({"error": "Title parameter is required"}), 400
|
33 |
|
|
|
|
|
|
|
|
|
34 |
# Check if the film is already cached
|
35 |
+
if title in load_balancer.FILM_STORE:
|
36 |
+
url = load_balancer.FILM_STORE[title]
|
37 |
return jsonify({"url":url})
|
38 |
|
39 |
movie_path = load_balancer.find_movie_path(title)
|
|
|
52 |
if not title or not season or not episode:
|
53 |
return jsonify({"error": "Title, season, and episode parameters are required"}), 400
|
54 |
|
|
|
|
|
|
|
|
|
55 |
# Check if the episode is already cached
|
56 |
+
if title in load_balancer.TV_STORE and season in load_balancer.TV_STORE[title]:
|
57 |
+
for ep in load_balancer.TV_STORE[title][season]:
|
58 |
if episode in ep:
|
59 |
+
url = load_balancer.TV_STORE[title][season][ep]
|
60 |
return jsonify({"url":url})
|
61 |
|
62 |
tv_path = load_balancer.find_tv_path(title)
|
|
|
121 |
@app.route('/api/tv/store', methods=['GET'])
|
122 |
def get_tv_store_api():
|
123 |
"""Endpoint to get the TV store JSON."""
|
124 |
+
return jsonify(load_balancer.TV_STORE)
|
125 |
+
|
|
|
|
|
|
|
126 |
|
127 |
@app.route('/api/film/store', methods=['GET'])
|
128 |
def get_film_store_api():
|
129 |
"""Endpoint to get the film store JSON."""
|
130 |
+
return jsonify(load_balancer.FILM_STORE)
|
131 |
+
|
|
|
|
|
|
|
132 |
|
133 |
@app.route('/api/film/metadata/<title>', methods=['GET'])
|
134 |
def get_film_metadata_api(title):
|