A complete gutting of the internals of ytdl-sub to support functions in our variable syntax, in addition to being able to access a yt-dlp entry's .info.json fields using functions. Functionally, ytdl-sub should still look and behave the same from a user-perspective. With so many lines of code changed (+8927, -2708), no doubt there will be new issues. Please make a GH issue or reach out on Discord if your config/subscriptions break in any way/shape/form. Details on how to use function support will come soon in the form of proper documentation in our readthedocs.
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
import json
|
|
from typing import Any
|
|
|
|
from ytdl_sub.script.types.array import Array
|
|
from ytdl_sub.script.types.map import Map
|
|
from ytdl_sub.script.types.resolvable import AnyArgument
|
|
from ytdl_sub.script.types.resolvable import Boolean
|
|
from ytdl_sub.script.types.resolvable import Float
|
|
from ytdl_sub.script.types.resolvable import Integer
|
|
from ytdl_sub.script.types.resolvable import Resolvable
|
|
from ytdl_sub.script.types.resolvable import String
|
|
from ytdl_sub.script.utils.exceptions import UNREACHABLE
|
|
|
|
|
|
def _from_json(out: Any) -> Resolvable:
|
|
# pylint: disable=too-many-return-statements
|
|
if out is None:
|
|
return String("")
|
|
if isinstance(out, int):
|
|
return Integer(out)
|
|
if isinstance(out, float):
|
|
return Float(out)
|
|
if isinstance(out, str):
|
|
return String(out)
|
|
if isinstance(out, bool):
|
|
return Boolean(out)
|
|
if isinstance(out, list):
|
|
return Array(value=[_from_json(arg) for arg in out])
|
|
if isinstance(out, dict):
|
|
return Map(value={_from_json(key): _from_json(value) for key, value in out.items()})
|
|
raise UNREACHABLE
|
|
|
|
|
|
class JsonFunctions:
|
|
@staticmethod
|
|
def from_json(argument: String) -> AnyArgument:
|
|
"""
|
|
Converts a JSON string into an actual type.
|
|
"""
|
|
return _from_json(json.loads(argument.value))
|