reuse streamlink sessions

This commit is contained in:
Roy Olav Purser 2021-05-29 10:33:10 +02:00
parent 278de926e3
commit 0505fee331
Signed by: roypur
GPG Key ID: E14D26A036F21656
2 changed files with 21 additions and 6 deletions

View File

@ -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

View File

@ -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)