Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,27 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
-
from fastapi import Response
|
| 4 |
from fastapi.templating import Jinja2Templates # Added for rendering HTML templates
|
| 5 |
import json
|
| 6 |
import requests
|
| 7 |
import canais
|
| 8 |
import os
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
-
from fastapi.middleware.cors import CORSMiddleware # Added for handling CORS
|
| 11 |
|
| 12 |
-
# Load environment variables from .env file
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
-
# Initialize FastAPI app
|
| 16 |
app_ = FastAPI()
|
| 17 |
|
| 18 |
-
# Add CORS middleware
|
| 19 |
-
app_.add_middleware(
|
| 20 |
-
CORSMiddleware,
|
| 21 |
-
allow_origins=["*"], # Allow all origins
|
| 22 |
-
allow_credentials=True,
|
| 23 |
-
allow_methods=["GET", "POST", "OPTIONS"], # Allowed methods
|
| 24 |
-
allow_headers=["Content-Type"], # Allowed headers
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
# Initialize Jinja2Templates for HTML rendering
|
| 28 |
templates = Jinja2Templates(directory="templates")
|
| 29 |
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
MANIFEST = {
|
| 32 |
"id": "com.skyflix",
|
| 33 |
"version": "1.0.0",
|
|
@@ -75,7 +69,7 @@ MANIFEST = {
|
|
| 75 |
"idPrefixes": ["skyflix", "tt"]
|
| 76 |
}
|
| 77 |
|
| 78 |
-
|
| 79 |
@app_.get("/")
|
| 80 |
async def home(request: Request):
|
| 81 |
name = MANIFEST['name']
|
|
@@ -85,7 +79,6 @@ async def home(request: Request):
|
|
| 85 |
version = MANIFEST['version']
|
| 86 |
return templates.TemplateResponse('index.html', {"request": request, "name": name, "types": types, "logo": logo, "description": description, "version": version})
|
| 87 |
|
| 88 |
-
# Proxy for logo
|
| 89 |
@app_.get("/logo")
|
| 90 |
async def proxy_logo(request: Request):
|
| 91 |
image_url = request.query_params.get('url')
|
|
@@ -111,12 +104,11 @@ async def proxy_logo(request: Request):
|
|
| 111 |
except requests.exceptions.RequestException as e:
|
| 112 |
return JSONResponse(content={"error": f"Erro ao buscar a imagem: {str(e)}"}, status_code=500)
|
| 113 |
|
| 114 |
-
|
| 115 |
@app_.get("/manifest.json")
|
| 116 |
async def manifest():
|
| 117 |
return JSONResponse(content=MANIFEST)
|
| 118 |
|
| 119 |
-
# Genre route for TV catalog
|
| 120 |
@app_.get("/catalog/tv/skyflix/genre={id}.json")
|
| 121 |
async def genres(request: Request, id: str):
|
| 122 |
host = request.client.host
|
|
@@ -128,11 +120,11 @@ async def genres(request: Request, id: str):
|
|
| 128 |
for canal in list_canais:
|
| 129 |
canal.pop('streams')
|
| 130 |
if id in canal['genres']:
|
| 131 |
-
canais_.append(canal)
|
| 132 |
|
| 133 |
return JSONResponse(content={"metas": canais_})
|
| 134 |
|
| 135 |
-
|
| 136 |
@app_.get("/catalog/{type}/{id}.json")
|
| 137 |
async def catalog_route(request: Request, type: str, id: str):
|
| 138 |
host = request.client.host
|
|
@@ -144,18 +136,17 @@ async def catalog_route(request: Request, type: str, id: str):
|
|
| 144 |
for canal in list_canais:
|
| 145 |
canal.pop('streams')
|
| 146 |
canais_.append(canal)
|
| 147 |
-
return JSONResponse(content={"metas": canais_})
|
| 148 |
else:
|
| 149 |
return JSONResponse(content={"metas": []})
|
| 150 |
|
| 151 |
-
|
| 152 |
@app_.get("/catalog/{type}/skyflix/search={query}.json")
|
| 153 |
async def search(type: str, query: str):
|
| 154 |
catalog = catalog_search(query)
|
| 155 |
results = [item for item in catalog if item.get('type', '') == type] if catalog else []
|
| 156 |
return JSONResponse(content={"metas": results})
|
| 157 |
|
| 158 |
-
# Meta route (individual item details)
|
| 159 |
@app_.get("/meta/{type}/{id}.json")
|
| 160 |
async def meta(request: Request, type: str, id: str):
|
| 161 |
host = request.client.host
|
|
@@ -173,7 +164,7 @@ async def meta(request: Request, type: str, id: str):
|
|
| 173 |
|
| 174 |
return JSONResponse(content={"meta": {}})
|
| 175 |
|
| 176 |
-
|
| 177 |
@app_.get("/stream/{type}/{id}.json")
|
| 178 |
async def stream(request: Request, type: str, id: str):
|
| 179 |
host = request.client.host
|
|
@@ -204,14 +195,13 @@ async def stream(request: Request, type: str, id: str):
|
|
| 204 |
}
|
| 205 |
}
|
| 206 |
}]
|
| 207 |
-
except
|
| 208 |
scrape_ = []
|
| 209 |
-
print(f"Error fetching stream for {id}: {e}")
|
| 210 |
|
| 211 |
return JSONResponse(content={"streams": scrape_})
|
| 212 |
|
| 213 |
-
|
| 214 |
@app_.options("/{path:path}")
|
| 215 |
async def options_handler(path: str):
|
| 216 |
response = Response()
|
| 217 |
-
return response
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi import Request, Response
|
| 4 |
from fastapi.templating import Jinja2Templates # Added for rendering HTML templates
|
| 5 |
import json
|
| 6 |
import requests
|
| 7 |
import canais
|
| 8 |
import os
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
| 10 |
|
|
|
|
| 11 |
load_dotenv()
|
| 12 |
|
|
|
|
| 13 |
app_ = FastAPI()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Initialize Jinja2Templates for HTML rendering
|
| 16 |
templates = Jinja2Templates(directory="templates")
|
| 17 |
|
| 18 |
+
# Function to add CORS headers
|
| 19 |
+
def add_cors_headers(response: Response):
|
| 20 |
+
response.headers['Access-Control-Allow-Origin'] = '*' # Allow all domains
|
| 21 |
+
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' # Allowed methods
|
| 22 |
+
response.headers['Access-Control-Allow-Headers'] = 'Content-Type' # Allowed headers
|
| 23 |
+
return response
|
| 24 |
+
|
| 25 |
MANIFEST = {
|
| 26 |
"id": "com.skyflix",
|
| 27 |
"version": "1.0.0",
|
|
|
|
| 69 |
"idPrefixes": ["skyflix", "tt"]
|
| 70 |
}
|
| 71 |
|
| 72 |
+
|
| 73 |
@app_.get("/")
|
| 74 |
async def home(request: Request):
|
| 75 |
name = MANIFEST['name']
|
|
|
|
| 79 |
version = MANIFEST['version']
|
| 80 |
return templates.TemplateResponse('index.html', {"request": request, "name": name, "types": types, "logo": logo, "description": description, "version": version})
|
| 81 |
|
|
|
|
| 82 |
@app_.get("/logo")
|
| 83 |
async def proxy_logo(request: Request):
|
| 84 |
image_url = request.query_params.get('url')
|
|
|
|
| 104 |
except requests.exceptions.RequestException as e:
|
| 105 |
return JSONResponse(content={"error": f"Erro ao buscar a imagem: {str(e)}"}, status_code=500)
|
| 106 |
|
| 107 |
+
|
| 108 |
@app_.get("/manifest.json")
|
| 109 |
async def manifest():
|
| 110 |
return JSONResponse(content=MANIFEST)
|
| 111 |
|
|
|
|
| 112 |
@app_.get("/catalog/tv/skyflix/genre={id}.json")
|
| 113 |
async def genres(request: Request, id: str):
|
| 114 |
host = request.client.host
|
|
|
|
| 120 |
for canal in list_canais:
|
| 121 |
canal.pop('streams')
|
| 122 |
if id in canal['genres']:
|
| 123 |
+
canais_.append(canal)
|
| 124 |
|
| 125 |
return JSONResponse(content={"metas": canais_})
|
| 126 |
|
| 127 |
+
|
| 128 |
@app_.get("/catalog/{type}/{id}.json")
|
| 129 |
async def catalog_route(request: Request, type: str, id: str):
|
| 130 |
host = request.client.host
|
|
|
|
| 136 |
for canal in list_canais:
|
| 137 |
canal.pop('streams')
|
| 138 |
canais_.append(canal)
|
| 139 |
+
return JSONResponse(content={"metas": canais_})
|
| 140 |
else:
|
| 141 |
return JSONResponse(content={"metas": []})
|
| 142 |
|
| 143 |
+
|
| 144 |
@app_.get("/catalog/{type}/skyflix/search={query}.json")
|
| 145 |
async def search(type: str, query: str):
|
| 146 |
catalog = catalog_search(query)
|
| 147 |
results = [item for item in catalog if item.get('type', '') == type] if catalog else []
|
| 148 |
return JSONResponse(content={"metas": results})
|
| 149 |
|
|
|
|
| 150 |
@app_.get("/meta/{type}/{id}.json")
|
| 151 |
async def meta(request: Request, type: str, id: str):
|
| 152 |
host = request.client.host
|
|
|
|
| 164 |
|
| 165 |
return JSONResponse(content={"meta": {}})
|
| 166 |
|
| 167 |
+
|
| 168 |
@app_.get("/stream/{type}/{id}.json")
|
| 169 |
async def stream(request: Request, type: str, id: str):
|
| 170 |
host = request.client.host
|
|
|
|
| 195 |
}
|
| 196 |
}
|
| 197 |
}]
|
| 198 |
+
except:
|
| 199 |
scrape_ = []
|
|
|
|
| 200 |
|
| 201 |
return JSONResponse(content={"streams": scrape_})
|
| 202 |
|
| 203 |
+
|
| 204 |
@app_.options("/{path:path}")
|
| 205 |
async def options_handler(path: str):
|
| 206 |
response = Response()
|
| 207 |
+
return add_cors_headers(response)
|