From cd9a094e2c0dc90885e9e688969b8e646cb0ee34 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Thu, 29 Sep 2022 15:39:53 -0700 Subject: [PATCH] [BACKEND] `epoch` and `playlist_index_reversed` variables (#257) --- src/ytdl_sub/entries/base_entry.py | 35 ++++++++++++++++++- .../entries/variables/entry_variables.py | 30 ++++++++++++++++ src/ytdl_sub/entries/variables/kwargs.py | 2 ++ tests/unit/entries/conftest.py | 7 ++++ 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/ytdl_sub/entries/base_entry.py b/src/ytdl_sub/entries/base_entry.py index 639d3528..9553bed7 100644 --- a/src/ytdl_sub/entries/base_entry.py +++ b/src/ytdl_sub/entries/base_entry.py @@ -1,4 +1,5 @@ from abc import ABC +from datetime import datetime from pathlib import Path from typing import Any from typing import Dict @@ -11,6 +12,8 @@ from typing import final from yt_dlp.utils import sanitize_filename from ytdl_sub.entries.variables.kwargs import DESCRIPTION +from ytdl_sub.entries.variables.kwargs import EPOCH +from ytdl_sub.entries.variables.kwargs import EXTRACTOR from ytdl_sub.entries.variables.kwargs import TITLE from ytdl_sub.entries.variables.kwargs import UID from ytdl_sub.entries.variables.kwargs import UPLOADER @@ -58,7 +61,37 @@ class BaseEntryVariables: str The ytdl extractor name """ - return self.kwargs("extractor") + return self.kwargs(EXTRACTOR) + + @property + def epoch(self: "BaseEntry") -> int: + """ + Returns + ------- + int + The unix epoch of when the metadata was scraped by yt-dlp. + """ + return self.kwargs(EPOCH) + + @property + def epoch_date(self: "BaseEntry") -> str: + """ + Returns + ------- + str + The epoch's date, in YYYYMMDD format. + """ + return datetime.utcfromtimestamp(self.epoch).strftime("%Y%m%d") + + @property + def epoch_hour(self: "BaseEntry") -> str: + """ + Returns + ------- + str + The epoch's hour, padded + """ + return datetime.utcfromtimestamp(self.epoch).strftime("%H") @property def title(self: "BaseEntry") -> str: diff --git a/src/ytdl_sub/entries/variables/entry_variables.py b/src/ytdl_sub/entries/variables/entry_variables.py index 05aded33..a37a7eb2 100644 --- a/src/ytdl_sub/entries/variables/entry_variables.py +++ b/src/ytdl_sub/entries/variables/entry_variables.py @@ -169,6 +169,16 @@ class EntryVariables(BaseEntryVariables): """ return self.kwargs_get(PLAYLIST_INDEX, 1) + @property + def playlist_index_reversed(self: Self) -> int: + """ + Returns + ------- + int + Playlist index reversed via ``playlist_count - playlist_index + 1`` + """ + return self.playlist_count - self.playlist_index + 1 + @property def playlist_index_padded(self: Self) -> str: """ @@ -179,6 +189,16 @@ class EntryVariables(BaseEntryVariables): """ return _pad(self.playlist_index, width=2) + @property + def playlist_index_reversed_padded(self: Self) -> str: + """ + Returns + ------- + str + playlist_index_reversed padded two digits + """ + return _pad(self.playlist_index_reversed, width=2) + @property def playlist_index_padded6(self: Self) -> str: """ @@ -189,6 +209,16 @@ class EntryVariables(BaseEntryVariables): """ return _pad(self.playlist_index, width=6) + @property + def playlist_index_reversed_padded6(self: Self) -> str: + """ + Returns + ------- + str + playlist_index_reversed padded six digits. + """ + return _pad(self.playlist_index_reversed, width=6) + @property def playlist_count(self: Self) -> int: """ diff --git a/src/ytdl_sub/entries/variables/kwargs.py b/src/ytdl_sub/entries/variables/kwargs.py index d222606f..80304ca2 100644 --- a/src/ytdl_sub/entries/variables/kwargs.py +++ b/src/ytdl_sub/entries/variables/kwargs.py @@ -41,6 +41,8 @@ PLAYLIST_UPLOADER_ID = _("playlist_uploader_id") PLAYLIST_UPLOADER_URL = _("playlist_uploader_url") UID = _("id") +EXTRACTOR = _("extractor") +EPOCH = _("epoch") CHANNEL = _("channel") EXT = _("ext") TITLE = _("title") diff --git a/tests/unit/entries/conftest.py b/tests/unit/entries/conftest.py index 610ee599..8be3a8b1 100644 --- a/tests/unit/entries/conftest.py +++ b/tests/unit/entries/conftest.py @@ -62,6 +62,9 @@ def mock_entry_to_dict( return { "uid": uid, "uid_sanitized": uid, + "epoch": 1596878400, + "epoch_date": "20200808", + "epoch_hour": "09", "title": "entry {title}", "title_sanitized": "entry {title}", "ext": ext, @@ -95,6 +98,9 @@ def mock_entry_to_dict( "playlist_index": 1, "playlist_index_padded": "01", "playlist_index_padded6": "000001", + "playlist_index_reversed": 1, + "playlist_index_reversed_padded": "01", + "playlist_index_reversed_padded6": "000001", "playlist_count": 1, "playlist_max_upload_year": 2021, "playlist_max_upload_year_truncated": 21, @@ -126,6 +132,7 @@ def mock_entry_kwargs( ): return { "id": uid, + "epoch": 1596878400, "extractor": extractor, "title": title, "ext": ext,