-
Notifications
You must be signed in to change notification settings - Fork 0
/
track_info_generator.py
91 lines (71 loc) · 2.71 KB
/
track_info_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# import spotipy
# from spotipy.oauth2 import SpotifyOAuth
# import configparser
from pprint import pprint
import time
def structure_song(track_details, audio_details):
song_data = {
"id": track_details["id"],
"name": track_details["name"],
"artists": [artist["name"] for artist in track_details["artists"]],
"id_artists": [artist["id"] for artist in track_details["artists"]],
"explicit": track_details["explicit"],
"popularity": track_details["popularity"],
"release_date": track_details["album"]["release_date"],
}
for feature, value in audio_details.items():
if feature in [
"popularity",
"explicit",
"danceability",
"energy",
"key",
"loudness",
"mode",
"speechiness",
"acousticness",
"instrumentalness",
"liveness",
"valence",
"tempo",
"duration_ms",
"time_signature",
]:
song_data[feature] = value
return song_data
# config = configparser.ConfigParser()
# config.read('./configs/config.cfg')
# SPOTIPY_CLIENT_ID = config.get('SPOTIFY', 'SPOTIPY_CLIENT_ID')
# SPOTIPY_CLIENT_SECRET = config.get('SPOTIFY', 'SPOTIPY_CLIENT_SECRET')
# SPOTIPY_REDIRECT_URI = config.get('SPOTIFY', 'SPOTIPY_REDIRECT_URI')
# sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
# SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URI, scope='user-library-read'))
def fetch_track_info(sp, id):
track_uri = "spotify:track:" + id
track_details = sp.track(track_uri)
audio_details = sp.audio_features(track_uri)[0]
song = structure_song(track_details, audio_details)
return song
def apply_cover_images(sp, df):
for index, row in df.iterrows():
track_details = sp.track(row["id"])
cover_image_url = track_details["album"]["images"][0]["url"]
df.at[index, "album_cover"] = cover_image_url
return df
def apply_preview_url(sp, df):
for index, row in df.iterrows():
track_details = sp.track(row["id"])
try:
preview_url = track_details["preview_url"]
except:
preview_url = "https://github.com/anars/blank-audio/blob/master/45-seconds-of-silence.mp3"
df.at[index, "preview_url"] = preview_url
return df
def convert_msTo_min(milliseconds):
total_seconds = milliseconds / 1000
minutes = int(total_seconds // 60)
seconds = int(total_seconds % 60)
return f"{minutes} minutes {seconds} seconds"
def generate_additional_info(sp, track_id):
track_details = sp.track(track_id)
return track_details["album"]["images"][0]["url"], track_details["preview_url"]