From 6650cd18181ff09e4cc4d84bd74a6fb13ccb0dce Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Fri, 30 Apr 2021 13:55:10 +0200 Subject: [PATCH] add proxy support --- stream.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/stream.py b/stream.py index 7795e01..4d9dcd3 100755 --- a/stream.py +++ b/stream.py @@ -3,6 +3,7 @@ import streamlink import tornado.web import tornado.routing import requests +import os providers = {} providers["nrk"] = "https://tv.nrk.no" @@ -10,11 +11,24 @@ providers["svt"] = "https://svtplay.se" providers["youtube"] = "https://youtu.be" providers["twitch"] = "https://twitch.tv" +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) + class MainHandler(tornado.web.RequestHandler): def get(self): provider = self.get_query_argument("provider", None) endpoint = None if provider is not None and provider in providers.keys(): + proxy = proxies.get(provider) src = providers[provider] + self.request.uri try: resp = requests.get(src) @@ -25,7 +39,7 @@ class MainHandler(tornado.web.RequestHandler): return else: try: - streams = streamlink.streams(src) + streams = proxy.streams(src) for key in reversed(streams): stream = streams.get(key) if hasattr(stream, "url"):