Commit
·
dc50e93
1
Parent(s):
886864f
fix
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
-
from fastapi import
|
| 2 |
-
from fastapi
|
|
|
|
| 3 |
from Instance import Instance
|
| 4 |
from api import LoadBalancerAPI
|
| 5 |
import os
|
|
@@ -34,3 +35,84 @@ async def get_tv_store_api():
|
|
| 34 |
async def get_film_store_api():
|
| 35 |
"""Endpoint to get the TV store JSON."""
|
| 36 |
return JSONResponse(instance.FILM_STORE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi.responses import JSONResponse, FileResponse
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from threading import Thread
|
| 4 |
from Instance import Instance
|
| 5 |
from api import LoadBalancerAPI
|
| 6 |
import os
|
|
|
|
| 35 |
async def get_film_store_api():
|
| 36 |
"""Endpoint to get the TV store JSON."""
|
| 37 |
return JSONResponse(instance.FILM_STORE)
|
| 38 |
+
|
| 39 |
+
@app.get("/api/get/film/{title}")
|
| 40 |
+
async def get_movie_api(title: str):
|
| 41 |
+
"""Endpoint to get the movie by title."""
|
| 42 |
+
if not title:
|
| 43 |
+
raise HTTPException(status_code=400, detail="Title parameter is required")
|
| 44 |
+
|
| 45 |
+
# Check if the film is already cached
|
| 46 |
+
if title in instance.FILM_STORE:
|
| 47 |
+
cache_path = instance.FILM_STORE[title]
|
| 48 |
+
if os.path.exists(cache_path):
|
| 49 |
+
return FileResponse(cache_path)
|
| 50 |
+
|
| 51 |
+
movie_path = instance.find_movie_path(title)
|
| 52 |
+
|
| 53 |
+
if not movie_path:
|
| 54 |
+
raise HTTPException(status_code=404, detail="Movie not found")
|
| 55 |
+
|
| 56 |
+
cache_path = os.path.join(CACHE_DIR, movie_path)
|
| 57 |
+
file_url = f"https://huggingface.co/{REPO}/resolve/main/{movie_path}"
|
| 58 |
+
film_id = instance.get_film_id(title)
|
| 59 |
+
|
| 60 |
+
# Start the download in a separate thread if not already downloading
|
| 61 |
+
if film_id not in instance.download_threads or not instance.download_threads[film_id].is_alive():
|
| 62 |
+
thread = Thread(target=instance.download_film, args=(file_url, TOKEN, cache_path, film_id, title))
|
| 63 |
+
instance.download_threads[film_id] = thread
|
| 64 |
+
thread.start()
|
| 65 |
+
|
| 66 |
+
return JSONResponse({"status": "Download started", "film_id": film_id})
|
| 67 |
+
|
| 68 |
+
@app.get("/api/get/tv/{title}/{season}/{episode}")
|
| 69 |
+
async def get_tv_show_api(title: str, season: str, episode: str):
|
| 70 |
+
"""Endpoint to get the TV show by title, season, and episode."""
|
| 71 |
+
if not title or not season or not episode:
|
| 72 |
+
raise HTTPException(status_code=400, detail="Title, season, and episode parameters are required")
|
| 73 |
+
|
| 74 |
+
# Check if the episode is already cached
|
| 75 |
+
if title in instance.TV_STORE and season in instance.TV_STORE[title]:
|
| 76 |
+
for ep in instance.TV_STORE[title][season]:
|
| 77 |
+
if episode in ep:
|
| 78 |
+
cache_path = instance.TV_STORE[title][season][ep]
|
| 79 |
+
if os.path.exists(cache_path):
|
| 80 |
+
return FileResponse(cache_path)
|
| 81 |
+
|
| 82 |
+
tv_path = instance.find_tv_path(title)
|
| 83 |
+
|
| 84 |
+
if not tv_path:
|
| 85 |
+
raise HTTPException(status_code=404, detail="TV show not found")
|
| 86 |
+
|
| 87 |
+
episode_path = None
|
| 88 |
+
for directory in instance.file_structure:
|
| 89 |
+
if directory['type'] == 'directory' and directory['path'] == 'tv':
|
| 90 |
+
for sub_directory in directory['contents']:
|
| 91 |
+
if sub_directory['type'] == 'directory' and title.lower() in sub_directory['path'].lower():
|
| 92 |
+
for season_dir in sub_directory['contents']:
|
| 93 |
+
if season_dir['type'] == 'directory' and season in season_dir['path']:
|
| 94 |
+
for episode_file in season_dir['contents']:
|
| 95 |
+
if episode_file['type'] == 'file' and episode in episode_file['path']:
|
| 96 |
+
episode_path = episode_file['path']
|
| 97 |
+
break
|
| 98 |
+
|
| 99 |
+
if not episode_path:
|
| 100 |
+
raise HTTPException(status_code=404, detail="Episode not found")
|
| 101 |
+
|
| 102 |
+
cache_path = os.path.join(CACHE_DIR, episode_path)
|
| 103 |
+
file_url = f"https://huggingface.co/{REPO}/resolve/main/{episode_path}"
|
| 104 |
+
episode_id = instance.encode_episodeid(title, season, episode)
|
| 105 |
+
|
| 106 |
+
# Start the download in a separate thread if not already downloading
|
| 107 |
+
if episode_id not in instance.download_threads or not instance.download_threads[episode_id].is_alive():
|
| 108 |
+
thread = Thread(target=instance.download_episode, args=(file_url, TOKEN, cache_path, episode_id, title))
|
| 109 |
+
instance.download_threads[episode_id] = thread
|
| 110 |
+
thread.start()
|
| 111 |
+
|
| 112 |
+
return JSONResponse({"status": "Download started", "episode_id": episode_id})
|
| 113 |
+
|
| 114 |
+
@app.get("/api/get/progress/{id}")
|
| 115 |
+
async def get_progress_api(id: str):
|
| 116 |
+
"""Endpoint to get the download progress of a movie or TV show episode."""
|
| 117 |
+
progress = instance.get_download_progress(id)
|
| 118 |
+
return JSONResponse({"id": id, "progress": progress})
|