Skip to content

Commit

Permalink
Adding spotify-backup license and reference, reformatting with black
Browse files Browse the repository at this point in the history
  • Loading branch information
linsomniac committed Feb 4, 2024
1 parent a9a375b commit 8407fee
Show file tree
Hide file tree
Showing 6 changed files with 423 additions and 288 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,7 @@ This is mostly for debugging, but there is a command to search for tracks in YTM

Creative Commons Zero v1.0 Universal

spotify-backup.py licensed under MIT License
See https://github.com/caseychu/spotify-backup for more information

[//]: # ( vim: set tw=90 ts=4 sw=4 ai: )
68 changes: 43 additions & 25 deletions spotify2ytmusic/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ def get_playlist_id_by_name(yt: YTMusic, title: str) -> Optional[str]:
return None


def lookup_song(yt: YTMusic, track_name: str, artist_name: str, album_name, yt_search_algo: int) -> dict:
def lookup_song(
yt: YTMusic, track_name: str, artist_name: str, album_name, yt_search_algo: int
) -> dict:
"""Look up a song on YTMusic
Given the Spotify track information, it does a lookup for the album by the same
Expand Down Expand Up @@ -188,12 +190,11 @@ def lookup_song(yt: YTMusic, track_name: str, artist_name: str, album_name, yt_s
print(f"Unable to lookup album ({e}), continuing...")

songs = yt.search(query=f"{track_name} by {artist_name}", filter="songs")

match yt_search_algo:

case 0:
return songs[0]

case 1:
for song in songs:
if (
Expand All @@ -204,24 +205,28 @@ def lookup_song(yt: YTMusic, track_name: str, artist_name: str, album_name, yt_s
return song
# print(f"SONG: {song['videoId']} - {song['title']} - {song['artists'][0]['name']} - {song['album']['name']}")

raise ValueError(f"Did not find {track_name} by {artist_name} from {album_name}")

raise ValueError(
f"Did not find {track_name} by {artist_name} from {album_name}"
)

case 2:
# This would need to do fuzzy matching
for song in songs:
# Remove everything in brackets in the song title
song_title_without_brackets = re.sub(r'[\[\(].*?[\]\)]', '', song["title"])
if ((
song_title_without_brackets == track_name
and song["album"]["name"] == album_name
) or (
song_title_without_brackets == track_name
) or (
song_title_without_brackets in track_name
) or (
track_name in song_title_without_brackets
)) and (
song["artists"][0]["name"] == artist_name or artist_name in song["artists"][0]["name"]
song_title_without_brackets = re.sub(
r"[\[\(].*?[\]\)]", "", song["title"]
)
if (
(
song_title_without_brackets == track_name
and song["album"]["name"] == album_name
)
or (song_title_without_brackets == track_name)
or (song_title_without_brackets in track_name)
or (track_name in song_title_without_brackets)
) and (
song["artists"][0]["name"] == artist_name
or artist_name in song["artists"][0]["name"]
):
return song

Expand All @@ -230,22 +235,35 @@ def lookup_song(yt: YTMusic, track_name: str, artist_name: str, album_name, yt_s
else:
track_name = track_name.lower()
first_song_title = songs[0]["title"].lower()
if track_name not in first_song_title or songs[0]["artists"][0]["name"] != artist_name: # If the first song is not the one we are looking for
if (
track_name not in first_song_title
or songs[0]["artists"][0]["name"] != artist_name
): # If the first song is not the one we are looking for
print("Not found in songs, searching videos")
new_songs = yt.search(query=f"{track_name} by {artist_name}", filter="videos") # Search videos

new_songs = yt.search(
query=f"{track_name} by {artist_name}", filter="videos"
) # Search videos

# From here, we search for videos reposting the song. They often contain the name of it and the artist. Like with 'Nekfeu - Ecrire'.
for new_song in new_songs:
new_song_title = new_song["title"].lower() # People sometimes mess up the capitalization in the title
if (track_name in new_song_title and artist_name in new_song_title) or (track_name in new_song_title):
new_song_title = new_song[
"title"
].lower() # People sometimes mess up the capitalization in the title
if (
track_name in new_song_title
and artist_name in new_song_title
) or (track_name in new_song_title):
print("Found a video")
return new_song
else:
else:
# Basically we only get here if the song isnt present anywhere on youtube
raise ValueError(f"Did not find {track_name} by {artist_name} from {album_name}")
raise ValueError(
f"Did not find {track_name} by {artist_name} from {album_name}"
)
else:
return songs[0]


def copier(
src_tracks: Iterator[SongInfo],
dst_pl_id: Optional[str] = None,
Expand Down
1 change: 0 additions & 1 deletion spotify2ytmusic/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ def parse_arguments():
None,
args.dry_run,
args.track_sleep,

)


Expand Down
Loading

0 comments on commit 8407fee

Please sign in to comment.