👷 Refactored favorite download methods to factory. Removed duplicate and falsely named code.
This commit is contained in:
+30
-25
@@ -28,6 +28,7 @@ dl_fav_group = typer.Typer(
|
|||||||
add_completion=True,
|
add_completion=True,
|
||||||
help="Download from a favorites collection.",
|
help="Download from a favorites collection.",
|
||||||
)
|
)
|
||||||
|
|
||||||
app.add_typer(dl_fav_group, name="dl_fav")
|
app.add_typer(dl_fav_group, name="dl_fav")
|
||||||
|
|
||||||
|
|
||||||
@@ -37,7 +38,7 @@ def version_callback(value: bool):
|
|||||||
raise typer.Exit()
|
raise typer.Exit()
|
||||||
|
|
||||||
|
|
||||||
def _download(ctx: typer.Context, urls: list[str], try_login: bool = True):
|
def _download(ctx: typer.Context, urls: list[str], try_login: bool = True) -> bool:
|
||||||
"""
|
"""
|
||||||
Shared logic for downloading a list of URLs.
|
Shared logic for downloading a list of URLs.
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ def _download(ctx: typer.Context, urls: list[str], try_login: bool = True):
|
|||||||
session=ctx.obj[CTX_TIDAL].session,
|
session=ctx.obj[CTX_TIDAL].session,
|
||||||
skip_existing=ctx.obj[CTX_TIDAL].settings.data.skip_existing,
|
skip_existing=ctx.obj[CTX_TIDAL].settings.data.skip_existing,
|
||||||
path_base=settings.data.download_base_path,
|
path_base=settings.data.download_base_path,
|
||||||
fn_logger=fn_logger, # noqa: type
|
fn_logger=fn_logger,
|
||||||
progress=progress,
|
progress=progress,
|
||||||
)
|
)
|
||||||
progress_table = Table.grid()
|
progress_table = Table.grid()
|
||||||
@@ -226,7 +227,7 @@ def download(
|
|||||||
help="List with URLs to download. One per line",
|
help="List with URLs to download. One per line",
|
||||||
),
|
),
|
||||||
] = None,
|
] = None,
|
||||||
):
|
) -> bool:
|
||||||
if not urls:
|
if not urls:
|
||||||
# Read the text file provided.
|
# Read the text file provided.
|
||||||
if file_urls:
|
if file_urls:
|
||||||
@@ -242,54 +243,58 @@ def download(
|
|||||||
|
|
||||||
@dl_fav_group.command(
|
@dl_fav_group.command(
|
||||||
name="tracks",
|
name="tracks",
|
||||||
help="Download your saved, favorite tracks collection.",
|
help="Download your favorite tracks collection.",
|
||||||
)
|
)
|
||||||
def download_fav_tracks(ctx: typer.Context):
|
def download_fav_tracks(ctx: typer.Context):
|
||||||
# Call login method to validate the token.
|
# Method name
|
||||||
ctx.invoke(login, ctx)
|
func_name_favorites: str = "tracks"
|
||||||
|
|
||||||
# Get favorite tracks
|
return _download_fav_factory(ctx, func_name_favorites)
|
||||||
track_urls = [track.share_url for track in ctx.obj[CTX_TIDAL].session.user.favorites.tracks()]
|
|
||||||
return _download(ctx, track_urls, try_login=False)
|
|
||||||
|
|
||||||
|
|
||||||
@dl_fav_group.command(
|
@dl_fav_group.command(
|
||||||
name="artists",
|
name="artists",
|
||||||
help="Download your saved, favorite artists collection.",
|
help="Download your favorite artists collection.",
|
||||||
)
|
)
|
||||||
def download_fav_artists(ctx: typer.Context):
|
def download_fav_artists(ctx: typer.Context):
|
||||||
# Call login method to validate the token.
|
# Method name
|
||||||
ctx.invoke(login, ctx)
|
func_name_favorites: str = "artists"
|
||||||
|
|
||||||
# Get favorite artists
|
return _download_fav_factory(ctx, func_name_favorites)
|
||||||
artist_urls = [artist.share_url for artist in ctx.obj[CTX_TIDAL].session.user.favorites.artists()]
|
|
||||||
return _download(ctx, artist_urls, try_login=False)
|
|
||||||
|
|
||||||
|
|
||||||
@dl_fav_group.command(
|
@dl_fav_group.command(
|
||||||
name="albums",
|
name="albums",
|
||||||
help="Download your saved, favorite albums collection.",
|
help="Download your favorite albums collection.",
|
||||||
)
|
)
|
||||||
def download_fav_albums(ctx: typer.Context):
|
def download_fav_albums(ctx: typer.Context):
|
||||||
# Call login method to validate the token.
|
# Method name
|
||||||
ctx.invoke(login, ctx)
|
func_name_favorites: str = "albums"
|
||||||
|
|
||||||
# Get favorite artists
|
return _download_fav_factory(ctx, func_name_favorites)
|
||||||
artist_urls = [artist.share_url for artist in ctx.obj[CTX_TIDAL].session.user.favorites.albums()]
|
|
||||||
return _download(ctx, artist_urls, try_login=False)
|
|
||||||
|
|
||||||
|
|
||||||
@dl_fav_group.command(
|
@dl_fav_group.command(
|
||||||
name="videos",
|
name="videos",
|
||||||
help="Download your saved, favorite videos collection.",
|
help="Download your favorite videos collection.",
|
||||||
)
|
)
|
||||||
def download_fav_videos(ctx: typer.Context):
|
def download_fav_videos(ctx: typer.Context):
|
||||||
|
# Method name
|
||||||
|
func_name_favorites: str = "videos"
|
||||||
|
|
||||||
|
return _download_fav_factory(ctx, func_name_favorites)
|
||||||
|
|
||||||
|
|
||||||
|
def _download_fav_factory(ctx: typer.Context, func_name_favorites: str):
|
||||||
# Call login method to validate the token.
|
# Call login method to validate the token.
|
||||||
ctx.invoke(login, ctx)
|
ctx.invoke(login, ctx)
|
||||||
|
|
||||||
# Get favorite artists
|
# Get the method from the module
|
||||||
artist_urls = [artist.share_url for artist in ctx.obj[CTX_TIDAL].session.user.favorites.videos()]
|
func_favorites = getattr(ctx.obj[CTX_TIDAL].session.user.favorites, func_name_favorites)
|
||||||
return _download(ctx, artist_urls, try_login=False)
|
# Get favorite videos
|
||||||
|
media_urls = [media.share_url for media in func_favorites()]
|
||||||
|
|
||||||
|
return _download(ctx, media_urls, try_login=False)
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
|
|||||||
Reference in New Issue
Block a user