Spaces:
Paused

mikmc5's picture
Update app.py
9146b34 verified
Raw History Blame Contribute Delete
6.87 kB
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi import Request, Response
from fastapi.templating import Jinja2Templates # Added for rendering HTML templates
import json
import requests
import canais
import os
from dotenv import load_dotenv
load_dotenv()
app_ = FastAPI()
# Initialize Jinja2Templates for HTML rendering
templates = Jinja2Templates(directory="templates")
# Function to add CORS headers
def add_cors_headers(response: Response):
response.headers['Access-Control-Allow-Origin'] = '*' # Allow all domains
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' # Allowed methods
response.headers['Access-Control-Allow-Headers'] = 'Content-Type' # Allowed headers
return response
MANIFEST = {
"id": "com.skyflix",
"version": "1.0.0",
"name": "SKYFLIX",
"description": "Tenha o melhor dos filmes e séries com Skyflix",
"logo": "https://i.imgur.com/qVgkbYn.png",
"resources": ["catalog", "meta", "stream"],
"types": ["tv", "movie", "series"],
"catalogs": [
{
"type": "tv",
"id": "skyflix",
"name": "SKYFLIX",
"extra": [
{
"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 origin
resp.headers['Access-Control-Allow-Methods'] = 'GET, OPTIONS' # Allowed methods
resp.headers['Access-Control-Allow-Headers'] = 'Content-Type' # Allowed headers
return resp
except 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.host
server = 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.host
server = 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.host
server = 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'] = canal
break
return JSONResponse(content=meta)
return JSONResponse(content={"meta": {}})
@app_.get("/stream/{type}/{id}.json")
async def stream(type: str, id: str):
host = request.client.host
server = 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', [])
break
elif 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)