diff --git a/backend/stream_providers.py b/backend/stream_providers.py index a8c499e..a16e025 100755 --- a/backend/stream_providers.py +++ b/backend/stream_providers.py @@ -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 = []