soundlcoud working, refactor album track

This commit is contained in:
jbannon 2022-04-11 23:35:17 +00:00
parent 0580745176
commit af2d091d50
3 changed files with 53 additions and 14 deletions

View file

@ -9,6 +9,11 @@ def uid():
return "abc123" return "abc123"
@pytest.fixture
def extractor():
return "xtract"
@pytest.fixture @pytest.fixture
def title(): def title():
return "entry title" return "entry title"
@ -45,7 +50,9 @@ def download_file_name(uid, ext):
@pytest.fixture @pytest.fixture
def mock_entry_to_dict(uid, title, ext, upload_date, upload_year, thumbnail, thumbnail_ext): def mock_entry_to_dict(
uid, title, ext, upload_date, upload_year, thumbnail, thumbnail_ext, extractor
):
return { return {
"uid": uid, "uid": uid,
"title": title, "title": title,
@ -55,6 +62,7 @@ def mock_entry_to_dict(uid, title, ext, upload_date, upload_year, thumbnail, thu
"upload_year": upload_year, "upload_year": upload_year,
"thumbnail": thumbnail, "thumbnail": thumbnail,
"thumbnail_ext": thumbnail_ext, "thumbnail_ext": thumbnail_ext,
"extractor": extractor,
} }
@ -62,6 +70,7 @@ def mock_entry_to_dict(uid, title, ext, upload_date, upload_year, thumbnail, thu
def mock_entry_kwargs(uid, title, ext, upload_date, thumbnail): def mock_entry_kwargs(uid, title, ext, upload_date, thumbnail):
return { return {
"id": uid, "id": uid,
"extractor": extractor,
"title": title, "title": title,
"ext": ext, "ext": ext,
"upload_date": upload_date, "upload_date": upload_date,

View file

@ -35,9 +35,21 @@ class BaseEntry(ABC):
@property @property
def extractor(self) -> str: def extractor(self) -> str:
"""Get the ytdl extrator name""" """
:return: The ytdl extrator name
"""
return self.kwargs("extractor") return self.kwargs("extractor")
@property
def order_index(self) -> int:
"""
Reserved for any child entry class that might have some kind of ordering to it.
Returns 1 unless overwritten.
:return: The order index (1-based)
"""
return 1
def to_dict(self) -> Dict[str, str]: def to_dict(self) -> Dict[str, str]:
"""Returns the entry's values as a dictionary""" """Returns the entry's values as a dictionary"""
return { return {

View file

@ -65,20 +65,24 @@ class SoundcloudAlbumTrack(SoundcloudTrack):
Entry object to represent a Soundcloud track yt-dlp that belongs to an album. Entry object to represent a Soundcloud track yt-dlp that belongs to an album.
""" """
def __init__(self, album: str, track_number: int, album_year: int, **kwargs): def __init__(self, album: str, album_year: int, track_number: int, **kwargs):
""" """
Initialize the album track using album metadata and ytdl metadata for the specific track. Initialize the album track using album metadata and ytdl metadata for the specific track.
""" """
super().__init__(**kwargs) super().__init__(**kwargs)
self._album = album self._album = album
self._track_number = track_number
self._album_year = album_year self._album_year = album_year
self._track_number = track_number
@property @property
def track_number(self) -> int: def track_number(self) -> int:
"""Returns the entry's track number""" """Returns the entry's track number"""
return self._track_number return self._track_number
@property
def order_index(self) -> int:
return self._track_number
@property @property
def album(self) -> str: def album(self) -> str:
"""Returns the entry's album name, fetched from its internal album""" """Returns the entry's album name, fetched from its internal album"""
@ -89,6 +93,17 @@ class SoundcloudAlbumTrack(SoundcloudTrack):
"""Returns the entry's album year, fetched from its internal album""" """Returns the entry's album year, fetched from its internal album"""
return self._album_year return self._album_year
@classmethod
def from_soundcloud_entry(
cls, soundcloud_track: SoundcloudTrack, album: str, album_year: int, track_number: int
) -> "SoundcloudAlbumTrack":
return SoundcloudAlbumTrack(
album=album,
album_year=album_year,
track_number=track_number,
**soundcloud_track._kwargs,
)
class SoundcloudAlbum(Entry): class SoundcloudAlbum(Entry):
""" """
@ -108,17 +123,20 @@ class SoundcloudAlbum(Entry):
Returns all tracks in the album represented as album-tracks. They will share the Returns all tracks in the album represented as album-tracks. They will share the
same album name, have ordered track numbers, and a shared album year. same album name, have ordered track numbers, and a shared album year.
""" """
album_tracks = [ tracks = [SoundcloudTrack(**entry) for entry in self.kwargs("entries")]
SoundcloudAlbumTrack(
album=self.title,
track_number=i + 1,
album_year=self.album_year,
**entry,
)
for i, entry in enumerate(self.kwargs("entries"))
]
if skip_premiere_tracks: if skip_premiere_tracks:
album_tracks = [t for t in album_tracks if not t.is_premiere] tracks = [track for track in tracks if not track.is_premiere]
album_tracks = [
SoundcloudAlbumTrack.from_soundcloud_entry(
soundcloud_track=track,
album=self.title,
album_year=self.album_year,
track_number=i + 1,
)
for i, track in enumerate(tracks)
]
return album_tracks return album_tracks