Spaces:
Paused
Paused
Commit
·
e568f14
1
Parent(s):
61f8970
remove metadata fetching and apis
Browse files- Instance.py +12 -47
- app.py +0 -37
Instance.py
CHANGED
@@ -7,8 +7,6 @@ from threading import Thread
|
|
7 |
from requests.exceptions import RequestException
|
8 |
from tqdm import tqdm
|
9 |
from indexer import indexer
|
10 |
-
import re
|
11 |
-
from tvdb import fetch_and_cache_json
|
12 |
import logging
|
13 |
from threading import Event
|
14 |
|
@@ -56,13 +54,8 @@ class Instance:
|
|
56 |
|
57 |
# Start prefetching metadata and monitoring registration
|
58 |
self.register_to_load_balancer()
|
59 |
-
prefetch_thread = Thread(target=self.start_prefetching)
|
60 |
registration_thread = Thread(target=self.monitor_registration)
|
61 |
-
|
62 |
-
prefetch_thread.daemon = True
|
63 |
registration_thread.daemon = True
|
64 |
-
|
65 |
-
prefetch_thread.start()
|
66 |
registration_thread.start()
|
67 |
|
68 |
def compile_report(self):
|
@@ -342,31 +335,6 @@ class Instance:
|
|
342 |
"""Generate a film ID based on the title."""
|
343 |
return title.replace(" ", "_").lower()
|
344 |
|
345 |
-
def prefetch_metadata(self):
|
346 |
-
"""Prefetch metadata for all items in the file structure."""
|
347 |
-
for item in self.file_structure:
|
348 |
-
if 'contents' in item:
|
349 |
-
for sub_item in item['contents']:
|
350 |
-
original_title = sub_item['path'].split('/')[-1]
|
351 |
-
media_type = 'series' if item['path'].startswith('tv') else 'movie'
|
352 |
-
title = original_title
|
353 |
-
year = None
|
354 |
-
|
355 |
-
# Extract year from the title if available
|
356 |
-
match = re.search(r'\((\d{4})\)', original_title)
|
357 |
-
if match:
|
358 |
-
year_str = match.group(1)
|
359 |
-
if year_str.isdigit() and len(year_str) == 4:
|
360 |
-
title = original_title[:match.start()].strip()
|
361 |
-
year = int(year_str)
|
362 |
-
else:
|
363 |
-
parts = original_title.rsplit(' ', 1)
|
364 |
-
if len(parts) > 1 and parts[-1].isdigit() and len(parts[-1]) == 4:
|
365 |
-
title = parts[0].strip()
|
366 |
-
year = int(parts[-1])
|
367 |
-
|
368 |
-
fetch_and_cache_json(original_title, title, media_type, year)
|
369 |
-
|
370 |
def bytes_to_human_readable(self, num, suffix="B"):
|
371 |
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
|
372 |
if abs(num) < 1024.0:
|
@@ -408,25 +376,22 @@ class Instance:
|
|
408 |
films.append(sub_directory['path'])
|
409 |
return films
|
410 |
|
411 |
-
def start_prefetching(self):
|
412 |
-
"""Start the metadata prefetching in a separate thread."""
|
413 |
-
self.prefetch_metadata()
|
414 |
-
|
415 |
-
|
416 |
def register_to_load_balancer(self):
|
417 |
retries = 0
|
418 |
delay = self.initial_delay
|
|
|
419 |
|
420 |
-
while
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
|
|
|
|
|
|
|
|
426 |
retries += 1
|
427 |
logging.warning(f'Attempt {retries} to register instance {self.id} failed. Retrying in {delay} seconds...')
|
428 |
time.sleep(delay)
|
429 |
-
delay
|
430 |
-
|
431 |
-
logging.error(f'Failed to register instance {self.id} to load balancer after {self.max_retries} attempts.')
|
432 |
-
return None
|
|
|
7 |
from requests.exceptions import RequestException
|
8 |
from tqdm import tqdm
|
9 |
from indexer import indexer
|
|
|
|
|
10 |
import logging
|
11 |
from threading import Event
|
12 |
|
|
|
54 |
|
55 |
# Start prefetching metadata and monitoring registration
|
56 |
self.register_to_load_balancer()
|
|
|
57 |
registration_thread = Thread(target=self.monitor_registration)
|
|
|
|
|
58 |
registration_thread.daemon = True
|
|
|
|
|
59 |
registration_thread.start()
|
60 |
|
61 |
def compile_report(self):
|
|
|
335 |
"""Generate a film ID based on the title."""
|
336 |
return title.replace(" ", "_").lower()
|
337 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
338 |
def bytes_to_human_readable(self, num, suffix="B"):
|
339 |
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
|
340 |
if abs(num) < 1024.0:
|
|
|
376 |
films.append(sub_directory['path'])
|
377 |
return films
|
378 |
|
|
|
|
|
|
|
|
|
|
|
379 |
def register_to_load_balancer(self):
|
380 |
retries = 0
|
381 |
delay = self.initial_delay
|
382 |
+
max_delay = 120
|
383 |
|
384 |
+
while True:
|
385 |
+
try:
|
386 |
+
result = self.load_balancer_api.register_instance(self.id, self.url)
|
387 |
+
if result:
|
388 |
+
logging.info(f'Successfully registered instance {self.id} to load balancer.')
|
389 |
+
return result
|
390 |
+
|
391 |
+
except Exception as e:
|
392 |
+
logging.error(f'Error during registration: {e}')
|
393 |
+
|
394 |
retries += 1
|
395 |
logging.warning(f'Attempt {retries} to register instance {self.id} failed. Retrying in {delay} seconds...')
|
396 |
time.sleep(delay)
|
397 |
+
delay = min(delay * 2, max_delay) # Exponential backoff with maximum delay
|
|
|
|
|
|
app.py
CHANGED
@@ -167,43 +167,6 @@ def get_film_store_api():
|
|
167 |
return jsonify(tv_store_data)
|
168 |
return jsonify({}), 404
|
169 |
|
170 |
-
@app.route('/api/film/metadata/<title>', methods=['GET'])
|
171 |
-
def get_film_metadata_api(title):
|
172 |
-
"""Endpoint to get the film metadata by title."""
|
173 |
-
if not title:
|
174 |
-
return jsonify({'error': 'No title provided'}), 400
|
175 |
-
|
176 |
-
json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.json")
|
177 |
-
|
178 |
-
if os.path.exists(json_cache_path):
|
179 |
-
with open(json_cache_path, 'r') as f:
|
180 |
-
data = json.load(f)
|
181 |
-
return jsonify(data)
|
182 |
-
|
183 |
-
return jsonify({'error': 'Metadata not found'}), 404
|
184 |
-
|
185 |
-
@app.route('/api/tv/metadata/<title>', methods=['GET'])
|
186 |
-
def get_tv_metadata_api(title):
|
187 |
-
"""Endpoint to get the TV show metadata by title."""
|
188 |
-
if not title:
|
189 |
-
return jsonify({'error': 'No title provided'}), 400
|
190 |
-
|
191 |
-
json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.json")
|
192 |
-
|
193 |
-
if os.path.exists(json_cache_path):
|
194 |
-
with open(json_cache_path, 'r') as f:
|
195 |
-
data = json.load(f)
|
196 |
-
|
197 |
-
# Add the file structure to the metadata
|
198 |
-
tv_structure_data = instance.get_tv_structure(title)
|
199 |
-
if tv_structure_data:
|
200 |
-
data['file_structure'] = tv_structure_data
|
201 |
-
|
202 |
-
return jsonify(data)
|
203 |
-
|
204 |
-
return jsonify({'error': 'Metadata not found'}), 404
|
205 |
-
|
206 |
-
|
207 |
@app.route("/api/film/all")
|
208 |
def get_all_films_api():
|
209 |
return instance.get_all_films()
|
|
|
167 |
return jsonify(tv_store_data)
|
168 |
return jsonify({}), 404
|
169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
@app.route("/api/film/all")
|
171 |
def get_all_films_api():
|
172 |
return instance.get_all_films()
|