Spaces:
Paused
Paused
Update app.py
Browse files123456789101112131411 unmodified lines26272829303132333435363738394041424344454647484950515253545556575859606162import osfrom fastapi import FastAPI, Request, Responsefrom fastapi.responses import JSONResponsefrom fastapi.templating import Jinja2Templatesimport requestsimport canaisapp = FastAPI()templates = Jinja2Templates(directory="templates")MANIFEST = {"id": "com.skyflix","version": "1.0.0",11 unmodified lines{"name": "genre","options": ["Canais Abertos", "Variedades", "Filmes e Series", "Documentarios","Esportes", "Infantil", "Noticias", "PLUTO TV", "CANAL 24H"],"isRequired": False}]},{"type": "movie", "id": "skyflix", "name": "SKYFLIX", "extraSupported": ["search"]},{"type": "series", "id": "skyflix", "name": "SKYFLIX", "extraSupported": ["search"]}],"idPrefixes": ["skyflix", "tt"]}@app.get("/")async def home(request: Request):return templates.TemplateResponse("index.html", {"request": request, "manifest": MANIFEST})@app.get("/manifest.json")async def manifest():return JSONResponse(content=MANIFEST)@app.get("/catalog/{type}/{id}.json")async def catalog_route(type: str, id: str, request: Request):host = request.client.hostserver = f'https://{host}/logo?url=' if 'localhost' not in host else f'http://{host}/logo?url='if type == 'tv':canais_ = []list_canais = canais.canais_list(server)for canal in list_canais:canal.pop('streams', None) # Fix potential KeyErrorcanais_.append(canal)return JSONResponse(content={"metas": canais_})return JSONResponse(content={"metas": []})12345678910111213141516171819202122232425262711 unmodified lines39404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207from fastapi import FastAPIfrom fastapi.responses import JSONResponsefrom fastapi import Request, Responsefrom fastapi.templating import Jinja2Templates # Added for rendering HTML templatesimport jsonimport requestsimport canaisimport osfrom dotenv import load_dotenvload_dotenv()app_ = FastAPI()# Initialize Jinja2Templates for HTML renderingtemplates = Jinja2Templates(directory="templates")# Function to add CORS headersdef add_cors_headers(response: Response):response.headers['Access-Control-Allow-Origin'] = '*' # Allow all domainsresponse.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' # Allowed methodsresponse.headers['Access-Control-Allow-Headers'] = 'Content-Type' # Allowed headersreturn responseMANIFEST = {"id": "com.skyflix","version": "1.0.0",11 unmodified lines{"name": "genre","options": ["Canais Abertos","Variedades","Filmes e Series","Documentarios","Esportes","Infantil","Noticias","PLUTO TV","CANAL 24H"],"isRequired": False}]},{"type": "movie","id": "skyflix","name": "SKYFLIX","extraSupported": ["search"]},{"type": "series","id": "skyflix","name": "SKYFLIX","extraSupported": ["search"]}],"idPrefixes": ["skyflix", "tt"]}@app_.get("/")async def home(request: Request):name = MANIFEST['name']types = MANIFEST['types']logo = MANIFEST['logo']description = MANIFEST['description']version = MANIFEST['version']return templates.TemplateResponse('index.html', {"request": request, "name": name, "types": types, "logo": logo, "description": description, "version": version})@app_.get("/logo")async def proxy_logo(request: Request):image_url = request.query_params.get('url')if not image_url:return JSONResponse(content={"error": "Nenhuma URL fornecida"}, status_code=400)try:response = requests.get(image_url, stream=True)if response.status_code != 200:return JSONResponse(content={"error": f"Erro ao buscar a imagem: {response.status_code}"}, status_code=400)content_type = response.headers.get('Content-Type', 'image/jpeg')resp = Response(content=response.content, media_type=content_type)resp.headers['Access-Control-Allow-Origin'] = '*' # Allow any originresp.headers['Access-Control-Allow-Methods'] = 'GET, OPTIONS' # Allowed methodsresp.headers['Access-Control-Allow-Headers'] = 'Content-Type' # Allowed headersreturn respexcept requests.exceptions.RequestException as e:return JSONResponse(content={"error": f"Erro ao buscar a imagem: {str(e)}"}, status_code=500)@app_.get("/manifest.json")async def manifest():return JSONResponse(content=MANIFEST)@app_.get("/catalog/tv/skyflix/genre={id}.json")async def genres(id: str):host = request.client.hostserver = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='canais_ = []if 'skip' not in id:list_canais = canais.canais_list(server)for canal in list_canais:canal.pop('streams')if id in canal['genres']:canais_.append(canal)return JSONResponse(content={"metas": canais_})@app_.get("/catalog/{type}/{id}.json")async def catalog_route(type: str, id: str):host = request.client.hostserver = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='if type == 'tv':canais_ = []list_canais = canais.canais_list(server)for canal in list_canais:canal.pop('streams')canais_.append(canal)return JSONResponse(content={"metas": canais_})else:return JSONResponse(content={"metas": []})@app_.get("/catalog/{type}/skyflix/search={query}.json")async def search(type: str, query: str):catalog = catalog_search(query)results = [item for item in catalog if item.get('type', '') == type] if catalog else []return JSONResponse(content={"metas": results})@app_.get("/meta/{type}/{id}.json")async def meta(type: str, id: str):host = request.client.hostserver = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='if type == 'tv':meta = {}list_canais = canais.canais_list(server)for canal in list_canais:if canal['id'] == id:canal.pop('streams')meta['meta'] = canalbreakreturn JSONResponse(content=meta)return JSONResponse(content={"meta": {}})@app_.get("/stream/{type}/{id}.json")async def stream(type: str, id: str):host = request.client.hostserver = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='if type == 'tv':scrape_ = []list_canais = canais.canais_list(server)for canal in list_canais:if canal['id'] == id:scrape_ = canal.get('streams', [])breakelif type in ['movie', 'series']:try:stream_, headers = search_link(id)scrape_ = [{"url": stream_,"name": "SKYFLIX","description": "Netcine","behaviorHints": {"notWebReady": True,"proxyHeaders": {"request": {"User-Agent": headers['User-Agent'],"Referer": headers['Referer'],"Cookie": headers['Cookie']}}}}]except:scrape_ = []return JSONResponse(content={"streams": scrape_})@app_.options("/{path:path}")async def options_handler(path: str):response = Response()return add_cors_headers(response)
app.py+163-18