Spaces:
Paused
Paused
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import traceback
|
| 2 |
+
import logging
|
| 3 |
+
from fastapi import FastAPI, Depends, HTTPException, Security, Request
|
| 4 |
+
from fastapi.security.api_key import APIKeyHeader
|
| 5 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from moviebox_api import (
|
| 8 |
+
Search, Session, SubjectType,
|
| 9 |
+
DownloadableMovieFilesDetail, DownloadableTVSeriesFilesDetail,
|
| 10 |
+
Trending, Homepage, MovieDetails, TVSeriesDetails,
|
| 11 |
+
Recommend, PopularSearch, HotMoviesAndTVSeries
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
logging.basicConfig(level=logging.INFO)
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
app = FastAPI(title="TorchFlix API")
|
| 18 |
+
app.add_middleware(
|
| 19 |
+
CORSMiddleware,
|
| 20 |
+
allow_origins=["*"],
|
| 21 |
+
allow_credentials=True,
|
| 22 |
+
allow_methods=["*"],
|
| 23 |
+
allow_headers=["*"],
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
API_KEY = "elijah2909_secret_key"
|
| 27 |
+
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
|
| 28 |
+
|
| 29 |
+
async def get_api_key(api_key: str = Security(api_key_header)):
|
| 30 |
+
if api_key == API_KEY: return api_key
|
| 31 |
+
raise HTTPException(status_code=403, detail="Invalid API Key. Use elijah2909_secret_key")
|
| 32 |
+
|
| 33 |
+
session = Session()
|
| 34 |
+
|
| 35 |
+
@app.middleware("http")
|
| 36 |
+
async def debug_requests(request: Request, call_next):
|
| 37 |
+
return await call_next(request)
|
| 38 |
+
|
| 39 |
+
@app.get("/", response_class=HTMLResponse)
|
| 40 |
+
async def serve_ui():
|
| 41 |
+
return """
|
| 42 |
+
<html>
|
| 43 |
+
<head><title>TorchFlix API</title></head>
|
| 44 |
+
<body style="font-family: Arial; text-align: center; padding: 50px;">
|
| 45 |
+
<h1>TorchFlix API is Running!</h1>
|
| 46 |
+
<p>Your Hugging Face backend is fully operational.</p>
|
| 47 |
+
</body>
|
| 48 |
+
</html>
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
async def _get_target_item(query: str, type: str):
|
| 52 |
+
sub_type = SubjectType.MOVIES if type == "movie" else SubjectType.TV_SERIES
|
| 53 |
+
search = Search(session, query=query, subject_type=sub_type)
|
| 54 |
+
results = await search.get_content_model()
|
| 55 |
+
if not results.items: raise Exception(f"No {type} found for query: {query}")
|
| 56 |
+
return results.first_item
|
| 57 |
+
|
| 58 |
+
@app.get("/api/search")
|
| 59 |
+
async def search_media(query: str, type: str = "all", api_key: str = Depends(get_api_key)):
|
| 60 |
+
try:
|
| 61 |
+
sub_type = SubjectType.ALL
|
| 62 |
+
if type.lower() == "movie": sub_type = SubjectType.MOVIES
|
| 63 |
+
elif type.lower() == "series": sub_type = SubjectType.TV_SERIES
|
| 64 |
+
return await Search(session, query=query, subject_type=sub_type, per_page=15).get_content()
|
| 65 |
+
except Exception as e: return JSONResponse(status_code=500, content={"error": str(e)})
|
| 66 |
+
|
| 67 |
+
@app.get("/api/trending")
|
| 68 |
+
async def get_trending(api_key: str = Depends(get_api_key)):
|
| 69 |
+
try: return await Trending(session).get_content()
|
| 70 |
+
except Exception as e: return JSONResponse(status_code=500, content={"error": str(e)})
|
| 71 |
+
|
| 72 |
+
@app.get("/api/media-files")
|
| 73 |
+
async def get_media_files(query: str, type: str = "movie", season: int = 1, episode: int = 1, api_key: str = Depends(get_api_key)):
|
| 74 |
+
try:
|
| 75 |
+
target_item = await _get_target_item(query, type)
|
| 76 |
+
if type == "movie": details = await DownloadableMovieFilesDetail(session, target_item).get_content_model()
|
| 77 |
+
else: details = await DownloadableTVSeriesFilesDetail(session, target_item).get_content_model(season=season, episode=episode)
|
| 78 |
+
videos = [{"resolution": d.resolution, "url": str(d.url), "size": d.size, "ext": d.ext} for d in details.downloads]
|
| 79 |
+
subs = [{"language": c.lanName, "url": str(c.url), "ext": c.ext} for c in details.captions]
|
| 80 |
+
return {"status": "success", "title": target_item.title, "videos": videos, "subtitles": subs}
|
| 81 |
+
except Exception as e: return JSONResponse(status_code=500, content={"error": str(e)})
|
| 82 |
+
|
| 83 |
+
@app.get("/api/homepage")
|
| 84 |
+
async def get_homepage(api_key: str = Depends(get_api_key)):
|
| 85 |
+
try: return await Homepage(session).get_content()
|
| 86 |
+
except Exception as e: return JSONResponse(status_code=500, content={"error": str(e)})
|
| 87 |
+
|
| 88 |
+
@app.get("/api/details")
|
| 89 |
+
async def get_details(query: str, type: str = "movie", api_key: str = Depends(get_api_key)):
|
| 90 |
+
try:
|
| 91 |
+
target_item = await _get_target_item(query, type)
|
| 92 |
+
if type == "movie": return await MovieDetails(target_item, session).get_content()
|
| 93 |
+
else: return await TVSeriesDetails(target_item, session).get_content()
|
| 94 |
+
except Exception as e: return JSONResponse(status_code=500, content={"error": str(e)})
|
| 95 |
+
|
| 96 |
+
@app.get("/api/recommendations")
|
| 97 |
+
async def get_recommendations(query: str, type: str = "movie", api_key: str = Depends(get_api_key)):
|
| 98 |
+
try: return await Recommend(session, await _get_target_item(query, type)).get_content()
|
| 99 |
+
except Exception as e: return JSONResponse(status_code=500, content={"error": str(e)})
|
| 100 |
+
|
| 101 |
+
@app.get("/api/popular-searches")
|
| 102 |
+
async def get_popular_searches(api_key: str = Depends(get_api_key)):
|
| 103 |
+
try: return await PopularSearch(session).get_content()
|
| 104 |
+
except Exception as e: return JSONResponse(status_code=500, content={"error": str(e)})
|
| 105 |
+
|
| 106 |
+
@app.get("/api/hot")
|
| 107 |
+
async def get_hot(api_key: str = Depends(get_api_key)):
|
| 108 |
+
try: return await HotMoviesAndTVSeries(session).get_content()
|
| 109 |
+
except Exception as e: return JSONResponse(status_code=500, content={"error": str(e)})
|