-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
77 lines (61 loc) · 2.05 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
from fastapi import FastAPI, HTTPException
from starlette.middleware.gzip import GZipMiddleware
from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from starlette.responses import HTMLResponse
import uvicorn
from streamlink import NoPluginError, PluginError, Streamlink
import os
app = FastAPI()
origins = [
os.environ['VUE_APP_API_URL'],
"http://localhost",
"https://localhost",
"http://127.0.0.1",
"https://127.0.0.1",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
def read_root():
f = open("static/index.html", "r")
html_content = f.read()
return HTMLResponse(content=html_content, status_code=200)
@app.get("/streamlink")
def api(url: str):
streamlink = Streamlink()
try:
plugin = streamlink.resolve_url(url)
streams = plugin.streams()
except NoPluginError as e:
raise HTTPException(status_code=500, detail="Streamlink is unable to handle the URL '{0}'".format(url))
except PluginError as e:
if e.__str__().startswith('Unable to find channel'):
raise HTTPException(status_code=404, detail="Stream not found")
raise HTTPException(status_code=500, detail="Error while fetching stream - " + str(e))
except Exception:
raise HTTPException(status_code=500, detail="Error while fetching stream")
out = {}
for quality, stream in streams.items():
out[quality] = stream.url
live = False
if len(streams.items()) > 0:
live = True
try:
id = plugin.channel_id
except:
id = plugin.video_id
try:
channel = plugin.channel
except:
channel = None
return {'streams': out, 'url': url, 'channel': {'live': live, 'id': id, 'plugin': plugin.module, 'author': plugin.author, 'channel': channel, 'url': plugin.url, 'title': plugin.title}}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)