From 97c2a66fbd467eef3560dcdaebf47d482979a04b Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Wed, 6 Dec 2023 16:47:42 -0800 Subject: [PATCH] all except one --- .../entries/script/function_scripts.py | 2 +- .../entries/script/variable_definitions.py | 437 ++++++++++-------- .../entries/script/variable_scripts.py | 207 +++++---- .../entries/variables/entry_variables.py | 2 +- 4 files changed, 369 insertions(+), 279 deletions(-) diff --git a/src/ytdl_sub/entries/script/function_scripts.py b/src/ytdl_sub/entries/script/function_scripts.py index de0990bc..da26fe5f 100644 --- a/src/ytdl_sub/entries/script/function_scripts.py +++ b/src/ytdl_sub/entries/script/function_scripts.py @@ -48,7 +48,7 @@ class CustomFunctions: return String(out) @staticmethod - def date_metadata(yyyymmdd: String) -> Map: + def to_date_metadata(yyyymmdd: String) -> Map: date_str = yyyymmdd.value if not (date_str.isnumeric() and len(date_str) == 6): raise RuntimeException( diff --git a/src/ytdl_sub/entries/script/variable_definitions.py b/src/ytdl_sub/entries/script/variable_definitions.py index cd70b322..25471d20 100644 --- a/src/ytdl_sub/entries/script/variable_definitions.py +++ b/src/ytdl_sub/entries/script/variable_definitions.py @@ -7,38 +7,73 @@ from typing import Optional @dataclass(frozen=True) -class KwargKey: - entry_key: str +class Variable: variable_name: str - @classmethod - def init(cls, entry_key: str, variable_name: Optional[str] = None) -> "KwargKey": - return cls(entry_key=entry_key, variable_name=variable_name if variable_name else entry_key) + +@dataclass(frozen=True) +class Metadata(Variable): + pass -class Variables: +@dataclass(frozen=True) +class MetadataVariable(Variable): + metadata_key: str + + +class _Variables: @property - def uid(self) -> KwargKey: + def entry_metadata(self) -> Metadata: + """ + Returns + ------- + dict + The entry's info.json in dict form + """ + return Metadata("entry_metadata") + + @property + def playlist_metadata(self) -> Metadata: + """ + Returns + ------- + dict + The playlist's info.json in dict form + """ + return Metadata("playlist_metadata") + + @property + def source_metadata(self) -> Metadata: + """ + Returns + ------- + dict + The source's info.json in dict form + """ + return Metadata("source_metadata") + + @property + def uid(self) -> MetadataVariable: """ Returns ------- str The entry's unique ID """ - return KwargKey.init(entry_key="id", variable_name="uid") + return MetadataVariable(metadata_key="id", variable_name="uid") @property - def uid_sanitized(self) -> KwargKey: + def uid_sanitized(self) -> Variable: """ Returns ------- str The sanitized uid of the entry, which is safe to use for Unix and Windows file names. """ - return KwargKey.init("uid_sanitized") + return Variable("uid_sanitized") @property - def uid_sanitized_plex(self) -> KwargKey: + def uid_sanitized_plex(self) -> Variable: """ Returns ------- @@ -46,80 +81,80 @@ class Variables: The sanitized uid with additional sanitizing for Plex. Replaces numbers with fixed-width numbers so Plex does not recognize them as season or episode numbers. """ - return KwargKey.init("uid_sanitized_plex") + return Variable("uid_sanitized_plex") @property - def ie_key(self) -> KwargKey: + def ie_key(self) -> MetadataVariable: """ Returns ------- str The info-extractor key """ - return KwargKey.init("ie_key") + return MetadataVariable(metadata_key="ie_key", variable_name="ie_key") @property - def extractor(self) -> KwargKey: + def extractor(self) -> MetadataVariable: """ Returns ------- str The ytdl extractor name """ - return KwargKey.init("extractor") + return MetadataVariable(variable_name="extractor", metadata_key="extractor") @property - def epoch(self) -> KwargKey: + def epoch(self) -> MetadataVariable: """ Returns ------- int The unix epoch of when the metadata was scraped by yt-dlp. """ - return KwargKey.init("epoch") + return MetadataVariable(metadata_key="epoch", variable_name="epoch") @property - def epoch_date(self) -> KwargKey: + def epoch_date(self) -> Variable: """ Returns ------- str The epoch's date, in YYYYMMDD format. """ - return KwargKey.init("epoch_date") + return Variable("epoch_date") @property - def epoch_hour(self) -> KwargKey: + def epoch_hour(self) -> Variable: """ Returns ------- str The epoch's hour, padded """ - return KwargKey.init("epoch_hour") + return Variable("epoch_hour") @property - def title(self) -> KwargKey: + def title(self) -> MetadataVariable: """ Returns ------- str The title of the entry. If a title does not exist, returns its unique ID. """ - return KwargKey.init("title") + return MetadataVariable(variable_name="title", metadata_key="title") @property - def title_sanitized(self) -> KwargKey: + def title_sanitized(self) -> Variable: """ Returns ------- str The sanitized title of the entry, which is safe to use for Unix and Windows file names. """ - return KwargKey.init("title_sanitized") + return Variable("title_sanitized") @property - def title_sanitized_plex(self) -> KwargKey: + def title_sanitized_plex(self) -> Variable: """ Returns ------- @@ -127,70 +162,70 @@ class Variables: The sanitized title with additional sanitizing for Plex. Replaces numbers with fixed-width numbers so Plex does not recognize them as season or episode numbers. """ - return KwargKey.init("title_sanitized_plex") + return Variable("title_sanitized_plex") @property - def webpage_url(self) -> KwargKey: + def webpage_url(self) -> MetadataVariable: """ Returns ------- str The url to the webpage. """ - return KwargKey.init("webpage_url") + return MetadataVariable(metadata_key="webpage_url", variable_name="webpage_url") @property - def info_json_ext(self) -> KwargKey: + def info_json_ext(self) -> Variable: """ Returns ------- str The "info.json" extension """ - return KwargKey.init("info_json_ext") + return Variable("info_json_ext") @property - def description(self) -> KwargKey: + def description(self) -> MetadataVariable: """ Returns ------- str The description if it exists. Otherwise, returns an emtpy string. """ - return KwargKey.init("description") + return MetadataVariable(variable_name="description", metadata_key="description") @property - def uploader_id(self) -> KwargKey: + def uploader_id(self) -> MetadataVariable: """ Returns ------- str The uploader id if it exists, otherwise return the unique ID. """ - return KwargKey.init("uploader_id") + return MetadataVariable(variable_name="uploader_id", metadata_key="uploader_id") @property - def uploader(self) -> KwargKey: + def uploader(self) -> MetadataVariable: """ Returns ------- str The uploader if it exists, otherwise return the uploader ID. """ - return KwargKey.init("uploader") + return MetadataVariable(variable_name="uploader", metadata_key="uploader") @property - def uploader_url(self) -> KwargKey: + def uploader_url(self) -> MetadataVariable: """ Returns ------- str The uploader url if it exists, otherwise returns the webpage_url. """ - return KwargKey.init("uploader_url") + return MetadataVariable(variable_name="uploader_url", metadata_key="uploader_url") @property - def source_title(self) -> KwargKey: + def source_title(self) -> Variable: """ Returns ------- @@ -198,30 +233,30 @@ class Variables: Name of the source (i.e. channel with multiple playlists) if it exists, otherwise returns its playlist_title. """ - return KwargKey.init("source_title") + return Variable("source_title") @property - def source_title_sanitized(self) -> KwargKey: + def source_title_sanitized(self) -> Variable: """ Returns ------- str The source title, sanitized """ - return KwargKey.init("source_title_sanitized") + return Variable("source_title_sanitized") @property - def source_uid(self) -> KwargKey: + def source_uid(self) -> Variable: """ Returns ------- str The source unique id if it exists, otherwise returns the playlist unique ID. """ - return KwargKey.init("source_uid") + return Variable("source_uid") @property - def source_index(self) -> KwargKey: + def source_index(self) -> Variable: """ Returns ------- @@ -231,80 +266,80 @@ class Variables: It is recommended to not use this unless you know the source will never add new content (it is easy for this value to change). """ - return KwargKey.init("source_index") + return Variable("source_index") @property - def source_index_padded(self) -> KwargKey: + def source_index_padded(self) -> Variable: """ Returns ------- int The source index, padded. """ - return KwargKey.init("source_index_padded") + return Variable("source_index_padded") @property - def source_count(self) -> KwargKey: + def source_count(self) -> Variable: """ Returns ------- int The source count if it exists, otherwise returns the playlist count. """ - return KwargKey.init("source_count") + return Variable("source_count") @property - def source_webpage_url(self) -> KwargKey: + def source_webpage_url(self) -> Variable: """ Returns ------- str The source webpage url if it exists, otherwise returns the playlist webpage url. """ - return KwargKey.init("source_webpage_url") + return Variable("source_webpage_url") @property - def source_description(self) -> KwargKey: + def source_description(self) -> Variable: """ Returns ------- str The source description if it exists, otherwise returns the playlist description. """ - return KwargKey.init("source_description") + return Variable("source_description") @property - def playlist_uid(self) -> KwargKey: + def playlist_uid(self) -> MetadataVariable: """ Returns ------- str - The playlist unique ID if it exists, otherwise return KwargKey.init("") + The playlist unique ID if it exists, otherwise return Variable("") """ - return KwargKey.init("playlist_uid") + return MetadataVariable(variable_name="playlist_uid", metadata_key="id") @property - def playlist_title(self) -> KwargKey: + def playlist_title(self) -> MetadataVariable: """ Returns ------- str Name of its parent playlist/channel if it exists, otherwise returns its title. """ - return KwargKey.init("playlist_title") + return MetadataVariable(variable_name="playlist_title", metadata_key="title") @property - def playlist_title_sanitized(self) -> KwargKey: + def playlist_title_sanitized(self) -> Variable: """ Returns ------- str The playlist name, sanitized """ - return KwargKey.init("playlist_title_sanitized") + return Variable("playlist_title_sanitized") @property - def playlist_index(self) -> KwargKey: + def playlist_index(self) -> MetadataVariable: """ Returns ------- @@ -314,60 +349,60 @@ class Variables: Note that for channels/playlists, any change (i.e. adding or removing a video) will make this value change. Use with caution. """ - return KwargKey.init("playlist_index") + return MetadataVariable(metadata_key="playlist_index", variable_name="playlist_index") @property - def playlist_index_reversed(self) -> KwargKey: + def playlist_index_reversed(self) -> Variable: """ Returns ------- int Playlist index reversed via ``playlist_count - playlist_index + 1`` """ - return KwargKey.init("playlist_index_reversed") + return Variable("playlist_index_reversed") @property - def playlist_index_padded(self) -> KwargKey: + def playlist_index_padded(self) -> Variable: """ Returns ------- str playlist_index padded two digits """ - return KwargKey.init("playlist_index_padded") + return Variable("playlist_index_padded") @property - def playlist_index_reversed_padded(self) -> KwargKey: + def playlist_index_reversed_padded(self) -> Variable: """ Returns ------- str playlist_index_reversed padded two digits """ - return KwargKey.init("playlist_index_reversed_padded") + return Variable("playlist_index_reversed_padded") @property - def playlist_index_padded6(self) -> KwargKey: + def playlist_index_padded6(self) -> Variable: """ Returns ------- str playlist_index padded six digits. """ - return KwargKey.init("playlist_index_padded6") + return Variable("playlist_index_padded6") @property - def playlist_index_reversed_padded6(self) -> KwargKey: + def playlist_index_reversed_padded6(self) -> Variable: """ Returns ------- str playlist_index_reversed padded six digits. """ - return KwargKey.init("playlist_index_reversed_padded6") + return Variable("playlist_index_reversed_padded6") @property - def playlist_count(self) -> KwargKey: + def playlist_count(self) -> MetadataVariable: """ Returns ------- @@ -377,30 +412,30 @@ class Variables: Note that for channels/playlists, any change (i.e. adding or removing a video) will make this value change. Use with caution. """ - return KwargKey.init("playlist_count") + return MetadataVariable(variable_name="playlist_count", metadata_key="playlist_count") @property - def playlist_description(self) -> KwargKey: + def playlist_description(self) -> MetadataVariable: """ Returns ------- str The playlist description if it exists, otherwise returns the entry's description. """ - return KwargKey.init("playlist_description") + return MetadataVariable(variable_name="playlist_description", metadata_key="description") @property - def playlist_webpage_url(self) -> KwargKey: + def playlist_webpage_url(self) -> MetadataVariable: """ Returns ------- str The playlist webpage url if it exists. Otherwise, returns the entry webpage url. """ - return KwargKey.init("playlist_webpage_url") + return MetadataVariable(variable_name="playlist_webpage_url", metadata_key="webpage_url") @property - def playlist_max_upload_year(self) -> KwargKey: + def playlist_max_upload_year(self) -> MetadataVariable: """ Returns ------- @@ -409,10 +444,12 @@ class Variables: ``upload_year`` """ # override in EntryParent - return KwargKey.init("playlist_max_upload_year") + return MetadataVariable( + variable_name="playlist_max_upload_year", metadata_key="playlist_max_upload_year" + ) @property - def playlist_max_upload_year_truncated(self) -> KwargKey: + def playlist_max_upload_year_truncated(self) -> Variable: """ Returns ------- @@ -420,151 +457,151 @@ class Variables: The max playlist truncated upload year for all entries in this entry's playlist if it exists, otherwise returns ``upload_year_truncated``. """ - return KwargKey.init("playlist_max_upload_year_truncated") + return Variable("playlist_max_upload_year_truncated") @property - def playlist_uploader_id(self) -> KwargKey: + def playlist_uploader_id(self) -> MetadataVariable: """ Returns ------- str The playlist uploader id if it exists, otherwise returns the entry uploader ID. """ - return KwargKey.init("playlist_uploader_id") + return MetadataVariable("playlist_uploader_id", metadata_key="uploader_id") @property - def playlist_uploader(self) -> KwargKey: + def playlist_uploader(self) -> MetadataVariable: """ Returns ------- str - The playlist uploader if it exists, otherwise return KwargKey.init("") + The playlist uploader if it exists, otherwise return Variable("") """ - return KwargKey.init("playlist_uploader") + return MetadataVariable("playlist_uploader", metadata_key="uploader") @property - def playlist_uploader_sanitized(self) -> KwargKey: + def playlist_uploader_sanitized(self) -> Variable: """ Returns ------- str The playlist uploader, sanitized. """ - return KwargKey.init("playlist_uploader_sanitized") + return Variable("playlist_uploader_sanitized") @property - def playlist_uploader_url(self) -> KwargKey: + def playlist_uploader_url(self) -> MetadataVariable: """ Returns ------- str The playlist uploader url if it exists, otherwise returns the playlist webpage_url. """ - return KwargKey.init("playlist_uploader_url") + return MetadataVariable("playlist_uploader_url", metadata_key="uploader_url") @property - def source_uploader_id(self) -> KwargKey: + def source_uploader_id(self) -> Variable: """ Returns ------- str The source uploader id if it exists, otherwise returns the playlist_uploader_id """ - return KwargKey.init("source_uploader_id") + return Variable("source_uploader_id") @property - def source_uploader(self) -> KwargKey: + def source_uploader(self) -> Variable: """ Returns ------- str - The source uploader if it exists, otherwise return KwargKey.init("") + The source uploader if it exists, otherwise return Variable("") """ - return KwargKey.init("source_uploader") + return Variable("source_uploader") @property - def source_uploader_url(self) -> KwargKey: + def source_uploader_url(self) -> Variable: """ Returns ------- str The source uploader url if it exists, otherwise returns the source webpage_url. """ - return KwargKey.init("source_uploader_url") + return Variable("source_uploader_url") @property - def creator(self) -> KwargKey: + def creator(self) -> MetadataVariable: """ Returns ------- str The creator name if it exists, otherwise returns the channel. """ - return KwargKey.init("creator") + return MetadataVariable(variable_name="creator", metadata_key="creator") @property - def creator_sanitized(self) -> KwargKey: + def creator_sanitized(self) -> Variable: """ Returns ------- str The creator name, sanitized """ - return KwargKey.init("creator_sanitized") + return Variable("creator_sanitized") @property - def channel(self) -> KwargKey: + def channel(self) -> MetadataVariable: """ Returns ------- str The channel name if it exists, otherwise returns the uploader. """ - return KwargKey.init("channel") + return MetadataVariable(variable_name="channel", metadata_key="channel") @property - def channel_sanitized(self) -> KwargKey: + def channel_sanitized(self) -> Variable: """ Returns ------- str The channel name, sanitized. """ - return KwargKey.init("channel_sanitized") + return Variable("channel_sanitized") @property - def channel_id(self) -> KwargKey: + def channel_id(self) -> MetadataVariable: """ Returns ------- str The channel id if it exists, otherwise returns the entry uploader ID. """ - return KwargKey.init("channel_id") + return MetadataVariable(variable_name="channel_id", metadata_key="channel_id") @property - def ext(self) -> KwargKey: + def ext(self) -> MetadataVariable: """ Returns ------- str The downloaded entry's file extension """ - return KwargKey.init("ext") + return MetadataVariable(variable_name="ext", metadata_key="ext") @property - def thumbnail_ext(self) -> KwargKey: + def thumbnail_ext(self) -> Variable: """ Returns ------- str - The download entry's thumbnail extension. Will always return KwargKey.init("") + The download entry's thumbnail extension. Will always return 'jpg'. Until there is a need to support other image types, we always convert to jpg. """ - return KwargKey.init("thumbnail_ext") + return Variable("thumbnail_ext") @property - def download_index(self) -> KwargKey: + def download_index(self) -> MetadataVariable: """ Returns ------- @@ -572,90 +609,90 @@ class Variables: The i'th entry downloaded. NOTE that this is fetched dynamically from the download archive. """ - return KwargKey.init("download_index") + return MetadataVariable(variable_name="download_index", metadata_key="download_index") @property - def download_index_padded6(self) -> KwargKey: + def download_index_padded6(self) -> Variable: """ Returns ------- str The download_index padded six digits """ - return KwargKey.init("download_index_padded6") + return Variable("download_index_padded6") @property - def upload_date_index(self) -> KwargKey: + def upload_date_index(self) -> MetadataVariable: """ Returns ------- int The i'th entry downloaded with this upload date. """ - return KwargKey.init("upload_date_index") + return MetadataVariable(variable_name="upload_date_index", metadata_key="upload_date_index") @property - def upload_date_index_padded(self) -> KwargKey: + def upload_date_index_padded(self) -> Variable: """ Returns ------- int The upload_date_index padded two digits """ - return KwargKey.init("upload_date_index_padded") + return Variable("upload_date_index_padded") @property - def upload_date_index_reversed(self) -> KwargKey: + def upload_date_index_reversed(self) -> Variable: """ Returns ------- int 100 - upload_date_index """ - return KwargKey.init("upload_date_index_reversed") + return Variable("upload_date_index_reversed") @property - def upload_date_index_reversed_padded(self) -> KwargKey: + def upload_date_index_reversed_padded(self) -> Variable: """ Returns ------- int The upload_date_index padded two digits """ - return KwargKey.init("upload_date_index_reversed_padded") + return Variable("upload_date_index_reversed_padded") @property - def upload_date(self) -> KwargKey: + def upload_date(self) -> MetadataVariable: """ Returns ------- str - The entry's uploaded date, in YYYYMMDD format. If not present, return KwargKey.init("") + The entry's uploaded date, in YYYYMMDD format. If not present, return Variable("") """ - return KwargKey.init("upload_date") + return MetadataVariable(variable_name="upload_date", metadata_key="upload_date") @property - def upload_year(self) -> KwargKey: + def upload_year(self) -> Variable: """ Returns ------- int The entry's upload year """ - return KwargKey.init("upload_year") + return Variable("upload_year") @property - def upload_year_truncated(self) -> KwargKey: + def upload_year_truncated(self) -> Variable: """ Returns ------- int The last two digits of the upload year, i.e. 22 in 2022 """ - return KwargKey.init("upload_year_truncated") + return Variable("upload_year_truncated") @property - def upload_year_truncated_reversed(self) -> KwargKey: + def upload_year_truncated_reversed(self) -> Variable: """ Returns ------- @@ -663,70 +700,70 @@ class Variables: The upload year truncated, but reversed using ``100 - {upload_year_truncated}``, i.e. 2022 returns ``100 - 22`` = ``78`` """ - return KwargKey.init("upload_year_truncated_reversed") + return Variable("upload_year_truncated_reversed") @property - def upload_month_reversed(self) -> KwargKey: + def upload_month_reversed(self) -> Variable: """ Returns ------- int The upload month, but reversed using ``13 - {upload_month}``, i.e. March returns ``10`` """ - return KwargKey.init("upload_month_reversed") + return Variable("upload_month_reversed") @property - def upload_month_reversed_padded(self) -> KwargKey: + def upload_month_reversed_padded(self) -> Variable: """ Returns ------- str The reversed upload month, but padded. i.e. November returns "02" """ - return KwargKey.init("upload_month_reversed_padded") + return Variable("upload_month_reversed_padded") @property - def upload_month_padded(self) -> KwargKey: + def upload_month_padded(self) -> Variable: """ Returns ------- str The entry's upload month padded to two digits, i.e. March returns "03" """ - return KwargKey.init("upload_month_padded") + return Variable("upload_month_padded") @property - def upload_day_padded(self) -> KwargKey: + def upload_day_padded(self) -> Variable: """ Returns ------- str The entry's upload day padded to two digits, i.e. the fifth returns "05" """ - return KwargKey.init("upload_day_padded") + return Variable("upload_day_padded") @property - def upload_month(self) -> KwargKey: + def upload_month(self) -> Variable: """ Returns ------- int The upload month as an integer (no padding). """ - return KwargKey.init("upload_month") + return Variable("upload_month") @property - def upload_day(self) -> KwargKey: + def upload_day(self) -> Variable: """ Returns ------- int The upload day as an integer (no padding). """ - return KwargKey.init("upload_day") + return Variable("upload_day") @property - def upload_day_reversed(self) -> KwargKey: + def upload_day_reversed(self) -> Variable: """ Returns ------- @@ -734,40 +771,40 @@ class Variables: The upload day, but reversed using ``{total_days_in_month} + 1 - {upload_day}``, i.e. August 8th would have upload_day_reversed of ``31 + 1 - 8`` = ``24`` """ - return KwargKey.init("upload_day_reversed") + return Variable("upload_day_reversed") @property - def upload_day_reversed_padded(self) -> KwargKey: + def upload_day_reversed_padded(self) -> Variable: """ Returns ------- str The reversed upload day, but padded. i.e. August 30th returns "02". """ - return KwargKey.init("upload_day_reversed_padded") + return Variable("upload_day_reversed_padded") @property - def upload_day_of_year(self) -> KwargKey: + def upload_day_of_year(self) -> Variable: """ Returns ------- int The day of the year, i.e. February 1st returns ``32`` """ - return KwargKey.init("upload_day_of_year") + return Variable("upload_day_of_year") @property - def upload_day_of_year_padded(self) -> KwargKey: + def upload_day_of_year_padded(self) -> Variable: """ Returns ------- str The upload day of year, but padded i.e. February 1st returns "032" """ - return KwargKey.init("upload_day_of_year_padded") + return Variable("upload_day_of_year_padded") @property - def upload_day_of_year_reversed(self) -> KwargKey: + def upload_day_of_year_reversed(self) -> Variable: """ Returns ------- @@ -775,60 +812,60 @@ class Variables: The upload day, but reversed using ``{total_days_in_year} + 1 - {upload_day}``, i.e. February 2nd would have upload_day_of_year_reversed of ``365 + 1 - 32`` = ``334`` """ - return KwargKey.init("upload_day_of_year_reversed") + return Variable("upload_day_of_year_reversed") @property - def upload_day_of_year_reversed_padded(self) -> KwargKey: + def upload_day_of_year_reversed_padded(self) -> Variable: """ Returns ------- str The reversed upload day of year, but padded i.e. December 31st returns "001" """ - return KwargKey.init("upload_day_of_year_reversed_padded") + return Variable("upload_day_of_year_reversed_padded") @property - def upload_date_standardized(self) -> KwargKey: + def upload_date_standardized(self) -> Variable: """ Returns ------- str The uploaded date formatted as YYYY-MM-DD """ - return KwargKey.init("upload_date_standardized") + return Variable("upload_date_standardized") @property - def release_date(self) -> KwargKey: + def release_date(self) -> MetadataVariable: """ Returns ------- str - The entry's release date, in YYYYMMDD format. If not present, return KwargKey.init("") + The entry's release date, in YYYYMMDD format. If not present, return Variable("") """ - return KwargKey.init("release_date") + return MetadataVariable(variable_name="release_date", metadata_key="release_date") @property - def release_year(self) -> KwargKey: + def release_year(self) -> Variable: """ Returns ------- int The entry's release year """ - return KwargKey.init("release_year") + return Variable("release_year") @property - def release_year_truncated(self) -> KwargKey: + def release_year_truncated(self) -> Variable: """ Returns ------- int The last two digits of the release year, i.e. 22 in 2022 """ - return KwargKey.init("release_year_truncated") + return Variable("release_year_truncated") @property - def release_year_truncated_reversed(self) -> KwargKey: + def release_year_truncated_reversed(self) -> Variable: """ Returns ------- @@ -836,10 +873,10 @@ class Variables: The release year truncated, but reversed using ``100 - {release_year_truncated}``, i.e. 2022 returns ``100 - 22`` = ``78`` """ - return KwargKey.init("release_year_truncated_reversed") + return Variable("release_year_truncated_reversed") @property - def release_month_reversed(self) -> KwargKey: + def release_month_reversed(self) -> Variable: """ Returns ------- @@ -847,60 +884,60 @@ class Variables: The release month, but reversed using ``13 - {release_month}``, i.e. March returns ``10`` """ - return KwargKey.init("release_month_reversed") + return Variable("release_month_reversed") @property - def release_month_reversed_padded(self) -> KwargKey: + def release_month_reversed_padded(self) -> Variable: """ Returns ------- str The reversed release month, but padded. i.e. November returns "02" """ - return KwargKey.init("release_month_reversed_padded") + return Variable("release_month_reversed_padded") @property - def release_month_padded(self) -> KwargKey: + def release_month_padded(self) -> Variable: """ Returns ------- str The entry's release month padded to two digits, i.e. March returns "03" """ - return KwargKey.init("release_month_padded") + return Variable("release_month_padded") @property - def release_day_padded(self) -> KwargKey: + def release_day_padded(self) -> Variable: """ Returns ------- str The entry's release day padded to two digits, i.e. the fifth returns "05" """ - return KwargKey.init("release_day_padded") + return Variable("release_day_padded") @property - def release_month(self) -> KwargKey: + def release_month(self) -> Variable: """ Returns ------- int The release month as an integer (no padding). """ - return KwargKey.init("release_month") + return Variable("release_month") @property - def release_day(self) -> KwargKey: + def release_day(self) -> Variable: """ Returns ------- int The release day as an integer (no padding). """ - return KwargKey.init("release_day") + return Variable("release_day") @property - def release_day_reversed(self) -> KwargKey: + def release_day_reversed(self) -> Variable: """ Returns ------- @@ -908,40 +945,40 @@ class Variables: The release day, but reversed using ``{total_days_in_month} + 1 - {release_day}``, i.e. August 8th would have release_day_reversed of ``31 + 1 - 8`` = ``24`` """ - return KwargKey.init("release_day_reversed") + return Variable("release_day_reversed") @property - def release_day_reversed_padded(self) -> KwargKey: + def release_day_reversed_padded(self) -> Variable: """ Returns ------- str The reversed release day, but padded. i.e. August 30th returns "02". """ - return KwargKey.init("release_day_reversed_padded") + return Variable("release_day_reversed_padded") @property - def release_day_of_year(self) -> KwargKey: + def release_day_of_year(self) -> Variable: """ Returns ------- int The day of the year, i.e. February 1st returns ``32`` """ - return KwargKey.init("release_day_of_year") + return Variable("release_day_of_year") @property - def release_day_of_year_padded(self) -> KwargKey: + def release_day_of_year_padded(self) -> Variable: """ Returns ------- str The release day of year, but padded i.e. February 1st returns "032" """ - return KwargKey.init("release_day_of_year_padded") + return Variable("release_day_of_year_padded") @property - def release_day_of_year_reversed(self) -> KwargKey: + def release_day_of_year_reversed(self) -> Variable: """ Returns ------- @@ -949,24 +986,28 @@ class Variables: The release day, but reversed using ``{total_days_in_year} + 1 - {release_day}``, i.e. February 2nd would have release_day_of_year_reversed of ``365 + 1 - 32`` = ``334`` """ - return KwargKey.init("release_day_of_year_reversed") + return Variable("release_day_of_year_reversed") @property - def release_day_of_year_reversed_padded(self) -> KwargKey: + def release_day_of_year_reversed_padded(self) -> Variable: """ Returns ------- str The reversed release day of year, but padded i.e. December 31st returns "001" """ - return KwargKey.init("release_day_of_year_reversed_padded") + return Variable("release_day_of_year_reversed_padded") @property - def release_date_standardized(self) -> KwargKey: + def release_date_standardized(self) -> Variable: """ Returns ------- str The release date formatted as YYYY-MM-DD """ - return KwargKey.init("release_date_standardized") + return Variable("release_date_standardized") + + +# Singleton to use externally +VARIABLES = _Variables() diff --git a/src/ytdl_sub/entries/script/variable_scripts.py b/src/ytdl_sub/entries/script/variable_scripts.py index 8d3d77d0..668ea935 100644 --- a/src/ytdl_sub/entries/script/variable_scripts.py +++ b/src/ytdl_sub/entries/script/variable_scripts.py @@ -1,60 +1,99 @@ from typing import Dict from typing import Optional -from ytdl_sub.entries.script.variable_definitions import KwargKey -from ytdl_sub.entries.script.variable_definitions import Variables +from ytdl_sub.entries.script.variable_definitions import VARIABLES as v +from ytdl_sub.entries.script.variable_definitions import Metadata +from ytdl_sub.entries.script.variable_definitions import MetadataVariable +from ytdl_sub.entries.script.variable_definitions import Variable -ENTRY_MAP_VARIABLE = "entry_metadata" -PLAYLIST_MAP_VARIABLE = "playlist_metadata" -SOURCE_MAP_VARIABLE = "source_metadata" +############################################################################################### +# Helpers -def _entry_get(key: KwargKey, default: Optional[KwargKey | str | int]) -> str: - if default is None: - # TODO: assert with good error message if key DNE - return f"%map_get({ENTRY_MAP_VARIABLE}, '{key.entry_key}')" - if isinstance(default, KwargKey): - return f"%map_get({ENTRY_MAP_VARIABLE}, '{key.entry_key}', {default.variable_name})" - if isinstance(default, str): - return f"%map_get({ENTRY_MAP_VARIABLE}, '{key.entry_key}', '{default}')" - - return f"%map_get({ENTRY_MAP_VARIABLE}, '{key.entry_key}', {default})" - - -def entry_get(key: KwargKey, default: Optional[KwargKey | str | int] = None) -> str: - return f"{{{_entry_get(key=key, default=default)}}}" - - -def entry_get_int(key: KwargKey, default: Optional[KwargKey] = None) -> str: - return f"{{%int({_entry_get(key=key, default=default)}}}" - - -def pad_int(key: KwargKey, pad: int) -> str: +def pad_int(key: Variable, pad: int) -> str: return f"{{%pad_zero({key.variable_name}, {pad})}}" -def sanitized(key: KwargKey) -> str: +def sanitized(key: Variable) -> str: return f"{{%sanitize({key.variable_name})}}" -def sanitized_plex(key: KwargKey) -> str: +def sanitized_plex(key: Variable) -> str: return f"{{%sanitize_plex_episode({key.variable_name})}}" -def date_metadata(date_key: KwargKey, metadata_key: str) -> str: - return f"{{%map_get(%date_metadata({date_key.variable_name}), '{metadata_key}')}}" +def date_metadata(date_key: Variable, metadata_key: str) -> str: + return f"{{%map_get(%to_date_metadata({date_key.variable_name}), '{metadata_key}')}}" -# singleton to produce variable keys -v = Variables() +############################################################################################### +# Metadata Getters -ENTRY_HARDCODED_VARIABLES: Dict[KwargKey, str] = { +def _get(metadata: Metadata, key: MetadataVariable, default: Optional[Variable | str | int]) -> str: + if default is None: + # TODO: assert with good error message if key DNE + return f"%map_get({metadata.variable_name}, '{key.metadata_key}')" + if isinstance(default, Variable): + return f"%map_get({metadata.variable_name}, '{key.metadata_key}', {default.variable_name})" + if isinstance(default, str): + return f"%map_get({metadata.variable_name}, '{key.metadata_key}', '{default}')" + + return f"%map_get({metadata.variable_name}, '{key.metadata_key}', {default})" + + +def _get_int( + metadata: Metadata, key: MetadataVariable, default: Optional[Variable | int] = None +) -> str: + return f"{{%int({_get(metadata=metadata, key=key, default=default)}}}" + + +############################################################################################### +# Entry Getters + + +def entry_get(key: MetadataVariable, default: Optional[Variable | str | int] = None) -> str: + return f"{{{_get(metadata=v.entry_metadata, key=key, default=default)}}}" + + +def entry_get_int(key: MetadataVariable, default: Optional[Variable | int] = None) -> str: + return f"{{%int({_get_int(metadata=v.entry_metadata, key=key, default=default)}}}" + + +############################################################################################### +# Playlist Getters + + +def playlist_get(key: MetadataVariable, default: Optional[Variable | str | int] = None) -> str: + return f"{{{_get(metadata=v.playlist_metadata, key=key, default=default)}}}" + + +def playlist_get_int(key: MetadataVariable, default: Optional[Variable | int] = None) -> str: + return f"{{%int({_get_int(metadata=v.playlist_metadata, key=key, default=default)}}}" + + +############################################################################################### +# Source Getters + + +def source_get(key: MetadataVariable, default: Optional[Variable | str | int] = None) -> str: + return f"{{{_get(metadata=v.source_metadata, key=key, default=default)}}}" + + +def source_get_int(key: MetadataVariable, default: Optional[Variable | int] = None) -> str: + return f"{{%int({_get_int(metadata=v.source_metadata, key=key, default=default)}}}" + + +############################################################################################### +# Scripts + + +ENTRY_HARDCODED_VARIABLES: Dict[Variable, str] = { v.info_json_ext: "info.json", v.thumbnail_ext: "jpg", } -ENTRY_REQUIRED_VARIABLES: Dict[KwargKey, str] = { +ENTRY_REQUIRED_VARIABLES: Dict[MetadataVariable, str] = { v.uid: entry_get(v.uid), v.ie_key: entry_get(v.ie_key), v.epoch: entry_get_int(v.epoch), @@ -62,41 +101,42 @@ ENTRY_REQUIRED_VARIABLES: Dict[KwargKey, str] = { v.ext: entry_get(v.ext), } -ENTRY_DEFAULT_VARIABLES: Dict[KwargKey, str] = { +ENTRY_DEFAULT_VARIABLES: Dict[MetadataVariable, str] = { + v.playlist_index: playlist_get_int(v.playlist_index, 1), v.title: entry_get(v.title, v.uid), v.extractor: entry_get(v.extractor, v.ie_key), v.description: entry_get(v.description, ""), v.uploader_id: entry_get(v.uploader_id, v.uid), v.uploader: entry_get(v.uploader, v.uploader_id), v.uploader_url: entry_get(v.uploader_url, v.webpage_url), + v.upload_date: entry_get(v.upload_date, v.epoch_date), + v.release_date: entry_get(v.release_date, v.upload_date), + v.channel: entry_get(v.channel, v.uploader), + v.creator: entry_get(v.creator, v.channel), + v.channel_id: entry_get(v.channel_id, v.uploader_id), } -ENTRY_DERIVED_VARIABLES: Dict[KwargKey, str] = { +ENTRY_INJECTED_VARIABLES: Dict[MetadataVariable, str] = { + v.download_index: entry_get_int(v.download_index), + v.upload_date_index: entry_get_int(v.upload_date_index), +} + +ENTRY_DERIVED_VARIABLES: Dict[Variable, str] = { v.uid_sanitized: sanitized(v.uid), v.uid_sanitized_plex: sanitized_plex(v.uid_sanitized_plex), v.title_sanitized: sanitized(v.title), v.title_sanitized_plex: sanitized_plex(v.title), v.epoch_date: f"{{%datetime_strftime({v.epoch.variable_name}, '%Y%m%d')}}", v.epoch_hour: f"{{%datetime_strftime({v.epoch.variable_name}, '%H')}}", - v.upload_date: entry_get(v.upload_date, v.epoch_date), - v.release_date: entry_get(v.release_date, v.upload_date), - v.channel: entry_get(v.channel, v.uploader), v.channel_sanitized: sanitized(v.channel), - v.creator: entry_get(v.creator, v.channel), v.creator_sanitized: sanitized(v.creator), - v.channel_id: entry_get(v.channel_id, v.uploader_id), -} - -ENTRY_ARCHIVE_VARIABLES: Dict[KwargKey, str] = { - v.download_index: entry_get_int(v.download_index), v.download_index_padded6: pad_int(v.download_index, 6), - v.upload_date_index: entry_get_int(v.upload_date_index), v.upload_date_index_padded: pad_int(v.upload_date_index, 2), v.upload_date_index_reversed: f"{{%sub(100, {v.download_index})}}", v.upload_date_index_reversed_padded: pad_int(v.upload_date_index_reversed, 2), } -ENTRY_UPLOAD_DATE_VARIABLES: Dict[KwargKey, str] = { +ENTRY_UPLOAD_DATE_VARIABLES: Dict[Variable, str] = { v.upload_year: date_metadata(v.upload_date, "year"), v.upload_year_truncated: date_metadata(v.upload_date, "year_truncated"), v.upload_year_truncated_reversed: date_metadata(v.upload_date, "year_truncated_reversed"), @@ -117,7 +157,7 @@ ENTRY_UPLOAD_DATE_VARIABLES: Dict[KwargKey, str] = { v.upload_date_standardized: date_metadata(v.upload_date, "date_standardized"), } -ENTRY_RELEASE_DATE_VARIABLES: Dict[KwargKey, str] = { +ENTRY_RELEASE_DATE_VARIABLES: Dict[Variable, str] = { v.release_year: date_metadata(v.release_date, "year"), v.release_year_truncated: date_metadata(v.release_date, "year_truncated"), v.release_year_truncated_reversed: date_metadata(v.release_date, "year_truncated_reversed"), @@ -138,37 +178,46 @@ ENTRY_RELEASE_DATE_VARIABLES: Dict[KwargKey, str] = { v.release_date_standardized: date_metadata(v.release_date, "date_standardized"), } -ENTRY_SOURCE_VARIABLES: Dict[KwargKey, str] = { - # source_title - # source_title_sanitized - # source_uid - # source_index - # source_index_padded - # source_count - # source_webpage_url - # source_description - # source_uploader_id - # source_uploader - # source_uploader_url +PLAYLIST_VARIABLES: Dict[MetadataVariable, str] = { + v.playlist_uid: playlist_get(v.playlist_uid, v.uid), + v.playlist_title: playlist_get(v.playlist_title, v.title), + v.playlist_webpage_url: playlist_get(v.playlist_webpage_url, v.webpage_url), + v.playlist_count: playlist_get_int(v.playlist_count, 1), + v.playlist_description: playlist_get(v.playlist_description, v.description), + v.playlist_uploader_id: playlist_get(v.playlist_uploader_id, v.uploader_id), + v.playlist_uploader: playlist_get(v.playlist_uploader, v.uploader), + v.playlist_uploader_url: playlist_get(v.playlist_uploader_url, v.uploader_url), } -ENTRY_PLAYLIST_VARIABLES: Dict[KwargKey, str] = { - # playlist_uid - # playlist_title - # playlist_title_sanitized - # playlist_index - # playlist_index_reversed - # playlist_index_padded - # playlist_index_reversed_padded - # playlist_index_padded6 - # playlist_index_reversed_padded6 - # playlist_count - # playlist_description - # playlist_webpage_url - # playlist_max_upload_year - # playlist_max_upload_year_truncated - # playlist_uploader_id - # playlist_uploader - # playlist_uploader_sanitized - # playlist_uploader_url +PLAYLIST_INJECTED_VARIABLES: Dict[MetadataVariable, str] = { + v.playlist_max_upload_year: playlist_get(v.playlist_max_upload_year, v.upload_year) +} + +PLAYLIST_DERIVED_VARIABLES: Dict[Variable, str] = { + v.playlist_title_sanitized: sanitized(v.playlist_title), + v.playlist_index_reversed: f"{{%sub({v.playlist_count, v.playlist_index, 1})}}", + v.playlist_index_padded: pad_int(v.playlist_index, 2), + v.playlist_index_reversed_padded: pad_int(v.playlist_index_reversed, 2), + v.playlist_index_padded6: pad_int(v.playlist_index, 6), + v.playlist_index_reversed_padded6: pad_int(v.playlist_index_reversed, 6), + v.playlist_uploader_sanitized: sanitized(v.playlist_uploader), + # v.playlist_max_upload_year_truncated: +} + + +SOURCE_VARIABLES: Dict[MetadataVariable, str] = { + v.source_uid: source_get(v.source_uid, v.playlist_uid), + v.source_title: source_get(v.source_title, v.playlist_title), + v.source_webpage_url: source_get(v.source_webpage_url, v.playlist_webpage_url), + v.source_index: source_get_int(v.source_index, 1), + v.source_count: source_get_int(v.source_count, 1), + v.source_description: source_get(v.source_description, v.playlist_description), + v.source_uploader_id: source_get(v.source_uploader_id, v.playlist_uploader_id), + v.source_uploader: source_get(v.source_uploader, v.playlist_uploader), + v.source_uploader_url: source_get(v.source_uploader_url, v.playlist_uploader_url), +} + +SOURCE_DERIVED_VARIABLES: Dict[Variable, str] = { + v.source_title_sanitized: sanitized(v.source_title), + v.source_index_padded: pad_int(v.source_index, 2), } diff --git a/src/ytdl_sub/entries/variables/entry_variables.py b/src/ytdl_sub/entries/variables/entry_variables.py index 8405b060..d907f5de 100644 --- a/src/ytdl_sub/entries/variables/entry_variables.py +++ b/src/ytdl_sub/entries/variables/entry_variables.py @@ -110,7 +110,7 @@ class EntryVariables(BaseEntryVariables): Returns ------- int - The source count if it exists, otherwise returns the playlist count. + The source count if it exists, otherwise returns 1. """ return self.kwargs_get(SOURCE_COUNT, self.playlist_count)