add provider cache

This commit is contained in:
Roy Olav Purser 2021-05-30 12:26:32 +02:00
parent 4f2501ed98
commit 0c32426d27
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -3,7 +3,7 @@ import streamlink
import requests import requests
import asyncio import asyncio
import html.parser import html.parser
import re import expiringdict
streamlink_sessions = {} streamlink_sessions = {}
streamlink_default_session = streamlink.Streamlink() streamlink_default_session = streamlink.Streamlink()
@ -76,6 +76,7 @@ class StreamData():
class StreamProvider(): class StreamProvider():
def __init__(self, upstream, proxy, logger): def __init__(self, upstream, proxy, logger):
self.name = self.__class__.__name__
self.upstream = upstream self.upstream = upstream
self.proxy = None self.proxy = None
self.logger = logger self.logger = logger
@ -166,38 +167,30 @@ class MetaRunner(StreamProvider):
self.logger.info(e) self.logger.info(e)
return StreamData(None, data.get("og:image"), data.get("og:title"), data.get("og:description"), False) return StreamData(None, data.get("og:image"), data.get("og:title"), data.get("og:description"), False)
async def get_ytdl(upstream, proxy, logger): upstream_cache = expiringdict.ExpiringDict(max_len=128, max_age_seconds=30)
async def get_from_runner(cache_key, runner, logger):
result = None result = None
cached = upstream_cache.get(cache_key)
if isinstance(cached, StreamData):
return cached
try: try:
runner = YoutubeRunner(upstream, proxy, logger)
result_temp = await runner.run() result_temp = await runner.run()
except Exception as e: except Exception as e:
logger.info(e) logger.info(e)
else: else:
upstream_cache[cache_key] = result_temp
result = result_temp result = result_temp
return result return result
async def get_streamlink(upstream, proxy, logger): async def get_streamlink(upstream, proxy, logger):
result = None return await get_from_runner((0, upstream), StreamlinkRunner(upstream, proxy, logger), logger)
try:
runner = StreamlinkRunner(upstream, proxy, logger) async def get_ytdl(upstream, proxy, logger):
result_temp = await runner.run() return await get_from_runner((1, upstream), YoutubeRunner(upstream, proxy, logger), logger)
except Exception as e:
logger.info(e)
else:
result = result_temp
return result
async def get_meta(upstream, proxy, logger): async def get_meta(upstream, proxy, logger):
result = None return await get_from_runner((2, upstream), MetaRunner(upstream, proxy, logger), logger)
try:
runner = MetaRunner(upstream, proxy, logger)
result_temp = await runner.run()
except Exception as e:
logger.info(e)
else:
result = result_temp
return result
async def get_any(upstream, proxy, logger): async def get_any(upstream, proxy, logger):
tasks = [] tasks = []