follow redirect

This commit is contained in:
Roy Olav Purser 2021-04-30 13:26:55 +02:00
parent 76a2947e7e
commit bbca0d2fd7
No known key found for this signature in database
GPG Key ID: 0BA77797F072BC52

View File

@ -2,29 +2,42 @@
import streamlink
import tornado.web
import tornado.routing
import requests
providers = {}
providers["nrk"] = "https://tv.nrk.no"
providers["svt"] = "https://svtplay.se"
providers["youtube"] = "https://youtu.be"
providers["twitch"] = "https://twitch.tv"
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
endpoint = None
if provider is not None and provider in providers.keys():
src = providers[provider] + self.request.uri
try:
stream = streamlink.streams(src).get("best")
if stream is not None:
endpoint = stream.url
resp = requests.get(src)
if resp is not None:
src = resp.url
except Exception as e:
self.write(str(e))
self.write(e)
return
else:
if endpoint is None:
self.write("stream not found")
else:
self.redirect(endpoint, status=303)
try:
streams = streamlink.streams(src)
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)
try:
app_web = tornado.web.Application([(tornado.routing.AnyMatches(), MainHandler)])
app_web.listen(8080)