Skip to content

Commit

Permalink
Add tempo information to track metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
krishsharma0413 committed Dec 30, 2023
1 parent b56afb2 commit 80b92c7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions spotify_dl/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -154,6 +159,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 +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:
Expand All @@ -203,6 +211,7 @@ def fetch_tracks(sp, item_type, item_id):
"genre": genre,
"track_url": None,
"spotify_id": spotify_id,
"tempo": tempo,
}
)

Expand Down
6 changes: 5 additions & 1 deletion spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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", "")),
)

Expand Down

0 comments on commit 80b92c7

Please sign in to comment.