File size: 6,870 Bytes
3486b0a
f54b2fa
3486b0a
1204f2c
 
f54b2fa
 
1204f2c
71831c6
f54b2fa
71831c6
f54b2fa
71831c6
 
1204f2c
f54b2fa
 
1204f2c
3486b0a
1204f2c
 
 
3486b0a
 
f54b2fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71831c6
 
 
 
 
 
 
 
 
f54b2fa
 
 
 
 
71831c6
 
 
 
 
 
 
 
 
 
 
 
f54b2fa
 
 
 
1204f2c
71831c6
f54b2fa
71831c6
 
 
 
 
 
 
 
 
 
f54b2fa
71831c6
 
 
 
 
 
 
 
 
 
 
 
1204f2c
 
 
71831c6
 
 
 
 
 
1204f2c
71831c6
f54b2fa
 
 
71831c6
9146b34
f54b2fa
71831c6
 
 
 
 
 
 
 
1204f2c
71831c6
 
 
1204f2c
71831c6
9146b34
71831c6
 
 
f54b2fa
 
 
 
71831c6
f54b2fa
1204f2c
71831c6
 
 
1204f2c
71831c6
 
 
 
 
 
 
9146b34
71831c6
 
1204f2c
71831c6
 
 
 
 
 
 
 
 
 
 
 
1204f2c
71831c6
9146b34
71831c6
 
1204f2c
71831c6
1204f2c
71831c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1204f2c
71831c6
 
 
 
1204f2c
71831c6
 
 
3486b0a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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)