Skip to content

Commit a0d4bcc

Browse files
Add tempo information to track metadata (#368)
* 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
1 parent b56afb2 commit a0d4bcc

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

spotify_dl/spotify.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ def fetch_tracks(sp, item_type, item_id):
4242
continue
4343
track_album_info = track_info.get("album")
4444
track_num = track_info.get("track_number")
45-
spotify_id = track_info.get("id")
4645
track_name = track_info.get("name")
46+
spotify_id = track_info.get("id")
47+
try:
48+
track_audio_data = sp.audio_analysis(spotify_id)
49+
tempo = track_audio_data.get("track").get("tempo")
50+
except:
51+
log.error("Couldn't fetch audio analysis for %s", track_name)
52+
tempo = None
4753
track_artist = ", ".join(
4854
[artist["name"] for artist in track_info.get("artists")]
4955
)
@@ -86,6 +92,7 @@ def fetch_tracks(sp, item_type, item_id):
8692
"genre": genre,
8793
"spotify_id": spotify_id,
8894
"track_url": None,
95+
"tempo": tempo,
8996
}
9097
)
9198
offset += 1
@@ -141,6 +148,12 @@ def fetch_tracks(sp, item_type, item_id):
141148
)
142149
track_num = item["track_number"]
143150
spotify_id = item.get("id")
151+
try:
152+
track_audio_data = sp.audio_analysis(spotify_id)
153+
tempo = track_audio_data.get("track").get("tempo")
154+
except:
155+
log.error("Couldn't fetch audio analysis for %s", track_name)
156+
tempo = None
144157
songs_list.append(
145158
{
146159
"name": track_name,
@@ -154,6 +167,7 @@ def fetch_tracks(sp, item_type, item_id):
154167
"cover": cover,
155168
"genre": genre,
156169
"spotify_id": spotify_id,
170+
"tempo": tempo,
157171
}
158172
)
159173
offset += 1
@@ -182,6 +196,12 @@ def fetch_tracks(sp, item_type, item_id):
182196
album_total = album_info.get("total_tracks")
183197
track_num = items["track_number"]
184198
spotify_id = items["id"]
199+
try:
200+
track_audio_data = sp.audio_analysis(spotify_id)
201+
tempo = track_audio_data.get("track").get("tempo")
202+
except:
203+
log.error("Couldn't fetch audio analysis for %s", track_name)
204+
tempo = None
185205
if len(items["album"]["images"]) > 0:
186206
cover = items["album"]["images"][0]["url"]
187207
else:
@@ -203,6 +223,7 @@ def fetch_tracks(sp, item_type, item_id):
203223
"genre": genre,
204224
"track_url": None,
205225
"spotify_id": spotify_id,
226+
"tempo": tempo,
206227
}
207228
)
208229

spotify_dl/youtube.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ def write_tracks(tracks_file, song_dict):
6464
i = 0
6565
writer = csv.writer(file_out, delimiter=";")
6666
for url_dict in song_dict["urls"]:
67-
# for track in url_dict['songs']:
6867
for track in url_dict["songs"]:
6968
track_url = track["track_url"] # here
7069
track_name = track["name"]
7170
track_artist = track["artist"]
7271
track_num = track["num"]
7372
track_album = track["album"]
73+
track_tempo = track["tempo"]
7474
track["save_path"] = url_dict["save_path"]
7575
track_db.append(track)
7676
track_index = i
@@ -81,6 +81,7 @@ def write_tracks(tracks_file, song_dict):
8181
track_url,
8282
str(track_num),
8383
track_album,
84+
str(track_tempo),
8485
str(track_index),
8586
]
8687
try:
@@ -119,6 +120,8 @@ def set_tags(temp, filename, kwargs):
119120
)
120121

121122
song_file["genre"] = song.get("genre")
123+
if song.get("tempo") is not None:
124+
song_file["bpm"] = str(song.get("tempo"))
122125
song_file.save()
123126
song_file = MP3(filename, ID3=ID3)
124127
cover = song.get("cover")

tests/test_spotify_fetch_tracks.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_spotify_playlist_fetch_one():
3232
"track_url": None,
3333
"playlist_num": 1,
3434
"spotify_id": "2GpBrAoCwt48fxjgjlzMd4",
35+
'tempo': 74.656,
3536
} == songs[0]
3637

3738

@@ -53,6 +54,7 @@ def test_spotify_playlist_fetch_more():
5354
"year": "2012",
5455
"playlist_num": 1,
5556
"spotify_id": "4rzfv0JLZfVhOhbSQ8o5jZ",
57+
'tempo': 135.016,
5658
},
5759
{
5860
"album": "Wellness & Dreaming Source",
@@ -66,6 +68,7 @@ def test_spotify_playlist_fetch_more():
6668
"playlist_num": 2,
6769
"track_url": None,
6870
"spotify_id": "5o3jMYOSbaVz3tkgwhELSV",
71+
'tempo': 137.805,
6972
},
7073
{
7174
"album": "This Is Happening",
@@ -79,6 +82,7 @@ def test_spotify_playlist_fetch_more():
7982
"year": "2010",
8083
"playlist_num": 3,
8184
"spotify_id": "4Cy0NHJ8Gh0xMdwyM9RkQm",
85+
'tempo': 134.99,
8286
},
8387
{
8488
"album": "Glenn Horiuchi Trio / Gelenn Horiuchi Quartet: Mercy / Jump Start "
@@ -94,6 +98,7 @@ def test_spotify_playlist_fetch_more():
9498
"track_url": None,
9599
"playlist_num": 4,
96100
"spotify_id": "6hvFrZNocdt2FcKGCSY5NI",
101+
'tempo': 114.767,
97102
},
98103
{
99104
"album": "All The Best (Spanish Version)",
@@ -107,6 +112,7 @@ def test_spotify_playlist_fetch_more():
107112
"year": "2007",
108113
"playlist_num": 5,
109114
"spotify_id": "2E2znCPaS8anQe21GLxcvJ",
115+
'tempo': 122.318,
110116
},
111117
] == songs
112118

@@ -128,6 +134,7 @@ def test_spotify_track_fetch_one():
128134
"track_url": None,
129135
"playlist_num": 1,
130136
"spotify_id": "2GpBrAoCwt48fxjgjlzMd4",
137+
'tempo': 74.656,
131138
} == songs[0]
132139

133140

@@ -148,6 +155,7 @@ def test_spotify_album_fetch_one():
148155
"year": "2012",
149156
"playlist_num": 1,
150157
"spotify_id": "5EoKQDGE2zxrTfRFZF52u5",
158+
'tempo': 120.009,
151159
} == songs[0]
152160

153161

@@ -169,6 +177,7 @@ def test_spotify_album_fetch_more():
169177
"year": "1974",
170178
"playlist_num": 1,
171179
"spotify_id": "69Yw7H4bRIwfIxL0ZCZy8y",
180+
'tempo': 120.955,
172181
},
173182
{
174183
"album": "Queen II (Deluxe Remastered Version)",
@@ -182,6 +191,7 @@ def test_spotify_album_fetch_more():
182191
"year": "1974",
183192
"playlist_num": 2,
184193
"spotify_id": "5GGSjXZeTgX9sKYBtl8K6U",
194+
'tempo': 147.384,
185195
},
186196
{
187197
"album": "Queen II (Deluxe Remastered Version)",
@@ -195,6 +205,7 @@ def test_spotify_album_fetch_more():
195205
"year": "1974",
196206
"playlist_num": 3,
197207
"spotify_id": "0Ssh20fuVhmasLRJ97MLnp",
208+
'tempo': 152.769,
198209
},
199210
{
200211
"album": "Queen II (Deluxe Remastered Version)",
@@ -208,6 +219,7 @@ def test_spotify_album_fetch_more():
208219
"year": "1974",
209220
"playlist_num": 4,
210221
"spotify_id": "2LasW39KJDE4VH9hTVNpE2",
222+
'tempo': 115.471,
211223
},
212224
{
213225
"album": "Queen II (Deluxe Remastered Version)",
@@ -221,6 +233,7 @@ def test_spotify_album_fetch_more():
221233
"year": "1974",
222234
"playlist_num": 5,
223235
"spotify_id": "6jXrIu3hWbmJziw34IHIwM",
236+
'tempo': 145.124,
224237
},
225238
{
226239
"album": "Queen II (Deluxe Remastered Version)",
@@ -234,6 +247,7 @@ def test_spotify_album_fetch_more():
234247
"track_url": None,
235248
"playlist_num": 6,
236249
"spotify_id": "5dHmGuUeRgp5f93G69tox5",
250+
'tempo': 108.544,
237251
},
238252
{
239253
"album": "Queen II (Deluxe Remastered Version)",
@@ -247,6 +261,7 @@ def test_spotify_album_fetch_more():
247261
"year": "1974",
248262
"playlist_num": 7,
249263
"spotify_id": "2KPj0oB7cUuHQ3FuardOII",
264+
'tempo': 159.156,
250265
},
251266
{
252267
"album": "Queen II (Deluxe Remastered Version)",
@@ -260,6 +275,7 @@ def test_spotify_album_fetch_more():
260275
"year": "1974",
261276
"playlist_num": 8,
262277
"spotify_id": "34CcBjL9WqEAtnl2i6Hbxa",
278+
'tempo': 118.48,
263279
},
264280
{
265281
"album": "Queen II (Deluxe Remastered Version)",
@@ -273,6 +289,7 @@ def test_spotify_album_fetch_more():
273289
"year": "1974",
274290
"playlist_num": 9,
275291
"spotify_id": "1x9ak6LGIazLhfuaSIEkhG",
292+
'tempo': 112.623,
276293
},
277294
{
278295
"album": "Queen II (Deluxe Remastered Version)",
@@ -286,6 +303,7 @@ def test_spotify_album_fetch_more():
286303
"year": "1974",
287304
"playlist_num": 10,
288305
"spotify_id": "4CITL18Tos0PscW1amCK4j",
306+
'tempo': 145.497,
289307
},
290308
{
291309
"album": "Queen II (Deluxe Remastered Version)",
@@ -299,6 +317,7 @@ def test_spotify_album_fetch_more():
299317
"year": "1974",
300318
"playlist_num": 11,
301319
"spotify_id": "1e9Tt3nKBwRbuaU79kN3dn",
320+
'tempo': 126.343,
302321
},
303322
{
304323
"album": "Queen II (Deluxe Remastered Version)",
@@ -312,6 +331,7 @@ def test_spotify_album_fetch_more():
312331
"year": "1974",
313332
"playlist_num": 12,
314333
"spotify_id": "0uHqoDT7J2TYBsJx6m4Tvi",
334+
'tempo': 172.274,
315335
},
316336
{
317337
"album": "Queen II (Deluxe Remastered Version)",
@@ -325,6 +345,8 @@ def test_spotify_album_fetch_more():
325345
"year": "1974",
326346
"playlist_num": 13,
327347
"spotify_id": "3MIueGYoNiyBNfi5ukDgAK",
348+
'tempo': 146.712,
349+
328350
},
329351
{
330352
"album": "Queen II (Deluxe Remastered Version)",
@@ -338,6 +360,7 @@ def test_spotify_album_fetch_more():
338360
"year": "1974",
339361
"playlist_num": 14,
340362
"spotify_id": "34WAOFWdJ83a3YYrDAZTjm",
363+
'tempo': 128.873,
341364
},
342365
{
343366
"album": "Queen II (Deluxe Remastered Version)",
@@ -351,6 +374,7 @@ def test_spotify_album_fetch_more():
351374
"year": "1974",
352375
"playlist_num": 15,
353376
"spotify_id": "2AFIPUlApcUwGEgOSDwoBz",
377+
'tempo': 122.986,
354378
},
355379
{
356380
"album": "Queen II (Deluxe Remastered Version)",
@@ -364,6 +388,7 @@ def test_spotify_album_fetch_more():
364388
"year": "1974",
365389
"playlist_num": 16,
366390
"spotify_id": "4G4Sf18XkFvNTV5vAxiQyd",
391+
'tempo': 169.166,
367392
},
368393
] == songs
369394
assert (len(songs)) == 16
@@ -387,5 +412,6 @@ def test_spotify_playlist_fetch_local_file():
387412
"year": "",
388413
"playlist_num": 1,
389414
"spotify_id": None,
415+
"tempo": None,
390416
}
391-
] == songs
417+
] == songs

tests/test_youtube.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def test_download_one_false_skip():
2828
"cover": "https://i.scdn.co/image/ab67616d0000b27396d28597a5ae44ab66552183",
2929
"genre": "album rock",
3030
"spotify_id": "2GpBrAoCwt48fxjgjlzMd4",
31+
'tempo': 74.656,
3132
}
3233
],
3334
}
@@ -83,6 +84,7 @@ def test_download_one_true_skip():
8384
"cover": "https://i.scdn.co/image/ab67616d0000b27396d28597a5ae44ab66552183",
8485
"genre": "album rock",
8586
"spotify_id": "2GpBrAoCwt48fxjgjlzMd4",
87+
'tempo': 74.656,
8688
}
8789
],
8890
}
@@ -120,6 +122,7 @@ def test_download_cover_none():
120122
"cover": None,
121123
"genre": "classic rock",
122124
"spotify_id": "12LhScrlYazmU4vsqpRQNI",
125+
'tempo': 159.15,
123126
}
124127
],
125128
}

0 commit comments

Comments
 (0)