v6d3music/v6d3music/utils/entries_effects_for_args.py
2022-11-20 23:28:27 +00:00

62 lines
2.1 KiB
Python

from typing import AsyncIterable, Optional
from ptvp35 import Db, KVJson
from v6d2ctx.context import Explicit
from v6d3music.config import myroot
from v6d3music.utils.effects_for_preset import effects_for_preset
from v6d3music.utils.entries_for_url import entries_for_url
from v6d3music.utils.info_tuple import info_tuple
from v6d3music.utils.options_for_effects import options_for_effects
from v6d3music.utils.presets import allowed_effects
from v6d3music.utils.sparq import sparq
effects_db = Db(myroot / 'effects.db', kvfactory=KVJson())
def default_effects(gid: int) -> Optional[str]:
effects = effects_db.get(gid, None)
if effects in allowed_effects:
return effects
else:
return None
async def set_default_effects(gid: int, effects: Optional[str]) -> None:
if effects is not None and effects not in allowed_effects:
raise Explicit('these effects are not allowed')
await effects_db.set(gid, effects)
async def entries_effects_for_args(args: list[str], gid: int) -> AsyncIterable[info_tuple]:
while args:
match args:
case [url, '-', effects, *args]:
pass
case [url, '+', preset, *args]:
effects = effects_for_preset(preset)
case [url, *args]:
effects = default_effects(gid)
case _:
raise RuntimeError
seconds = 0
match args:
case [h, m, s, *args] if h.isdecimal() and m.isdecimal() and s.isdecimal():
seconds = 3600 * int(h) + 60 * int(m) + int(s)
case [m, s, *args] if m.isdecimal() and s.isdecimal():
seconds = 60 * int(m) + int(s)
case [s, *args] if s.isdecimal():
seconds = int(s)
case [*args]:
pass
already_read = round(seconds / sparq(options_for_effects(effects)))
tor = False
match args:
case ['tor', *args]:
tor = True
case [*args]:
pass
async for info in entries_for_url(url, tor):
yield info, effects, already_read, tor