Commit ·
2e81e75
1
Parent(s): 15d97c1
init
Browse filesFiles changed (9)
- .gitignoreAdded+12
- Instance.pyAdded+403
- README.md+1-1
- api.pyAdded+22
- app.py+16-2
- hf_scrapper.pyAdded+249
- indexer.pyAdded+32
- old.app.pyAdded+174
- requirements.txt+4
123456789101112#.env.env# cachetmp# pycache__pycache__# stream-test.pystream-test.py#testtest.py# README.mdREADME.md123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403import osimport requestsimport jsonimport urllib.requestimport timefrom threading import Thread, Eventfrom requests.exceptions import RequestExceptionfrom tqdm import tqdmfrom indexer import indexerimport logging
CACHE_DIR = os.getenv("CACHE_DIR")
download_progress = {}
class Instance: def __init__(self, id, url, cache_dir, index_file, token, repo, load_balancer_api, max_retries=20, initial_delay=1): self.version = "0.0.0.1 Alpha" self.id = id self.url = url self.CACHE_DIR = cache_dir self.INDEX_FILE = index_file self.TOKEN = token self.REPO = repo self.FILM_STORE_JSON_PATH = os.path.join(cache_dir, "film_store.json") self.TV_STORE_JSON_PATH = os.path.join(cache_dir, "tv_store.json") self.download_threads = {} self.file_structure = None self.load_balancer_api = load_balancer_api self.max_retries = max_retries self.initial_delay = initial_delay self.last_report_time = time.time() # Initialize the last report time self.re_register_event = Event()
# Ensure CACHE_DIR exists if not os.path.exists(self.CACHE_DIR): os.makedirs(self.CACHE_DIR)
for path in [self.FILM_STORE_JSON_PATH, self.TV_STORE_JSON_PATH]: if not os.path.exists(path): with open(path, 'w') as json_file: json.dump({}, json_file)
# Index the file structure and load it self.run_indexer_and_load()
# Start prefetching metadata and monitoring registration self.register_to_load_balancer() registration_thread = Thread(target=self.monitor_registration) registration_thread.daemon = True registration_thread.start()
# Start the thread to re-index every 2 minutes indexer_thread = Thread(target=self.run_indexer_periodically) indexer_thread.daemon = True indexer_thread.start()
def run_indexer_and_load(self): """Runs the indexer and loads the file structure from INDEX_FILE.""" indexer() if not os.path.exists(self.INDEX_FILE): raise FileNotFoundError(f"{self.INDEX_FILE} not found. Please make sure the file exists.")
with open(self.INDEX_FILE, 'r') as f: self.file_structure = json.load(f) logging.info("File structure reloaded successfully.")
def run_indexer_periodically(self): """Periodically reruns the indexer and reloads the file structure.""" while True: time.sleep(120) # Wait for 2 minutes logging.info("Re-running indexer and reloading file structure.") self.run_indexer_and_load()
def compile_report(self): self.last_report_time = time.time() # Update the last report time film_store_path = os.path.join(self.CACHE_DIR, "film_store.json") tv_store_path = os.path.join(self.CACHE_DIR, "tv_store.json") cache_size = self.get_cache_size()
report = { "instance_id": self.id, "instance_url": self.url, "film_store": self.read_json(film_store_path), "tv_store": self.read_json(tv_store_path), "cache_size": cache_size } return report
def register_to_load_balancer(self): result = self.load_balancer_api.register_instance(self.id, self.url) if result is not None: logging.info(f'Registered instance {self.id} to load balancer.') else: logging.error(f'Failed to register instance {self.id} to load balancer.')
def monitor_registration(self): while True: if time.time() - self.last_report_time > 60: # Check if 1 minute has passed logging.info('1 minute passed since last report. Re-registering...') self.register_to_load_balancer() self.last_report_time = time.time() # Reset the last report time time.sleep(30) # Check every 30 seconds
def get_cache_size(self): total_size = 0 for dirpath, dirnames, filenames in os.walk(CACHE_DIR): for f in filenames: fp = os.path.join(dirpath, f) total_size += os.path.getsize(fp) return {"cache_size": f"{total_size / (1024 * 1024 * 1024):.2f} GB"}
@staticmethod def read_json(file_path): if os.path.exists(file_path): with open(file_path, 'r') as json_file: return json.load(json_file) return {}
@staticmethod def get_system_proxies(): """ Retrieves the system's HTTP and HTTPS proxies.
Returns: dict: A dictionary containing the proxies. """ try: proxies = urllib.request.getproxies() print("System proxies:", proxies) return { "http": proxies.get("http"), "https": proxies.get("http") } except Exception as e: print(f"Error getting system proxies: {e}") return {}
def download_film(self, file_url, token, cache_path, proxies, film_id, title, chunk_size=100 * 1024 * 1024): """ Downloads a file from the specified URL and saves it to the cache path. Tracks the download progress.
Args: file_url (str): The URL of the file to download. token (str): The authorization token for the request. cache_path (str): The path to save the downloaded file. proxies (dict): Proxies for the request. film_id (str): Unique identifier for the film download. title (str): The title of the film. chunk_size (int): Size of each chunk to download. """ print(f"Downloading file from URL: {file_url} to {cache_path} with proxies: {proxies}") headers = {'Authorization': f'Bearer {token}'} try: response = requests.get(file_url, headers=headers, proxies=proxies, stream=True) response.raise_for_status() total_size = int(response.headers.get('content-length', 0)) download_progress[film_id] = {"total": total_size, "downloaded": 0, "status": "Downloading", "start_time": time.time()}
os.makedirs(os.path.dirname(cache_path), exist_ok=True) with open(cache_path, 'wb') as file, tqdm(total=total_size, unit='B', unit_scale=True, desc=cache_path) as pbar: for data in response.iter_content(chunk_size=chunk_size): file.write(data) pbar.update(len(data)) download_progress[film_id]["downloaded"] += len(data) print(f'File cached to {cache_path} successfully.') self.update_film_store_json(title, cache_path) download_progress[film_id]["status"] = "Completed" except RequestException as e: print(f"Error downloading file: {e}") download_progress[film_id]["status"] = "Failed" except IOError as e: print(f"Error writing file {cache_path}: {e}") download_progress[film_id]["status"] = "Failed" finally: if download_progress[film_id]["status"] != "Downloading": download_progress[film_id]["end_time"] = time.time()
@staticmethod def get_download_progress(id): """ Gets the download progress for a specific film.
Args: film_id (str): The unique identifier for the film download.
Returns: dict: A dictionary containing the total size, downloaded size, progress percentage, status, and ETA. """ if id in download_progress: total = download_progress[id]["total"] downloaded = download_progress[id]["downloaded"] status = download_progress[id].get("status", "In Progress") progress = (downloaded / total) * 100 if total > 0 else 0
eta = None if status == "Downloading" and downloaded > 0: elapsed_time = time.time() - download_progress[id]["start_time"] estimated_total_time = elapsed_time * (total / downloaded) eta = estimated_total_time - elapsed_time elif status == "Completed": eta = 0
return {"total": total, "downloaded": downloaded, "progress": progress, "status": status, "eta": eta} return {"total": 0, "downloaded": 0, "progress": 0, "status": "Not Found", "eta": None}
def update_film_store_json(self,title, cache_path): """ Updates the film store JSON with the new file.
Args: title (str): The title of the film. cache_path (str): The local path where the file is saved. """ film_store_data = {} if os.path.exists(self.FILM_STORE_JSON_PATH): with open(self.FILM_STORE_JSON_PATH, 'r') as json_file: film_store_data = json.load(json_file)
film_store_data[title] = cache_path
with open(self.FILM_STORE_JSON_PATH, 'w') as json_file: json.dump(film_store_data, json_file, indent=2) print(f'Film store updated with {title}.')
def download_episode(self, file_url, token, cache_path, proxies, episode_id, title, chunk_size=100 * 1024 * 1024): """ Downloads a file from the specified URL and saves it to the cache path. Tracks the download progress.
Args: file_url (str): The URL of the file to download. token (str): The authorization token for the request. cache_path (str): The path to save the downloaded file. proxies (dict): Proxies for the request. episode_id (str): Unique identifier for the film download. title (str): The title of the film. chunk_size (int): Size of each chunk to download. """ print(f"Downloading file from URL: {file_url} to {cache_path} with proxies: {proxies}") headers = {'Authorization': f'Bearer {token}'} try: response = requests.get(file_url, headers=headers, proxies=proxies, stream=True) response.raise_for_status() total_size = int(response.headers.get('content-length', 0)) download_progress[episode_id] = {"total": total_size, "downloaded": 0, "status": "Downloading", "start_time": time.time()}
os.makedirs(os.path.dirname(cache_path), exist_ok=True) with open(cache_path, 'wb') as file, tqdm(total=total_size, unit='B', unit_scale=True, desc=cache_path) as pbar: for data in response.iter_content(chunk_size=chunk_size): file.write(data) pbar.update(len(data)) download_progress[episode_id]["downloaded"] += len(data) print(f'File cached to {cache_path} successfully.') self.update_tv_store_json(title, cache_path) download_progress[episode_id]["status"] = "Completed" except RequestException as e: print(f"Error downloading file: {e}") download_progress[episode_id]["status"] = "Failed" except IOError as e: print(f"Error writing file {cache_path}: {e}") download_progress[episode_id]["status"] = "Failed" finally: if download_progress[episode_id]["status"] != "Downloading": download_progress[episode_id]["end_time"] = time.time()
def update_tv_store_json(self, title, cache_path): """ Updates the TV store JSON with the new file, organizing by title, season, and episode.
Args: title (str): The title of the TV show. cache_path (str): The local path where the file is saved. """ tv_store_data = {} if os.path.exists(self.TV_STORE_JSON_PATH): with open(self.TV_STORE_JSON_PATH, 'r') as json_file: tv_store_data = json.load(json_file)
# Extract season and episode information from the cache_path season_part = os.path.basename(os.path.dirname(cache_path)) # Extracts 'Season 1' episode_part = os.path.basename(cache_path) # Extracts 'Grand Blue Dreaming - S01E01 - Deep Blue HDTV-720p.mp4'
# Create the structure if not already present if title not in tv_store_data: tv_store_data[title] = {} if season_part not in tv_store_data[title]: tv_store_data[title][season_part] = {}
# Assuming episode_part is unique for each episode within a season tv_store_data[title][season_part][episode_part] = cache_path
with open(self.TV_STORE_JSON_PATH, 'w') as json_file: json.dump(tv_store_data, json_file, indent=2)
print(f'TV store updated with {title}, {season_part}, {episode_part}.')
def load_json(self, file_path): """Load JSON data from a file.""" with open(file_path, 'r') as file: return json.load(file)
def find_movie_path(self, title): """Find the path of the movie in the JSON data based on the title.""" for directory in self.file_structure: if directory['type'] == 'directory' and directory['path'] == 'films': for sub_directory in directory['contents']: if sub_directory['type'] == 'directory': for item in sub_directory['contents']: if item['type'] == 'file' and title.lower() in item['path'].lower(): return item['path'] return None
def find_tv_path(self, title): """Find the path of the TV show in the JSON data based on the title.""" for directory in self.file_structure: if directory['type'] == 'directory' and directory['path'] == 'tv': for sub_directory in directory['contents']: if sub_directory['type'] == 'directory' and title.lower() in sub_directory['path'].lower(): return sub_directory['path'] return None
def get_tv_structure(self, title): """Find the path of the TV show in the JSON data based on the title.""" for directory in self.file_structure: if directory['type'] == 'directory' and directory['path'] == 'tv': for sub_directory in directory['contents']: if sub_directory['type'] == 'directory' and title.lower() in sub_directory['path'].lower(): return sub_directory return None
def get_film_id(self, title): """Generate a film ID based on the title.""" return title.replace(" ", "_").lower()
def bytes_to_human_readable(self, num, suffix="B"): for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]: if abs(num) < 1024.0: return f"{num:3.1f} {unit}{suffix}" num /= 1024.0 return f"{num:.1f} Y{suffix}"
def encode_episodeid(self, title, season, episode): return f"{title}_{season}_{episode}"
def get_all_tv_shows(self): """Get all TV shows from the indexed cache structure JSON file.""" tv_shows = {} for directory in self.file_structure: if directory['type'] == 'directory' and directory['path'] == 'tv': for sub_directory in directory['contents']: if sub_directory['type'] == 'directory': show_title = sub_directory['path'].split('/')[-1] tv_shows[show_title] = [] for season_directory in sub_directory['contents']: if season_directory['type'] == 'directory': season = season_directory['path'].split('/')[-1] for episode in season_directory['contents']: if episode['type'] == 'file': tv_shows[show_title].append({ "season": season, "episode": episode['path'].split('/')[-1], "path": episode['path'] }) return tv_shows
def get_all_films(self): """Get all films from the indexed cache structure JSON file.""" films = [] for directory in self.file_structure: if directory['type'] == 'directory' and directory['path'] == 'films': for sub_directory in directory['contents']: if sub_directory['type'] == 'directory': films.append(sub_directory['path']) return films
def register_to_load_balancer(self): retries = 0 delay = self.initial_delay max_delay = 120
while True: try: result = self.load_balancer_api.register_instance(self.id, self.url) if result: logging.info(f'Successfully registered instance {self.id} to load balancer.') return result
except Exception as e: logging.error(f'Error during registration: {e}') retries += 1 logging.warning(f'Attempt {retries} to register instance {self.id} failed. Retrying in {delay} seconds...') time.sleep(delay) delay = min(delay * 2, max_delay) # Exponential backoff with maximum delay12345---title: Load Balanceremoji: 👀colorFrom: pinkcolorTo: red12345---title: Instance1emoji: 👀colorFrom: pinkcolorTo: red
12345678910111213141516171819202122import requestsimport loggingimport json
class LoadBalancerAPI: def __init__(self, base_url): self.base_url = base_url
def register_instance(self, instance_id, instance_url): data = { "url": instance_url } api_endpoint = f'{self.base_url}/api/post/register' try: headers = {'Content-Type': 'application/json'} response = requests.post(api_endpoint, data=json.dumps(data), headers=headers) response.raise_for_status() return response.json() # Assuming the API returns JSON except requests.exceptions.RequestException as e: logging.error(f'Failed to register instance {instance_id} to load balancer: {e}') return None1234567from fastapi import FastAPIapp = FastAPI()@app.get("/")def greet_json():return {"Hello": "World!"}123456789101112131415161718192021from fastapi import FastAPIfrom Instance import Instancefrom api import LoadBalancerAPIimport os# Constants and ConfigurationCACHE_DIR = os.getenv("CACHE_DIR")INDEX_FILE = os.getenv("INDEX_FILE")TOKEN = os.getenv("TOKEN")REPO = os.getenv("REPO")ID = os.getenv("ID")URL = os.getenv("URL")LOAD_BALANCER_URL = os.getenv("LOAD_BALANCER_URL")load_balancer_api = LoadBalancerAPI(base_url=LOAD_BALANCER_URL)instance = Instance(id=ID, url=URL, cache_dir=CACHE_DIR, index_file=INDEX_FILE, token=TOKEN, repo=REPO, load_balancer_api=load_balancer_api)app = FastAPI()@app.get("/")async def index():return instance.version
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249import osimport requestsimport jsonimport urllib.requestimport timefrom requests.exceptions import RequestExceptionfrom tqdm import tqdm
CACHE_DIR = os.getenv("CACHE_DIR")CACHE_JSON_PATH = os.path.join(CACHE_DIR, "cached_films.json")
download_progress = {}
def get_system_proxies(): """ Retrieves the system's HTTP and HTTPS proxies.
Returns: dict: A dictionary containing the proxies. """ try: proxies = urllib.request.getproxies() print("System proxies:", proxies) return { "http": proxies.get("http"), "https": proxies.get("http") } except Exception as e: print(f"Error getting system proxies: {e}") return {}
def download_film(file_url, token, cache_path, proxies, film_id, title, chunk_size=100 * 1024 * 1024): """ Downloads a file from the specified URL and saves it to the cache path. Tracks the download progress.
Args: file_url (str): The URL of the file to download. token (str): The authorization token for the request. cache_path (str): The path to save the downloaded file. proxies (dict): Proxies for the request. film_id (str): Unique identifier for the film download. title (str): The title of the film. chunk_size (int): Size of each chunk to download. """ print(f"Downloading file from URL: {file_url} to {cache_path} with proxies: {proxies}") headers = {'Authorization': f'Bearer {token}'} try: response = requests.get(file_url, headers=headers, proxies=proxies, stream=True) response.raise_for_status() total_size = int(response.headers.get('content-length', 0)) download_progress[film_id] = {"total": total_size, "downloaded": 0, "status": "Downloading", "start_time": time.time()}
os.makedirs(os.path.dirname(cache_path), exist_ok=True) with open(cache_path, 'wb') as file, tqdm(total=total_size, unit='B', unit_scale=True, desc=cache_path) as pbar: for data in response.iter_content(chunk_size=chunk_size): file.write(data) pbar.update(len(data)) download_progress[film_id]["downloaded"] += len(data) print(f'File cached to {cache_path} successfully.') update_film_store_json(title, cache_path) download_progress[film_id]["status"] = "Completed" except RequestException as e: print(f"Error downloading file: {e}") download_progress[film_id]["status"] = "Failed" except IOError as e: print(f"Error writing file {cache_path}: {e}") download_progress[film_id]["status"] = "Failed" finally: if download_progress[film_id]["status"] != "Downloading": download_progress[film_id]["end_time"] = time.time()
def get_download_progress(id): """ Gets the download progress for a specific film.
Args: film_id (str): The unique identifier for the film download.
Returns: dict: A dictionary containing the total size, downloaded size, progress percentage, status, and ETA. """ if id in download_progress: total = download_progress[id]["total"] downloaded = download_progress[id]["downloaded"] status = download_progress[id].get("status", "In Progress") progress = (downloaded / total) * 100 if total > 0 else 0
eta = None if status == "Downloading" and downloaded > 0: elapsed_time = time.time() - download_progress[id]["start_time"] estimated_total_time = elapsed_time * (total / downloaded) eta = estimated_total_time - elapsed_time elif status == "Completed": eta = 0
return {"total": total, "downloaded": downloaded, "progress": progress, "status": status, "eta": eta} return {"total": 0, "downloaded": 0, "progress": 0, "status": "Not Found", "eta": None}
def update_film_store_json(title, cache_path): """ Updates the film store JSON with the new file.
Args: title (str): The title of the film. cache_path (str): The local path where the file is saved. """ FILM_STORE_JSON_PATH = os.path.join(CACHE_DIR, "film_store.json")
film_store_data = {} if os.path.exists(FILM_STORE_JSON_PATH): with open(FILM_STORE_JSON_PATH, 'r') as json_file: film_store_data = json.load(json_file)
film_store_data[title] = cache_path
with open(FILM_STORE_JSON_PATH, 'w') as json_file: json.dump(film_store_data, json_file, indent=2) print(f'Film store updated with {title}.')
###############################################################################def download_episode(file_url, token, cache_path, proxies, episode_id, title, chunk_size=100 * 1024 * 1024): """ Downloads a file from the specified URL and saves it to the cache path. Tracks the download progress.
Args: file_url (str): The URL of the file to download. token (str): The authorization token for the request. cache_path (str): The path to save the downloaded file. proxies (dict): Proxies for the request. film_id (str): Unique identifier for the film download. title (str): The title of the film. chunk_size (int): Size of each chunk to download. """ print(f"Downloading file from URL: {file_url} to {cache_path} with proxies: {proxies}") headers = {'Authorization': f'Bearer {token}'} try: response = requests.get(file_url, headers=headers, proxies=proxies, stream=True) response.raise_for_status() total_size = int(response.headers.get('content-length', 0)) download_progress[episode_id] = {"total": total_size, "downloaded": 0, "status": "Downloading", "start_time": time.time()}
os.makedirs(os.path.dirname(cache_path), exist_ok=True) with open(cache_path, 'wb') as file, tqdm(total=total_size, unit='B', unit_scale=True, desc=cache_path) as pbar: for data in response.iter_content(chunk_size=chunk_size): file.write(data) pbar.update(len(data)) download_progress[episode_id]["downloaded"] += len(data) print(f'File cached to {cache_path} successfully.') update_tv_store_json(title, cache_path) download_progress[episode_id]["status"] = "Completed" except RequestException as e: print(f"Error downloading file: {e}") download_progress[episode_id]["status"] = "Failed" except IOError as e: print(f"Error writing file {cache_path}: {e}") download_progress[episode_id]["status"] = "Failed" finally: if download_progress[episode_id]["status"] != "Downloading": download_progress[episode_id]["end_time"] = time.time()
def update_tv_store_json(title, cache_path): """ Updates the TV store JSON with the new file, organizing by title, season, and episode.
Args: title (str): The title of the TV show. cache_path (str): The local path where the file is saved. """ TV_STORE_JSON_PATH = os.path.join(CACHE_DIR, "tv_store.json")
tv_store_data = {} if os.path.exists(TV_STORE_JSON_PATH): with open(TV_STORE_JSON_PATH, 'r') as json_file: tv_store_data = json.load(json_file)
# Extract season and episode information from the cache_path season_part = os.path.basename(os.path.dirname(cache_path)) # Extracts 'Season 1' episode_part = os.path.basename(cache_path) # Extracts 'Grand Blue Dreaming - S01E01 - Deep Blue HDTV-720p.mp4'
# Create the structure if not already present if title not in tv_store_data: tv_store_data[title] = {} if season_part not in tv_store_data[title]: tv_store_data[title][season_part] = {}
# Assuming episode_part is unique for each episode within a season tv_store_data[title][season_part][episode_part] = cache_path
with open(TV_STORE_JSON_PATH, 'w') as json_file: json.dump(tv_store_data, json_file, indent=2)
print(f'TV store updated with {title}, {season_part}, {episode_part}.')
###############################################################################def get_file_structure(repo, token, path="", proxies=None): """ Fetches the file structure of a specified Hugging Face repository.
Args: repo (str): The name of the repository. token (str): The authorization token for the request. path (str, optional): The specific path in the repository. Defaults to "". proxies (dict, optional): The proxies to use for the request. Defaults to None.
Returns: list: A list of file structure information. """ api_url = f"https://huggingface.co/api/models/{repo}/tree/main/{path}" headers = {'Authorization': f'Bearer {token}'} print(f"Fetching file structure from URL: {api_url} with proxies: {proxies}") try: response = requests.get(api_url, headers=headers, proxies=proxies) response.raise_for_status() return response.json() except RequestException as e: print(f"Error fetching file structure: {e}") return []
def write_file_structure_to_json(file_structure, file_path): """ Writes the file structure to a JSON file.
Args: file_structure (list): The file structure data. file_path (str): The path where the JSON file will be saved. """ try: with open(file_path, 'w') as json_file: json.dump(file_structure, json_file, indent=2) print(f'File structure written to {file_path}') except IOError as e: print(f"Error writing file structure to JSON: {e}")
if __name__ == "__main__": file_url = "https://huggingface.co/Unicone-Studio/jellyfin_media/resolve/main/films/Funky%20Monkey%202004/Funky%20Monkey%20(2004)%20Web-dl%201080p.mp4" token = os.getenv("TOKEN") cache_path = os.path.join(CACHE_DIR, "films/Funky Monkey 2004/Funky Monkey (2004) Web-dl 1080p.mp4") proxies = get_system_proxies() film_id = "funky_monkey_2004" # Unique identifier for the film download download_film(file_url, token, cache_path, proxies=proxies, film_id=film_id)1234567891011121314151617181920212223242526272829303132import jsonfrom hf_scrapper import get_system_proxies, get_file_structure, write_file_structure_to_jsonfrom dotenv import load_dotenvimport os
load_dotenv()
def index_repository(token, repo, current_path="", proxies=None): file_structure = get_file_structure(repo, token, current_path, proxies) full_structure = [] for item in file_structure: if item['type'] == 'directory': sub_directory_structure = index_repository(token, repo, item['path'], proxies) full_structure.append({ "type": "directory", "path": item['path'], "contents": sub_directory_structure }) else: full_structure.append(item) return full_structure
def indexer(): token = os.getenv("TOKEN") repo = os.getenv("REPO") output_path = os.getenv("INDEX_FILE") proxies = get_system_proxies() full_structure = index_repository(token, repo, "", proxies) write_file_structure_to_json(full_structure, output_path) print(f"Full file structure for repository '{repo}' has been indexed and saved to {output_path}") 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174from flask import Flask, jsonify, request, send_from_directoryfrom flask_cors import CORSimport osimport jsonfrom threading import Threadimport urllib.parsefrom Instance import Instancefrom api import LoadBalancerAPI
app = Flask(__name__)CORS(app)
# Constants and ConfigurationCACHE_DIR = os.getenv("CACHE_DIR")INDEX_FILE = os.getenv("INDEX_FILE")TOKEN = os.getenv("TOKEN")REPO = os.getenv("REPO")ID = os.getenv("ID")URL = os.getenv("URL")LOAD_BALANCER_URL = os.getenv("LOAD_BALANCER_URL")
load_balancer_api = LoadBalancerAPI(base_url=LOAD_BALANCER_URL)instance = Instance(id=ID, url=URL, cache_dir=CACHE_DIR, index_file=INDEX_FILE, token=TOKEN, repo=REPO, load_balancer_api=load_balancer_api)
# API Endpoints@app.route('/api/film/<title>', methods=['GET'])def get_movie_api(title): """Endpoint to get the movie by title.""" if not title: return jsonify({"error": "Title parameter is required"}), 400 # Load the film store JSON with open(instance.FILM_STORE_JSON_PATH, 'r') as json_file: film_store_data = json.load(json_file)
# Check if the film is already cached if title in film_store_data: cache_path = film_store_data[title] if os.path.exists(cache_path): return send_from_directory(os.path.dirname(cache_path), os.path.basename(cache_path)) movie_path = instance.find_movie_path(title) if not movie_path: return jsonify({"error": "Movie not found"}), 404 cache_path = os.path.join(CACHE_DIR, movie_path) file_url = f"https://huggingface.co/{REPO}/resolve/main/{movie_path}" proxies = instance.get_system_proxies() film_id = instance.get_film_id(title) # Start the download in a separate thread if not already downloading if film_id not in instance.download_threads or not instance.download_threads[film_id].is_alive(): thread = Thread(target=instance.download_film, args=(file_url, TOKEN, cache_path, proxies, film_id, title)) instance.download_threads[film_id] = thread thread.start() return jsonify({"status": "Download started", "film_id": film_id})
@app.route('/api/tv/<title>/<season>/<episode>', methods=['GET'])def get_tv_show_api(title, season, episode): """Endpoint to get the TV show by title, season, and episode.""" if not title or not season or not episode: return jsonify({"error": "Title, season, and episode parameters are required"}), 400
# Load the TV store JSON with open(instance.TV_STORE_JSON_PATH, 'r') as json_file: tv_store_data = json.load(json_file)
# Check if the episode is already cached if title in tv_store_data and season in tv_store_data[title]: for ep in tv_store_data[title][season]: if episode in ep: cache_path = tv_store_data[title][season][ep] print(cache_path) if os.path.exists(cache_path): return send_from_directory(os.path.dirname(cache_path), os.path.basename(cache_path))
tv_path = instance.find_tv_path(title) if not tv_path: return jsonify({"error": "TV show not found"}), 404
episode_path = None for directory in instance.file_structure: if directory['type'] == 'directory' and directory['path'] == 'tv': for sub_directory in directory['contents']: if sub_directory['type'] == 'directory' and title.lower() in sub_directory['path'].lower(): for season_dir in sub_directory['contents']: if season_dir['type'] == 'directory' and season in season_dir['path']: for episode_file in season_dir['contents']: if episode_file['type'] == 'file' and episode in episode_file['path']: episode_path = episode_file['path'] break
if not episode_path: return jsonify({"error": "Episode not found"}), 404 cache_path = os.path.join(CACHE_DIR, episode_path) file_url = f"https://huggingface.co/{REPO}/resolve/main/{episode_path}" proxies = instance.get_system_proxies() episode_id = instance.encode_episodeid(title, season, episode) # Start the download in a separate thread if not already downloading if episode_id not in instance.download_threads or not instance.download_threads[episode_id].is_alive(): thread = Thread(target=instance.download_episode, args=(file_url, TOKEN, cache_path, proxies, episode_id, title)) instance.download_threads[episode_id] = thread thread.start() return jsonify({"status": "Download started", "episode_id": episode_id})
@app.route('/api/progress/<id>', methods=['GET'])def get_progress_api(id): """Endpoint to get the download progress of a movie or TV show episode.""" progress = instance.get_download_progress(id) return jsonify({"id": id, "progress": progress})
@app.route('/api/cache/size', methods=['GET'])def get_cache_size_api(): total_size = 0 for dirpath, dirnames, filenames in os.walk(CACHE_DIR): for f in filenames: fp = os.path.join(dirpath, f) total_size += os.path.getsize(fp) readable_size = instance.bytes_to_human_readable(total_size) return jsonify({"cache_size": readable_size})
@app.route('/api/cache/clear', methods=['POST'])def clear_cache_api(): for dirpath, dirnames, filenames in os.walk(CACHE_DIR): for f in filenames: fp = os.path.join(dirpath, f) os.remove(fp) return jsonify({"status": "Cache cleared"})
@app.route('/api/tv/store', methods=['GET'])def get_tv_store_api(): """Endpoint to get the TV store JSON.""" if os.path.exists(instance.TV_STORE_JSON_PATH): with open(instance.TV_STORE_JSON_PATH, 'r') as json_file: tv_store_data = json.load(json_file) return jsonify(tv_store_data) return jsonify({}), 404
@app.route('/api/film/store', methods=['GET'])def get_film_store_api(): """Endpoint to get the film store JSON.""" if os.path.exists(instance.FILM_STORE_JSON_PATH): with open(instance.FILM_STORE_JSON_PATH, 'r') as json_file: tv_store_data = json.load(json_file) return jsonify(tv_store_data) return jsonify({}), 404
@app.route("/api/film/all")def get_all_films_api(): return instance.get_all_films()
@app.route("/api/tv/all")def get_all_tvshows_api(): return instance.get_all_tv_shows()
@app.route("/api/get/report",methods=["GET"])def get_report(): report=instance.compile_report() return jsonify(report)
# Routes@app.route('/')def index(): return jsonify(instance.version)
# Main entry pointif __name__ == "__main__": app.run(debug=True, host="0.0.0.0", port=7860)12fastapiuvicorn[standard]123456fastapiuvicorn[standard]requestspython-dotenvtqdmaiofiles
.gitignoreAdded+12
Instance.pyAdded+403
README.md+1-1
api.pyAdded+22
app.py+16-2
hf_scrapper.pyAdded+249
indexer.pyAdded+32
old.app.pyAdded+174
requirements.txt+4