Skip to content

Commit

Permalink
Added an option to delete deprecated tracks. (#340)
Browse files Browse the repository at this point in the history
* Added an option to delete deprecated tracks.

* Adapted test to new option.

* ... and for other tests.
  • Loading branch information
LuluDavid committed Apr 27, 2023
1 parent 524e065 commit f2beda1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion spotify_dl/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def fetch_tracks(sp, item_type, url):
track_num = track_info.get("track_number")
spotify_id = track_info.get("id")
track_name = track_info.get("name")
track_artist = ",".join(
track_artist = ", ".join(
[artist["name"] for artist in track_info.get("artists")]
)
if track_album_info:
Expand Down
8 changes: 8 additions & 0 deletions spotify_dl/spotify_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ def spotify_dl():
help="Whether we should avoid overwriting the target audio file if it already exists",
default=False,
)
parser.add_argument(
"-r",
"--remove-trailing-tracks",
default="no",
action="store_true",
help="Whether we should delete tracks that were previously downloaded but are not longer in the playlist"
)
parser.add_argument(
"-V",
"--verbose",
Expand Down Expand Up @@ -190,6 +197,7 @@ def spotify_dl():
skip_mp3=args.skip_mp3,
keep_playlist_order=args.keep_playlist_order,
no_overwrites=args.no_overwrites,
remove_trailing_tracks=args.remove_trailing_tracks,
use_sponsorblock=args.use_sponsorblock,
file_name_f=file_name_f,
multi_core=args.multi_core,
Expand Down
25 changes: 20 additions & 5 deletions spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from os import path
import os
import multiprocessing

import mutagen
import csv
import yt_dlp
Expand Down Expand Up @@ -120,6 +121,7 @@ def find_and_download_songs(kwargs):
"""
sponsorblock_postprocessor = []
reference_file = kwargs["reference_file"]
files = {}
with open(reference_file, "r", encoding="utf-8") as file:
for line in file:
temp = line.split(";")
Expand Down Expand Up @@ -150,14 +152,21 @@ def find_and_download_songs(kwargs):
"force_keyframes": True,
},
]
save_path = kwargs["track_db"][i]["save_path"]
file_path = path.join(save_path, file_name)

mp3file_path = f"{file_path}.mp3"

file_path = path.join(kwargs["track_db"][i]["save_path"], file_name)
if save_path not in files:
path_files = set()
files[save_path] = path_files
else:
path_files = files[save_path]

mp3filename = f"{file_path}.mp3"
mp3file_path = path.join(mp3filename)
path_files.add(f"{file_name}.mp3")

if kwargs["no_overwrites"] and not kwargs["skip_mp3"] and path.exists(mp3file_path):
print(f'File {mp3filename} already exists, we do not overwrite it ')
print(f'File {mp3file_path} already exists, we do not overwrite it ')
continue

outtmpl = f"{file_path}.%(ext)s"
Expand Down Expand Up @@ -192,7 +201,13 @@ def find_and_download_songs(kwargs):
log.debug(e)
print(f"Failed to download {name}, make sure yt_dlp is up to date")
if not kwargs["skip_mp3"]:
set_tags(temp, mp3filename, kwargs)
set_tags(temp, mp3file_path, kwargs)
if kwargs["remove_trailing_tracks"]:
for save_path in files:
for f in os.listdir(save_path):
if f not in files[save_path]:
print(f"File {f} is not in the playlist anymore, we delete it")
os.remove(path.join(save_path, f))


def multicore_find_and_download_songs(kwargs):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_download_one_false_skip():
skip_mp3=False,
keep_playlist_order=False,
no_overwrites=False,
remove_trailing_tracks=False,
use_sponsorblock="no",
file_name_f=yt.default_filename,
multi_core=0,
Expand Down Expand Up @@ -94,6 +95,7 @@ def test_download_one_true_skip():
skip_mp3=True,
keep_playlist_order=False,
no_overwrites=False,
remove_trailing_tracks=False,
use_sponsorblock="yes",
file_name_f=yt.default_filename,
multi_core=0,
Expand Down Expand Up @@ -130,6 +132,7 @@ def test_download_cover_none():
skip_mp3=False,
keep_playlist_order=False,
no_overwrites=False,
remove_trailing_tracks=False,
use_sponsorblock="no",
file_name_f=yt.default_filename,
multi_core=0,
Expand Down

0 comments on commit f2beda1

Please sign in to comment.