From 80b92c704b971059178ff44ee2f855f1df1455f6 Mon Sep 17 00:00:00 2001 From: krishsharma0413 Date: Sat, 30 Dec 2023 17:28:44 +0530 Subject: [PATCH 1/8] Add tempo information to track metadata --- spotify_dl/spotify.py | 9 +++++++++ spotify_dl/youtube.py | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spotify_dl/spotify.py b/spotify_dl/spotify.py index f777a59..04a15c8 100644 --- a/spotify_dl/spotify.py +++ b/spotify_dl/spotify.py @@ -43,6 +43,8 @@ def fetch_tracks(sp, item_type, item_id): track_album_info = track_info.get("album") track_num = track_info.get("track_number") spotify_id = track_info.get("id") + track_audio_data = sp.audio_analysis(spotify_id) + tempo = track_audio_data.get("track").get("tempo") track_name = track_info.get("name") track_artist = ", ".join( [artist["name"] for artist in track_info.get("artists")] @@ -86,6 +88,7 @@ def fetch_tracks(sp, item_type, item_id): "genre": genre, "spotify_id": spotify_id, "track_url": None, + "tempo": tempo, } ) offset += 1 @@ -141,6 +144,8 @@ def fetch_tracks(sp, item_type, item_id): ) track_num = item["track_number"] spotify_id = item.get("id") + track_audio_data = sp.audio_analysis(spotify_id) + tempo = track_audio_data.get("track").get("tempo") songs_list.append( { "name": track_name, @@ -154,6 +159,7 @@ def fetch_tracks(sp, item_type, item_id): "cover": cover, "genre": genre, "spotify_id": spotify_id, + "tempo": tempo, } ) offset += 1 @@ -182,6 +188,8 @@ def fetch_tracks(sp, item_type, item_id): album_total = album_info.get("total_tracks") track_num = items["track_number"] spotify_id = items["id"] + track_audio_data = sp.audio_analysis(spotify_id) + tempo = track_audio_data.get("track").get("tempo") if len(items["album"]["images"]) > 0: cover = items["album"]["images"][0]["url"] else: @@ -203,6 +211,7 @@ def fetch_tracks(sp, item_type, item_id): "genre": genre, "track_url": None, "spotify_id": spotify_id, + "tempo": tempo, } ) diff --git a/spotify_dl/youtube.py b/spotify_dl/youtube.py index e0f47f5..3d84a1b 100644 --- a/spotify_dl/youtube.py +++ b/spotify_dl/youtube.py @@ -71,6 +71,7 @@ def write_tracks(tracks_file, song_dict): track_artist = track["artist"] track_num = track["num"] track_album = track["album"] + track_tempo = track["tempo"] track["save_path"] = url_dict["save_path"] track_db.append(track) track_index = i @@ -81,6 +82,7 @@ def write_tracks(tracks_file, song_dict): track_url, str(track_num), track_album, + str(track_tempo), str(track_index), ] try: @@ -119,6 +121,7 @@ def set_tags(temp, filename, kwargs): ) song_file["genre"] = song.get("genre") + song_file["bpm"] = str(song.get("tempo")) song_file.save() song_file = MP3(filename, ID3=ID3) cover = song.get("cover") @@ -150,10 +153,11 @@ def find_and_download_songs(kwargs): with open(reference_file, "r", encoding="utf-8") as file: for line in file: temp = line.split(";") - name, artist, album, i = ( + name, artist, album, tempo, i = ( temp[0], temp[1], temp[4], + temp[5], int(temp[-1].replace("\n", "")), ) From 35429d45e89cf9ae7c199d3919861ebffc043326 Mon Sep 17 00:00:00 2001 From: krishsharma0413 Date: Sat, 30 Dec 2023 17:43:28 +0530 Subject: [PATCH 2/8] cleanup --- .cache | 1 + spotify_dl/youtube.py | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 .cache diff --git a/.cache b/.cache new file mode 100644 index 0000000..418b9f4 --- /dev/null +++ b/.cache @@ -0,0 +1 @@ +{"access_token": "BQBOFvTjRqI66SdCAo12CxZIMju5q7CUVspG68WVrnZLPdtcd4wZVq5Ky9rdHrAfCUVlS1zus9tbzu-qP9vPmZ9tAYAKbQd26A7d7UReHyzcHSqLq60", "token_type": "Bearer", "expires_in": 3600, "expires_at": 1703941566} \ No newline at end of file diff --git a/spotify_dl/youtube.py b/spotify_dl/youtube.py index 3d84a1b..db04af2 100644 --- a/spotify_dl/youtube.py +++ b/spotify_dl/youtube.py @@ -64,7 +64,6 @@ def write_tracks(tracks_file, song_dict): i = 0 writer = csv.writer(file_out, delimiter=";") for url_dict in song_dict["urls"]: - # for track in url_dict['songs']: for track in url_dict["songs"]: track_url = track["track_url"] # here track_name = track["name"] @@ -153,11 +152,10 @@ def find_and_download_songs(kwargs): with open(reference_file, "r", encoding="utf-8") as file: for line in file: temp = line.split(";") - name, artist, album, tempo, i = ( + name, artist, album, i = ( temp[0], temp[1], temp[4], - temp[5], int(temp[-1].replace("\n", "")), ) From 097e25eecc22e77be12f97d00ff5bb98e6595c77 Mon Sep 17 00:00:00 2001 From: krishsharma0413 Date: Sat, 30 Dec 2023 19:09:52 +0530 Subject: [PATCH 3/8] check for analysis error for local files --- spotify_dl/spotify.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/spotify_dl/spotify.py b/spotify_dl/spotify.py index 04a15c8..448d5e4 100644 --- a/spotify_dl/spotify.py +++ b/spotify_dl/spotify.py @@ -42,10 +42,14 @@ def fetch_tracks(sp, item_type, item_id): continue track_album_info = track_info.get("album") track_num = track_info.get("track_number") - spotify_id = track_info.get("id") - track_audio_data = sp.audio_analysis(spotify_id) - tempo = track_audio_data.get("track").get("tempo") track_name = track_info.get("name") + spotify_id = track_info.get("id") + try: + track_audio_data = sp.audio_analysis(spotify_id) + tempo = track_audio_data.get("track").get("tempo") + except: + log.error("Couldn't fetch audio analysis for %s", track_name) + tempo = None track_artist = ", ".join( [artist["name"] for artist in track_info.get("artists")] ) @@ -144,8 +148,12 @@ def fetch_tracks(sp, item_type, item_id): ) track_num = item["track_number"] spotify_id = item.get("id") - track_audio_data = sp.audio_analysis(spotify_id) - tempo = track_audio_data.get("track").get("tempo") + try: + track_audio_data = sp.audio_analysis(spotify_id) + tempo = track_audio_data.get("track").get("tempo") + except: + log.error("Couldn't fetch audio analysis for %s", track_name) + tempo = None songs_list.append( { "name": track_name, @@ -188,8 +196,12 @@ def fetch_tracks(sp, item_type, item_id): album_total = album_info.get("total_tracks") track_num = items["track_number"] spotify_id = items["id"] - track_audio_data = sp.audio_analysis(spotify_id) - tempo = track_audio_data.get("track").get("tempo") + try: + track_audio_data = sp.audio_analysis(spotify_id) + tempo = track_audio_data.get("track").get("tempo") + except: + log.error("Couldn't fetch audio analysis for %s", track_name) + tempo = None if len(items["album"]["images"]) > 0: cover = items["album"]["images"][0]["url"] else: From faf4617e20431771a3a96994ae58f4fc739b203c Mon Sep 17 00:00:00 2001 From: krishsharma0413 Date: Sat, 30 Dec 2023 19:10:39 +0530 Subject: [PATCH 4/8] add proper testcases --- tests/test_spotify_fetch_tracks.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/test_spotify_fetch_tracks.py b/tests/test_spotify_fetch_tracks.py index 89b2ed6..ee6be9c 100644 --- a/tests/test_spotify_fetch_tracks.py +++ b/tests/test_spotify_fetch_tracks.py @@ -32,6 +32,7 @@ def test_spotify_playlist_fetch_one(): "track_url": None, "playlist_num": 1, "spotify_id": "2GpBrAoCwt48fxjgjlzMd4", + 'tempo': 74.656, } == songs[0] @@ -53,6 +54,7 @@ def test_spotify_playlist_fetch_more(): "year": "2012", "playlist_num": 1, "spotify_id": "4rzfv0JLZfVhOhbSQ8o5jZ", + 'tempo': 135.016, }, { "album": "Wellness & Dreaming Source", @@ -66,6 +68,7 @@ def test_spotify_playlist_fetch_more(): "playlist_num": 2, "track_url": None, "spotify_id": "5o3jMYOSbaVz3tkgwhELSV", + 'tempo': 137.805, }, { "album": "This Is Happening", @@ -79,6 +82,7 @@ def test_spotify_playlist_fetch_more(): "year": "2010", "playlist_num": 3, "spotify_id": "4Cy0NHJ8Gh0xMdwyM9RkQm", + 'tempo': 134.99, }, { "album": "Glenn Horiuchi Trio / Gelenn Horiuchi Quartet: Mercy / Jump Start " @@ -94,6 +98,7 @@ def test_spotify_playlist_fetch_more(): "track_url": None, "playlist_num": 4, "spotify_id": "6hvFrZNocdt2FcKGCSY5NI", + 'tempo': 114.767, }, { "album": "All The Best (Spanish Version)", @@ -107,6 +112,7 @@ def test_spotify_playlist_fetch_more(): "year": "2007", "playlist_num": 5, "spotify_id": "2E2znCPaS8anQe21GLxcvJ", + 'tempo': 122.318, }, ] == songs @@ -128,6 +134,7 @@ def test_spotify_track_fetch_one(): "track_url": None, "playlist_num": 1, "spotify_id": "2GpBrAoCwt48fxjgjlzMd4", + 'tempo': 74.656, } == songs[0] @@ -148,6 +155,7 @@ def test_spotify_album_fetch_one(): "year": "2012", "playlist_num": 1, "spotify_id": "5EoKQDGE2zxrTfRFZF52u5", + 'tempo': 120.009, } == songs[0] @@ -169,6 +177,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 1, "spotify_id": "69Yw7H4bRIwfIxL0ZCZy8y", + 'tempo': 120.955, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -182,6 +191,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 2, "spotify_id": "5GGSjXZeTgX9sKYBtl8K6U", + 'tempo': 147.384, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -195,6 +205,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 3, "spotify_id": "0Ssh20fuVhmasLRJ97MLnp", + 'tempo': 152.769, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -208,6 +219,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 4, "spotify_id": "2LasW39KJDE4VH9hTVNpE2", + 'tempo': 115.471, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -221,6 +233,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 5, "spotify_id": "6jXrIu3hWbmJziw34IHIwM", + 'tempo': 145.124, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -234,6 +247,7 @@ def test_spotify_album_fetch_more(): "track_url": None, "playlist_num": 6, "spotify_id": "5dHmGuUeRgp5f93G69tox5", + 'tempo': 108.544, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -247,6 +261,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 7, "spotify_id": "2KPj0oB7cUuHQ3FuardOII", + 'tempo': 159.156, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -260,6 +275,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 8, "spotify_id": "34CcBjL9WqEAtnl2i6Hbxa", + 'tempo': 118.48, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -273,6 +289,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 9, "spotify_id": "1x9ak6LGIazLhfuaSIEkhG", + 'tempo': 112.623, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -286,6 +303,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 10, "spotify_id": "4CITL18Tos0PscW1amCK4j", + 'tempo': 145.497, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -299,6 +317,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 11, "spotify_id": "1e9Tt3nKBwRbuaU79kN3dn", + 'tempo': 126.343, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -312,6 +331,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 12, "spotify_id": "0uHqoDT7J2TYBsJx6m4Tvi", + 'tempo': 172.274, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -325,6 +345,8 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 13, "spotify_id": "3MIueGYoNiyBNfi5ukDgAK", + 'tempo': 146.712, + }, { "album": "Queen II (Deluxe Remastered Version)", @@ -338,6 +360,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 14, "spotify_id": "34WAOFWdJ83a3YYrDAZTjm", + 'tempo': 128.873, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -351,6 +374,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 15, "spotify_id": "2AFIPUlApcUwGEgOSDwoBz", + 'tempo': 122.986, }, { "album": "Queen II (Deluxe Remastered Version)", @@ -364,6 +388,7 @@ def test_spotify_album_fetch_more(): "year": "1974", "playlist_num": 16, "spotify_id": "4G4Sf18XkFvNTV5vAxiQyd", + 'tempo': 169.166, }, ] == songs assert (len(songs)) == 16 @@ -374,6 +399,7 @@ def test_spotify_playlist_fetch_local_file(): url = "https://open.spotify.com/playlist/1TWZ36xJ8qkvSeAQQUvU5b?si=ad56b6bb085b4ab9" item_type = "playlist" songs = fetch_tracks(sp, item_type, url) + print(songs) assert [ { "album": "Yoshi's Island", @@ -387,5 +413,6 @@ def test_spotify_playlist_fetch_local_file(): "year": "", "playlist_num": 1, "spotify_id": None, + "tempo": None, } - ] == songs + ] == songs \ No newline at end of file From 73aef9bd50ea8dfd934edd4516d77fceda1c13bb Mon Sep 17 00:00:00 2001 From: krishsharma0413 Date: Sun, 31 Dec 2023 12:51:41 +0530 Subject: [PATCH 5/8] Fix tempo handling for test cases --- spotify_dl/youtube.py | 3 ++- tests/test_youtube.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/spotify_dl/youtube.py b/spotify_dl/youtube.py index db04af2..e38bf83 100644 --- a/spotify_dl/youtube.py +++ b/spotify_dl/youtube.py @@ -120,7 +120,8 @@ def set_tags(temp, filename, kwargs): ) song_file["genre"] = song.get("genre") - song_file["bpm"] = str(song.get("tempo")) + if song.get("tempo") != None: + song_file["bpm"] = str(song.get("tempo")) song_file.save() song_file = MP3(filename, ID3=ID3) cover = song.get("cover") diff --git a/tests/test_youtube.py b/tests/test_youtube.py index d3c29d6..38f0f38 100644 --- a/tests/test_youtube.py +++ b/tests/test_youtube.py @@ -28,6 +28,7 @@ def test_download_one_false_skip(): "cover": "https://i.scdn.co/image/ab67616d0000b27396d28597a5ae44ab66552183", "genre": "album rock", "spotify_id": "2GpBrAoCwt48fxjgjlzMd4", + 'tempo': 74.656, } ], } @@ -83,6 +84,7 @@ def test_download_one_true_skip(): "cover": "https://i.scdn.co/image/ab67616d0000b27396d28597a5ae44ab66552183", "genre": "album rock", "spotify_id": "2GpBrAoCwt48fxjgjlzMd4", + 'tempo': 74.656, } ], } @@ -120,6 +122,7 @@ def test_download_cover_none(): "cover": None, "genre": "classic rock", "spotify_id": "12LhScrlYazmU4vsqpRQNI", + 'tempo': 159.15, } ], } From e3b3b2f651bcb61d2d6924907adb33f8f76956a8 Mon Sep 17 00:00:00 2001 From: krishsharma0413 Date: Sun, 31 Dec 2023 12:52:25 +0530 Subject: [PATCH 6/8] remove .cache file --- .cache | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .cache diff --git a/.cache b/.cache deleted file mode 100644 index 418b9f4..0000000 --- a/.cache +++ /dev/null @@ -1 +0,0 @@ -{"access_token": "BQBOFvTjRqI66SdCAo12CxZIMju5q7CUVspG68WVrnZLPdtcd4wZVq5Ky9rdHrAfCUVlS1zus9tbzu-qP9vPmZ9tAYAKbQd26A7d7UReHyzcHSqLq60", "token_type": "Bearer", "expires_in": 3600, "expires_at": 1703941566} \ No newline at end of file From 00db1067cf426ee5a3855ace0714b1055eebc440 Mon Sep 17 00:00:00 2001 From: krish Date: Mon, 1 Jan 2024 12:39:47 +0530 Subject: [PATCH 7/8] Update test_spotify_fetch_tracks.py --- tests/test_spotify_fetch_tracks.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_spotify_fetch_tracks.py b/tests/test_spotify_fetch_tracks.py index ee6be9c..7addca4 100644 --- a/tests/test_spotify_fetch_tracks.py +++ b/tests/test_spotify_fetch_tracks.py @@ -399,7 +399,6 @@ def test_spotify_playlist_fetch_local_file(): url = "https://open.spotify.com/playlist/1TWZ36xJ8qkvSeAQQUvU5b?si=ad56b6bb085b4ab9" item_type = "playlist" songs = fetch_tracks(sp, item_type, url) - print(songs) assert [ { "album": "Yoshi's Island", From 69e1299304b872f62076a6c520f075c3a38af09f Mon Sep 17 00:00:00 2001 From: krish Date: Mon, 1 Jan 2024 12:40:33 +0530 Subject: [PATCH 8/8] Update youtube.py --- spotify_dl/youtube.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spotify_dl/youtube.py b/spotify_dl/youtube.py index e38bf83..f3b6846 100644 --- a/spotify_dl/youtube.py +++ b/spotify_dl/youtube.py @@ -120,7 +120,7 @@ def set_tags(temp, filename, kwargs): ) song_file["genre"] = song.get("genre") - if song.get("tempo") != None: + if song.get("tempo") is not None: song_file["bpm"] = str(song.get("tempo")) song_file.save() song_file = MP3(filename, ID3=ID3)