dropin

alternative frontend for dropout.tv
git clone https://git.ce9e.org/dropin.git

commit
1aaaa2a32003599e5e6ea81c05bc49b99ba498b1
parent
6b688a2690d62035d70660015cd6be423fab71ed
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-02-07 10:50
invalidate cache after 1 hour

Diffstat

M dropin.py 39 ++++++++++++++++++---------------------

1 files changed, 18 insertions, 21 deletions


diff --git a/dropin.py b/dropin.py

@@ -15,6 +15,7 @@ from aiohttp import web
   15    15 
   16    16 BASE_DIR = Path(__file__).parent
   17    17 SITE_ID = 36348
   -1    18 CACHE_DURATION = 3600
   18    19 FORMATS = [
   19    20     ('m3u8', 'application/vnd.apple.mpegurl', 'adaptive'),
   20    21     ('mpd', 'application/dash+xml', 'adaptive'),
@@ -120,28 +121,24 @@ auth = Auth(os.getenv('DROPOUT_EMAIL'), os.getenv('DROPOUT_PASSWORD'))
  120   121 unauth = Auth()
  121   122 
  122   123 
  123    -1 def async_cache(maxsize=128):
   -1   124 def async_cache(func):
  124   125     cache = {}
  125    -1     def decorator(func):
  126    -1         async def wrapper(*args, **kwargs):
  127    -1             key = functools._make_key(args, kwargs, typed=False)
  128    -1             if key not in cache:
  129    -1                 future = asyncio.Future()
  130    -1                 if len(cache) >= maxsize:
  131    -1                     del cache[next(iter(cache))]
  132    -1                 cache[key] = future
  133    -1                 try:
  134    -1                     future.set_result(await func(*args, **kwargs))
  135    -1                 except Exception as e:
  136    -1                     future.set_exception(e)
  137    -1                     del cache[key]
  138    -1                     raise
  139    -1             return await cache[key]
  140    -1         return wrapper
  141    -1     return decorator
  142    -1 
  143    -1 
  144    -1 @async_cache()
   -1   126     async def wrapper(*args, **kwargs):
   -1   127         key = functools._make_key(args, kwargs, typed=False)
   -1   128         if key not in cache or cache[key][0] < time.time():
   -1   129             future = asyncio.Future()
   -1   130             cache[key] = (time.time() + CACHE_DURATION, future)
   -1   131             try:
   -1   132                 future.set_result(await func(*args, **kwargs))
   -1   133             except Exception as e:
   -1   134                 future.set_exception(e)
   -1   135                 del cache[key]
   -1   136                 raise
   -1   137         return await cache[key][1]
   -1   138     return wrapper
   -1   139 
   -1   140 
   -1   141 @async_cache
  145   142 async def fetch(url, *, need_auth=False, **params):
  146   143     print('fetch', url)
  147   144