stream-api/stream.py

137 lines
4.4 KiB
Python
Raw Normal View History

2021-04-30 08:49:10 +00:00
#!/usr/bin/env python3
import streamlink
import tornado.web
import tornado.routing
2021-04-30 11:26:55 +00:00
import requests
2021-05-06 13:23:43 +00:00
import json
2021-04-30 11:55:10 +00:00
import os
2021-05-08 17:08:54 +00:00
import urllib.parse
2021-04-30 08:49:10 +00:00
providers = {}
providers["nrk"] = "https://tv.nrk.no"
providers["svt"] = "https://svtplay.se"
providers["youtube"] = "https://youtu.be"
2021-04-30 11:26:55 +00:00
providers["twitch"] = "https://twitch.tv"
2021-04-30 08:49:10 +00:00
2021-04-30 11:55:10 +00:00
proxies = {}
2021-05-08 17:08:54 +00:00
proxies_raw = {}
2021-05-09 09:37:19 +00:00
proxies_req = {}
2021-04-30 11:55:10 +00:00
2021-05-08 17:08:54 +00:00
for key in providers:
proxies[key] = streamlink.Streamlink()
2021-04-30 11:55:10 +00:00
proxy = os.environ.get(key.upper() + "_PROXY")
proxies[key].set_option("http-timeout", 2.0)
if proxy is not None:
2021-05-09 09:37:19 +00:00
proxies[key].set_option("https-proxy", "socks5://" + proxy)
proxies_raw[key] = proxy
proxies_req[key] = {}
proxies_req[key]["http"] = "socks5://" + proxy
proxies_req[key]["https"] = "socks5://" + proxy
2021-04-30 11:55:10 +00:00
2021-05-06 13:23:43 +00:00
playlist = None
icecast_server = os.environ.get("ICECAST_SERVER")
stream_server = os.environ.get("STREAM_SERVER")
2021-05-08 17:08:54 +00:00
proxy_server = os.environ.get("PROXY_SERVER")
2021-05-06 13:54:18 +00:00
2021-05-06 13:23:43 +00:00
if icecast_server is not None and stream_server is not None:
with open("/app/sources.json", "r") as f:
data = json.loads(f.read())
playlist = "#EXTM3U\n"
for key in data:
current = data[key]
name = current["name"]
radio = current["radio"]
if radio:
playlist += f'#EXTINF:0 radio="true", {name}\n'
playlist += icecast_server + key + "\n"
else:
playlist += f'#EXTINF:0 radio="false", {name}\n'
playlist += stream_server + key + "\n"
2021-05-09 09:37:19 +00:00
def rewrite(current, provider):
proxy_req = proxies_req.get(provider)
2021-05-09 09:39:37 +00:00
proxy = proxies_raw.get(provider)
2021-05-09 09:37:19 +00:00
resp = requests.head(current, proxies=proxy_req)
2021-05-09 09:14:45 +00:00
ctype = resp.headers.get("Content-Type")
2021-05-09 07:53:37 +00:00
print(ctype)
2021-05-09 09:14:45 +00:00
if ctype is None:
2021-05-08 22:49:52 +00:00
return None
2021-05-09 09:14:45 +00:00
else:
if "mpegurl" not in ctype.lower():
return None
2021-05-09 09:49:09 +00:00
resp = requests.get(current, proxies=proxy_req)
2021-05-08 22:49:52 +00:00
ndata = None
if resp.text is not None:
2021-05-08 17:08:54 +00:00
ndata = ""
for line in resp.text.splitlines():
if line.startswith("#"):
ndata += line
else:
data = {}
data["upstream"] = urllib.parse.urljoin(current, line)
data["proxy"] = proxy
2021-05-08 22:49:52 +00:00
if proxy is None or proxy_server is None:
ndata += data["upstream"]
else:
presp = requests.post(proxy_server, json=data)
ndata += presp.text
2021-05-08 17:08:54 +00:00
ndata += "\n"
2021-05-08 22:49:52 +00:00
return ndata
2021-05-08 17:08:54 +00:00
2021-04-30 08:49:10 +00:00
class MainHandler(tornado.web.RequestHandler):
2021-05-06 13:54:18 +00:00
def handle_any(self, write):
2021-04-30 08:49:10 +00:00
provider = self.get_query_argument("provider", None)
2021-05-08 17:12:21 +00:00
upstream = None
2021-05-08 17:08:54 +00:00
proxy = None
2021-04-30 11:26:55 +00:00
if provider is not None and provider in providers.keys():
2021-04-30 11:55:10 +00:00
proxy = proxies.get(provider)
2021-04-30 08:49:10 +00:00
src = providers[provider] + self.request.uri
try:
2021-04-30 11:26:55 +00:00
resp = requests.get(src)
if resp is not None:
src = resp.url
2021-04-30 08:49:10 +00:00
except Exception as e:
2021-05-01 18:01:24 +00:00
print(e)
2021-04-30 08:49:10 +00:00
else:
2021-04-30 11:26:55 +00:00
try:
2021-04-30 11:55:10 +00:00
streams = proxy.streams(src)
2021-04-30 11:26:55 +00:00
for key in reversed(streams):
stream = streams.get(key)
if hasattr(stream, "url"):
2021-05-08 17:12:21 +00:00
upstream = stream.url
2021-04-30 11:26:55 +00:00
break
except Exception as e:
2021-05-01 18:01:24 +00:00
print(str(e))
2021-05-08 17:12:21 +00:00
upstream = None
if upstream is None:
2021-05-06 13:54:18 +00:00
self.set_status(404)
if write:
self.write("stream not found")
2021-04-30 11:26:55 +00:00
else:
2021-05-09 09:37:19 +00:00
data = rewrite(upstream, provider)
2021-05-08 22:49:52 +00:00
if data is None:
self.redirect(upstream, status=303)
else:
self.set_header("Content-Type", "application/vnd.apple.mpegurl")
self.write(data)
2021-04-30 15:51:09 +00:00
def get(self):
2021-05-06 13:54:18 +00:00
self.handle_any(True)
2021-04-30 15:51:09 +00:00
def head(self):
2021-05-06 13:54:18 +00:00
self.handle_any(False)
2021-05-01 18:01:24 +00:00
class FileHandler(tornado.web.RequestHandler):
def get(self):
self.set_header("Content-Type", "text/plain; charset=utf-8")
2021-05-06 13:23:43 +00:00
self.write(playlist)
2021-04-30 08:49:10 +00:00
try:
2021-05-01 18:01:24 +00:00
handlers = []
handlers.append((tornado.routing.PathMatches("/sources.m3u8"), FileHandler))
handlers.append((tornado.routing.AnyMatches(), MainHandler))
app_web = tornado.web.Application(handlers)
2021-04-30 08:49:10 +00:00
app_web.listen(8080)
tornado.ioloop.IOLoop.current().start()
except KeyboardInterrupt:
print()