Skip to content

Commit

Permalink
Two minor fixes (#377)
Browse files Browse the repository at this point in the history
* fix crash when no search results are found

* fix shutil.SameFileError while moving reference log

Refs: Maritsu@e05d9e4#commitcomment-144139425

* try to search for videos if no song is found instead of skipping
  • Loading branch information
Maritsu authored Jul 12, 2024
1 parent e05d9e4 commit e74a4d4
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ def dump_json(songs):
ytJson = {}
with ytmusicapi.YTMusic() as ym:
# Reduce results to array of titles and video IDs
search_results = ym.search(query, filter="songs")
if len(search_results) == 0:
log.warning("No song results found for %s, retrying.", query)
search_results = ym.search(query, filter="videos")
if len(search_results) == 0:
log.error("No search results found for %s, skipping.", query)
continue
result_titles, result_ids = zip(*map(
lambda d: (f"{d['artists'][0]['name']} - {d['title']}".replace(":", "").replace('"', ""), d["videoId"]),
ym.search(query, filter="songs")
search_results
))
# Get ID of closest matching result by finding index in titles list
videoId = result_ids[result_titles.index(get_closest_match(result_titles, query))]
Expand Down Expand Up @@ -214,9 +221,16 @@ def find_and_download_songs(kwargs):

with ytmusicapi.YTMusic() as ym:
# Reduce search results to array of titles and video IDs
search_results = ym.search(query, filter="songs")
if len(search_results) == 0:
log.warning("No song results found for %s, retrying.", query)
search_results = ym.search(query, filter="videos")
if len(search_results) == 0:
log.error("No search results found for %s, skipping.", query)
continue
result_titles, result_ids = zip(*map(
lambda d: (f"{d['artists'][0]['name']} - {d['title']}".replace(":", "").replace('"', ""), d["videoId"]),
ym.search(query, filter="songs")
search_results
))
# Get ID of closest matching result by finding index in titles list
video_id = result_ids[result_titles.index(get_closest_match(result_titles, query))]
Expand Down Expand Up @@ -335,8 +349,9 @@ def download_songs(**kwargs):
log.debug("Downloading to %s", url["save_path"])
reference_file = DOWNLOAD_LIST
track_db = write_tracks(reference_file, kwargs["songs"])
shutil.copy(reference_file, kwargs["output_dir"] + "/" + reference_file)
os.remove(reference_file)
if not shutil._samefile(reference_file, kwargs["output_dir"] + "/" + reference_file):
shutil.copy(reference_file, kwargs["output_dir"] + "/" + reference_file)
os.remove(reference_file)
reference_file = str(kwargs["output_dir"]) + "/" + reference_file
kwargs["reference_file"] = reference_file
kwargs["track_db"] = track_db
Expand Down

0 comments on commit e74a4d4

Please sign in to comment.