Skip to content

Commit

Permalink
Made changes to --archive to allow saving the links to downloaded son…
Browse files Browse the repository at this point in the history
…gs incrementally. Closes #2196
  • Loading branch information
Shajal-Kumar committed Nov 2, 2024
1 parent 237bc56 commit ad080be
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions spotdl/download/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ def __init__(
# Initialize archive
self.url_archive = Archive()
if self.settings["archive"]:
# Ensure the archive file is created if it doesn't exist
self.url_archive.initialize(self.settings["archive"])
# Attempt to load the existing URLs from the archive
self.url_archive.load(self.settings["archive"])

logger.debug("Archive: %d urls", len(self.url_archive))
Expand Down Expand Up @@ -291,9 +294,6 @@ def download_multiple_songs(
songs = [song for song in songs if song.url not in self.url_archive]
logger.debug("Filtered %d songs with archive", len(songs))

# Initialize the archive file (create it if it doesn't exist)
self.url_archive.initialize(self.settings["archive"])

self.progress_handler.set_song_count(len(songs))

results = []
Expand All @@ -318,23 +318,28 @@ def download_multiple_songs(

except Exception as e:
logger.error(f"An error occurred: {str(e)}")

finally:
# Save archive incrementally after each successful download
if self.settings["archive"]:
for result in results:
# Check if the download was successful
if result[1] or self.settings["add_unavailable"]:
# Add the URL to the archive and write to the file
self.url_archive.add(result[0].url)
# Call the add_entry function to update the archive and flush
self.url_archive.add_entry(self.settings["archive"], result[0].url)
self.url_archive.add_entry(
self.settings["archive"], result[0].url
)

logger.info(
"Archive saved with %d URLs", len(self.url_archive)
)
logger.info("Archive saved with %d URLs", len(self.url_archive))

# Create m3u playlist
if self.settings["m3u"]:
song_list = [song for song, path in results if path or self.settings["add_unavailable"]]
song_list = [
song
for song, path in results
if path or self.settings["add_unavailable"]
]
gen_m3u_files(
song_list,
self.settings["m3u"],
Expand Down
2 changes: 1 addition & 1 deletion spotdl/utils/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ def add_entry(self, file: str, url: str) -> None:
"""
with open(file, "a", encoding="utf-8") as archive:
archive.write(f"{url}\n")
archive.flush()
archive.flush()

0 comments on commit ad080be

Please sign in to comment.