From 0505fee3312f4325dd05320316ea16be81263e0a Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Sat, 29 May 2021 10:33:10 +0200 Subject: [PATCH] reuse streamlink sessions --- backend/stream.py | 7 +++++++ backend/stream_providers.py | 20 ++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/backend/stream.py b/backend/stream.py index 3dfac48..370e919 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -111,6 +111,13 @@ for key in providers: for proxy in current: proxies[key].append(ProxyElem(proxy)) +proxy_keys = [] +for proxy_provider in proxies.values(): + for proxy in proxy_provider: + if isinstance(proxy, ProxyElem) and isinstance(proxy.proxy, str): + proxy_keys.append(proxy.proxy) +stream_providers.setup(proxy_keys) + class UpstreamHandler(): def __init__(self): self.provider = None diff --git a/backend/stream_providers.py b/backend/stream_providers.py index f8f5abc..d5ca056 100755 --- a/backend/stream_providers.py +++ b/backend/stream_providers.py @@ -5,7 +5,13 @@ import asyncio import html.parser import re -ytimg_pattern = re.compile(r'(https:\/\/[a-z0-9.]+ytimg\.com\/.+\/)[a-z0-9]+(\.[a-z0-9]+)') +streamlink_sessions = {} +streamlink_default_session = streamlink.Streamlink() +def setup(proxies): + for proxy in proxies: + streamlink_sessions[proxy] = streamlink.Streamlink() + streamlink_sessions[proxy].set_option("http-proxy", proxy) + streamlink_sessions[proxy].set_option("https-proxy", proxy) class DummyLogger(): def debug(self, msg): @@ -88,11 +94,13 @@ class StreamProvider(): class StreamlinkRunner(StreamProvider): def stream(self): try: - session = streamlink.Streamlink() - if self.proxy is not None: - session.set_option("https-proxy", self.proxy) - session.set_option("http-proxy", self.proxy) - streams = session.streams(self.upstream) + session = None + if self.proxy is None: + session = streamlink_default_session + else: + session = streamlink_sessions.get(self.proxy) + media = session.resolve_url(self.upstream) + streams = media.streams() if streams is not None: for key in reversed(streams): stream = streams.get(key)