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 asyncio
import html.parser
import re
import expiringdict
streamlink_sessions = {}
streamlink_default_session = streamlink.Streamlink()
@ -76,6 +76,7 @@ class StreamData():
class StreamProvider():
def __init__(self, upstream, proxy, logger):
self.name = self.__class__.__name__
self.upstream = upstream
self.proxy = None
self.logger = logger
@ -166,38 +167,30 @@ class MetaRunner(StreamProvider):
self.logger.info(e)
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
cached = upstream_cache.get(cache_key)
if isinstance(cached, StreamData):
return cached
try:
runner = YoutubeRunner(upstream, proxy, logger)
result_temp = await runner.run()
except Exception as e:
logger.info(e)
else:
upstream_cache[cache_key] = result_temp
result = result_temp
return result
async def get_streamlink(upstream, proxy, logger):
result = None
try:
runner = StreamlinkRunner(upstream, proxy, logger)
result_temp = await runner.run()
except Exception as e:
logger.info(e)
else:
result = result_temp
return result
return await get_from_runner((0, upstream), StreamlinkRunner(upstream, proxy, logger), logger)
async def get_ytdl(upstream, proxy, logger):
return await get_from_runner((1, upstream), YoutubeRunner(upstream, proxy, logger), logger)
async def get_meta(upstream, proxy, logger):
result = None
try:
runner = MetaRunner(upstream, proxy, logger)
result_temp = await runner.run()
except Exception as e:
logger.info(e)
else:
result = result_temp
return result
return await get_from_runner((2, upstream), MetaRunner(upstream, proxy, logger), logger)
async def get_any(upstream, proxy, logger):
tasks = []