-
Notifications
You must be signed in to change notification settings - Fork 380
/
main.py
72 lines (62 loc) · 2.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
#!/usr/bin/env python
from flask import Flask, redirect, request
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from validators import url
import validators
from api import get_streams
app = Flask(__name__)
# Create a rate limiter to control the number of requests per hour from each IP address.
limiter = Limiter(
get_remote_address,
app=app,
default_limits=["100 per hour"],
storage_uri="memory://"
)
# Reads the URL parameters and redirects to Streamlink.
def query_handler(args):
streaming_ip = args.get("url")
provider = args.get("provider")
quality = args.get("quality")
proxy = args.get("proxy") # Get the proxy parameter
if not streaming_ip:
return "You didn't provide any URL."
if not quality:
quality = "best"
if validators.url(streaming_ip):
if provider:
return get_streams(streaming_ip + "&provider=" + provider, quality, proxy)
else:
return get_streams(streaming_ip, quality, proxy)
else:
return "The URL you entered is not valid."
# Presentation page
@app.route("/", methods=['GET'])
def index():
return """
This program allows you to access streams directly using Streamlink.
To process a link, append '/iptv-query?streaming-ip=*your URL*' to this webpage.
Please note that it only works with Streamlink-supported websites.
Enjoy! BellezaEmporium. Special thanks to Keystroke for the API usage.
"""
# iptv-query route -> provides a link to Streamlink, analyzes the link
# for correct plugin routing, and redirects (or displays) the stream link.
@app.route("/iptv-query", methods=['GET'])
@limiter.limit("20/minute")
def home():
no_redirect = request.args.get("no_redirect")
"""Handles the IPTV query request and redirects to the stream link"""
try:
response = query_handler(request.args)
except Exception as e:
return f"An error occurred: {str(e)}"
if not url(response):
return response
return redirect(response) if not no_redirect else response
# Rate limiting system.
@app.errorhandler(429)
def ratelimit_handler(e):
"""Handles rate limit exceeded error"""
return "{}. To ensure fair access to the program, we are limiting the number of requests.".format(e)
if __name__ == '__main__':
app.run(threaded=False, port=5000)