forked from cool-dev-guy/vidsrc-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
129 lines (106 loc) · 3.92 KB
/
main.py
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
"""
file written by : cool-dev-guy
based on ciarands vidsrc resolver's.
This is an ASGI made using fastapi as a proof of concept and for educational uses.The writer/dev is not responsible for any isues caused by this project.
"""
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware # CORS
import gzip
from redis import Redis
#from redis import Redis
from models import vidsrctoget, vidsrcmeget, info, fetch
from io import BytesIO
from fastapi.responses import StreamingResponse, FileResponse
from models import stremio_addon,m3u8_parser, redis_checker
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
redcon: Redis = redis_checker.get_redis_connection()
@app.get('/')
async def index():
return await info()
@app.get('/vidsrc/{dbid}')
async def vidsrc(dbid: str, s: int = None, e: int = None):
if dbid:
return {
"status": 200,
"info": "success",
"sources": await vidsrctoget(dbid, s, e)
}
else:
raise HTTPException(status_code=404, detail=f"Invalid id: {dbid}")
@app.get('/vsrcme/{dbid}')
async def vsrcme(dbid: str = '', s: int = None, e: int = None, l: str = 'eng'):
if dbid:
return {
"status": 200,
"info": "success",
"sources": await vidsrcmeget(dbid, s, e)
}
else:
raise HTTPException(status_code=404, detail=f"Invalid id: {dbid}")
# imDB id
@app.get('/streams/{dbid}')
async def streams(dbid: str = '', s: int = None, e: int = None):
if dbid:
try:
return {
"status": 200,
"info": "success",
"sources": await vidsrcmeget(dbid, s, e) + await vidsrctoget(dbid, s, e)
}
except:
return []
else:
#raise HTTPException(status_code=404, detail=f"Invalid id: {dbid}")
return []
@app.get("/subs")
async def subs(url: str):
try:
response = await fetch(url)
with gzip.open(BytesIO(response.content), 'rt', encoding='utf-8') as f:
subtitle_content = f.read()
async def generate():
yield subtitle_content.encode("utf-8")
return StreamingResponse(generate(), media_type="application/octet-stream",
headers={"Content-Disposition": "attachment; filename=subtitle.srt"})
except:
raise HTTPException(status_code=500, detail=f"Error fetching subtitle")
@app.get('/manifest.json')
async def get_manifest(request: Request):
# Building the URL from request data
url_scheme = request.url.scheme # 'http' or 'https'
server_host = request.headers.get("host") # Get the host header
full_url = f"{url_scheme}://{server_host}"
#return {"URL": full_url}
return stremio_addon.get_manifest(full_url)
@app.get('/stream/{type}/{id}.json')
async def get_stream(type: str, id: str):
stored_json = redis_checker.check_redis(redcon,id)
if stored_json == -1 or stored_json == 0:
#return f"<h1>{type} {stremio_addon.transform_string(id)}"
response = await streams( *stremio_addon.transform_string(id))
if len(response) == 0:
return []
streamsList = [
{'title': source['name'], 'type': type, 'url': source['data']['stream']}
for source in response.get('sources', [])
if source['data']['stream'] and source['data']['stream'].strip()
]
m3u8_lists = m3u8_parser.getJSONs({'streams': streamsList})
print(streamsList)
if len(m3u8_lists['streams']) > 0:
redis_checker.store_json(redcon,id,m3u8_lists)
return m3u8_lists
else:
print("returned stored ")
return stored_json
@app.get("/getlogo/")
def get_file():
return FileResponse("vid-min.png", media_type='image/png', filename="vid-min.png")