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-04-30 11:55:10 +00:00
|
|
|
import os
|
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 = {}
|
|
|
|
proxies["nrk"] = streamlink.Streamlink()
|
|
|
|
proxies["svt"] = streamlink.Streamlink()
|
|
|
|
proxies["youtube"] = streamlink.Streamlink()
|
|
|
|
proxies["twitch"] = streamlink.Streamlink()
|
|
|
|
|
|
|
|
for key in proxies:
|
|
|
|
proxy = os.environ.get(key.upper() + "_PROXY")
|
|
|
|
proxies[key].set_option("http-timeout", 2.0)
|
|
|
|
if proxy is not None:
|
|
|
|
proxies[key].set_option("https-proxy", proxy)
|
|
|
|
|
2021-04-30 08:49:10 +00:00
|
|
|
class MainHandler(tornado.web.RequestHandler):
|
|
|
|
def get(self):
|
|
|
|
provider = self.get_query_argument("provider", None)
|
2021-04-30 11:26:55 +00:00
|
|
|
endpoint = None
|
|
|
|
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-04-30 11:26:55 +00:00
|
|
|
self.write(e)
|
|
|
|
return
|
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"):
|
|
|
|
endpoint = stream.url
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
self.write(str(e))
|
|
|
|
return
|
|
|
|
if endpoint is None:
|
|
|
|
self.write("stream not found")
|
|
|
|
else:
|
|
|
|
self.redirect(endpoint, status=303)
|
2021-04-30 08:49:10 +00:00
|
|
|
try:
|
|
|
|
app_web = tornado.web.Application([(tornado.routing.AnyMatches(), MainHandler)])
|
|
|
|
app_web.listen(8080)
|
|
|
|
tornado.ioloop.IOLoop.current().start()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print()
|
|
|
|
|