forked from BellezaEmporium/query-streamlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
118 lines (97 loc) · 3.36 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
#!/usr/bin/env python
from flask import Flask, request, redirect, send_file
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import validators
from api import Fetch
app = Flask(__name__)
limiter = Limiter(
app,
key_func=get_remote_address
)
def make_m3u8(output):
"""Creates m3u file and string
(output: dict, query: str)
"""
speeds = {
1000: 5_000_000,
700: 2_500_000,
400: 1_100_000,
300: 700_000,
200: 400_000,
100: 200_000,
}
link_str = "#EXTM3U\n"
for res in output:
r = res.split("p")[0]
if type(r) == int:
for speed in speeds:
if r >= speed:
bandwidth = speeds[speed]
break
else:
bandwidth = 100_000
title = (
f"#EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,BANDWIDTH={bandwidth},NAME={res}\n"
)
link = f"{output[res]}\n"
link_str += title + link
with open("stream.m3u8", "w") as f:
f.write(link_str)
return link_str
def api_formatted(output, api):
"""Formats the output to json if the endpoint is /api"""
if api:
if type(output) == dict:
return output
return {"Error": output}
if type(output) == dict:
if len(output) == 1:
return next(iter(output.values()))
return make_m3u8(output)
return output
def query_handler(args, api):
"""Checks and tests arguments before serving request"""
if args:
query = args.get("streaming-ip")
if not query:
message = "streaming-ip string is empty"
return api_formatted(message, api)
valid = validators.url(query)
if not valid:
message = "The URL you've entered is not valid."
return api_formatted(message, api)
quality = args.get("quality")
if quality == "":
message = "Empty quality string"
return api_formatted(message, api)
stream_obj = Fetch(query, quality)
streams = stream_obj.filtered_streams()
return api_formatted(streams, api)
else:
message = "No queries provided. Nothing to do."
return api_formatted(message, api)
@app.route("/", methods=['GET'])
def index():
return "This program permits you to get direct access to streams by using Streamlink.\nIf you have a link that needs to be treated, from this webpage, add /iptv-query?streaming-ip= *your URL*.\nNote that it will work only on Streamlink-supported websites.\nEnjoy ! LaneSh4d0w. Special thanks to Keystroke for the API usage."
@app.route("/iptv-query", methods=['GET'])
@limiter.limit("20/minute")
@limiter.limit("1/second")
def home():
response = query_handler(request.args, False)
if response.startswith("#EXTM3U"):
return send_file("stream.m3u8")
elif response.startswith("http"):
return redirect(response)
else:
return response
@app.route("/api", methods=['GET'])
@limiter.limit("20/minute")
@limiter.limit("1/second")
def api():
return query_handler(request.args, True)
@app.errorhandler(429)
def ratelimit_handler(e):
return "Whoa there ! I know you like that service, but there's no need to spam me ! Let the server breathe a little bit (RATE LIMIT EXCEEDED)"
if __name__ == '__main__':
app.run(threaded=False, port=5000)