stream-api/stream.py
2021-04-30 11:26:21 +02:00

35 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
import streamlink
import tornado.web
import tornado.routing
providers = {}
providers["nrk"] = "https://tv.nrk.no"
providers["svt"] = "https://svtplay.se"
providers["youtube"] = "https://youtu.be"
class MainHandler(tornado.web.RequestHandler):
def get(self):
provider = self.get_query_argument("provider", None)
if provider is not None and provider in providers:
endpoint = None
src = providers[provider] + self.request.uri
try:
stream = streamlink.streams(src).get("best")
if stream is not None:
endpoint = stream.url
except Exception as e:
self.write(str(e))
else:
if endpoint is None:
self.write("stream not found")
else:
self.redirect(endpoint, status=303)
try:
app_web = tornado.web.Application([(tornado.routing.AnyMatches(), MainHandler)])
app_web.listen(8080)
tornado.ioloop.IOLoop.current().start()
except KeyboardInterrupt:
print()