Skip to content

Commit

Permalink
Add tempo information to track metadata (#368)
Browse files Browse the repository at this point in the history
* Add tempo information to track metadata

* cleanup

* check for analysis error for local files

* add proper testcases

* Fix tempo handling for test cases

* remove .cache file

* Update test_spotify_fetch_tracks.py

* Update youtube.py
  • Loading branch information
krishsharma0413 authored Jan 1, 2024
1 parent b56afb2 commit a0d4bcc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
23 changes: 22 additions & 1 deletion spotify_dl/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +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_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")]
)
Expand Down Expand Up @@ -86,6 +92,7 @@ def fetch_tracks(sp, item_type, item_id):
"genre": genre,
"spotify_id": spotify_id,
"track_url": None,
"tempo": tempo,
}
)
offset += 1
Expand Down Expand Up @@ -141,6 +148,12 @@ def fetch_tracks(sp, item_type, item_id):
)
track_num = item["track_number"]
spotify_id = item.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
songs_list.append(
{
"name": track_name,
Expand All @@ -154,6 +167,7 @@ def fetch_tracks(sp, item_type, item_id):
"cover": cover,
"genre": genre,
"spotify_id": spotify_id,
"tempo": tempo,
}
)
offset += 1
Expand Down Expand Up @@ -182,6 +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"]
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:
Expand All @@ -203,6 +223,7 @@ def fetch_tracks(sp, item_type, item_id):
"genre": genre,
"track_url": None,
"spotify_id": spotify_id,
"tempo": tempo,
}
)

Expand Down
5 changes: 4 additions & 1 deletion spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ 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"]
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
Expand All @@ -81,6 +81,7 @@ def write_tracks(tracks_file, song_dict):
track_url,
str(track_num),
track_album,
str(track_tempo),
str(track_index),
]
try:
Expand Down Expand Up @@ -119,6 +120,8 @@ def set_tags(temp, filename, kwargs):
)

song_file["genre"] = song.get("genre")
if song.get("tempo") is not None:
song_file["bpm"] = str(song.get("tempo"))
song_file.save()
song_file = MP3(filename, ID3=ID3)
cover = song.get("cover")
Expand Down
28 changes: 27 additions & 1 deletion tests/test_spotify_fetch_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_spotify_playlist_fetch_one():
"track_url": None,
"playlist_num": 1,
"spotify_id": "2GpBrAoCwt48fxjgjlzMd4",
'tempo': 74.656,
} == songs[0]


Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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 "
Expand All @@ -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)",
Expand All @@ -107,6 +112,7 @@ def test_spotify_playlist_fetch_more():
"year": "2007",
"playlist_num": 5,
"spotify_id": "2E2znCPaS8anQe21GLxcvJ",
'tempo': 122.318,
},
] == songs

Expand All @@ -128,6 +134,7 @@ def test_spotify_track_fetch_one():
"track_url": None,
"playlist_num": 1,
"spotify_id": "2GpBrAoCwt48fxjgjlzMd4",
'tempo': 74.656,
} == songs[0]


Expand All @@ -148,6 +155,7 @@ def test_spotify_album_fetch_one():
"year": "2012",
"playlist_num": 1,
"spotify_id": "5EoKQDGE2zxrTfRFZF52u5",
'tempo': 120.009,
} == songs[0]


Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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
Expand All @@ -387,5 +412,6 @@ def test_spotify_playlist_fetch_local_file():
"year": "",
"playlist_num": 1,
"spotify_id": None,
"tempo": None,
}
] == songs
] == songs
3 changes: 3 additions & 0 deletions tests/test_youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
],
}
Expand Down Expand Up @@ -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,
}
],
}
Expand Down Expand Up @@ -120,6 +122,7 @@ def test_download_cover_none():
"cover": None,
"genre": "classic rock",
"spotify_id": "12LhScrlYazmU4vsqpRQNI",
'tempo': 159.15,
}
],
}
Expand Down

0 comments on commit a0d4bcc

Please sign in to comment.