mikmc5 commited on
Commit
951ff4e
·
verified ·
1 Parent(s): 1e4bfb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -24
app.py CHANGED
@@ -1,27 +1,40 @@
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,7 +82,7 @@ MANIFEST = {
69
  "idPrefixes": ["skyflix", "tt"]
70
  }
71
 
72
-
73
  @app_.get("/")
74
  async def home(request: Request):
75
  name = MANIFEST['name']
@@ -79,6 +92,7 @@ async def home(request: Request):
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,13 +118,14 @@ async def proxy_logo(request: Request):
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(id: str):
114
  host = request.client.host
115
  server = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='
116
  canais_ = []
@@ -120,13 +135,13 @@ async def genres(id: str):
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(type: str, id: str):
130
  host = request.client.host
131
  server = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='
132
 
@@ -136,22 +151,23 @@ async def catalog_route(type: str, id: str):
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(type: str, id: str):
152
  host = request.client.host
153
  server = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='
154
-
155
  if type == 'tv':
156
  meta = {}
157
  list_canais = canais.canais_list(server)
@@ -164,12 +180,12 @@ async def meta(type: str, id: str):
164
 
165
  return JSONResponse(content={"meta": {}})
166
 
167
-
168
  @app_.get("/stream/{type}/{id}.json")
169
- async def stream(type: str, id: str):
170
  host = request.client.host
171
  server = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='
172
-
173
  if type == 'tv':
174
  scrape_ = []
175
  list_canais = canais.canais_list(server)
@@ -195,13 +211,14 @@ async def stream(type: str, id: str):
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)
 
1
+ from fastapi import FastAPI, Request
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
+ # Function to add CORS headers (no longer needed with middleware)
31
+ # def add_cors_headers(response: Response):
32
+ # response.headers['Access-Control-Allow-Origin'] = '*' # Allow all domains
33
+ # response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' # Allowed methods
34
+ # response.headers['Access-Control-Allow-Headers'] = 'Content-Type' # Allowed headers
35
+ # return response
36
 
37
+ # Manifest data
38
  MANIFEST = {
39
  "id": "com.skyflix",
40
  "version": "1.0.0",
 
82
  "idPrefixes": ["skyflix", "tt"]
83
  }
84
 
85
+ # Home route
86
  @app_.get("/")
87
  async def home(request: Request):
88
  name = MANIFEST['name']
 
92
  version = MANIFEST['version']
93
  return templates.TemplateResponse('index.html', {"request": request, "name": name, "types": types, "logo": logo, "description": description, "version": version})
94
 
95
+ # Proxy for logo
96
  @app_.get("/logo")
97
  async def proxy_logo(request: Request):
98
  image_url = request.query_params.get('url')
 
118
  except requests.exceptions.RequestException as e:
119
  return JSONResponse(content={"error": f"Erro ao buscar a imagem: {str(e)}"}, status_code=500)
120
 
121
+ # Manifest route
122
  @app_.get("/manifest.json")
123
  async def manifest():
124
  return JSONResponse(content=MANIFEST)
125
 
126
+ # Genre route for TV catalog
127
  @app_.get("/catalog/tv/skyflix/genre={id}.json")
128
+ async def genres(request: Request, id: str):
129
  host = request.client.host
130
  server = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='
131
  canais_ = []
 
135
  for canal in list_canais:
136
  canal.pop('streams')
137
  if id in canal['genres']:
138
+ canais_.append(canal)
139
 
140
  return JSONResponse(content={"metas": canais_})
141
 
142
+ # Catalog route (general)
143
  @app_.get("/catalog/{type}/{id}.json")
144
+ async def catalog_route(request: Request, type: str, id: str):
145
  host = request.client.host
146
  server = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='
147
 
 
151
  for canal in list_canais:
152
  canal.pop('streams')
153
  canais_.append(canal)
154
+ return JSONResponse(content={"metas": canais_})
155
  else:
156
  return JSONResponse(content={"metas": []})
157
 
158
+ # Search route
159
  @app_.get("/catalog/{type}/skyflix/search={query}.json")
160
  async def search(type: str, query: str):
161
  catalog = catalog_search(query)
162
  results = [item for item in catalog if item.get('type', '') == type] if catalog else []
163
  return JSONResponse(content={"metas": results})
164
 
165
+ # Meta route (individual item details)
166
  @app_.get("/meta/{type}/{id}.json")
167
+ async def meta(request: Request, type: str, id: str):
168
  host = request.client.host
169
  server = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='
170
+
171
  if type == 'tv':
172
  meta = {}
173
  list_canais = canais.canais_list(server)
 
180
 
181
  return JSONResponse(content={"meta": {}})
182
 
183
+ # Stream route
184
  @app_.get("/stream/{type}/{id}.json")
185
+ async def stream(request: Request, type: str, id: str):
186
  host = request.client.host
187
  server = f'https://{host}/logo?url=' if not ('localhost' in host or '127.0.0.1' in host) else f'http://{host}/logo?url='
188
+
189
  if type == 'tv':
190
  scrape_ = []
191
  list_canais = canais.canais_list(server)
 
211
  }
212
  }
213
  }]
214
+ except Exception as e:
215
  scrape_ = []
216
+ print(f"Error fetching stream for {id}: {e}")
217
 
218
  return JSONResponse(content={"streams": scrape_})
219
 
220
+ # CORS preflight handler
221
  @app_.options("/{path:path}")
222
  async def options_handler(path: str):
223
  response = Response()
224
+ return response