-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
234 lines (191 loc) · 7.59 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import pandas as pd
import requests
import psycopg2
# CREDENTIALS
CLIENT_ID = '4ae4c39d96574aff818d*****'
CLIENT_SECRET = '0ba53f9d5ce3489481de47*****'
AUTH_URL = 'https://accounts.spotify.com/api/token'
# -------------------------------------------------------------------------------
# POST
auth_response = requests.post(AUTH_URL, {
'grant_type': 'client_credentials',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
})
# convert the response to JSON
auth_response_data = auth_response.json()
# save the access token
access_token = auth_response_data['access_token']
# -------------------------------------------------------------------------------
# HEADER TOKEN
headers = {
'Authorization': 'Bearer {token}'.format(token=access_token)
}
# -------------------------------------------------------------------------------
# base URL of all Spotify API endpoints
BASE_URL = 'https://api.spotify.com/v1/'
# Track ID from the URI
artist_id_list = {'5sWHDYs0csV6RS48xBl0tH', '1Xyo4u8uXC1ZmMpatF05PJ', '36QJpDe2go2KgaRleHCDTp', '26VFTg2z8YR0cCuwLzESi2', '6qqNVTkY8uBg9cP3Jd7DAH'} #For artists
#artist_id_list = {'5sWHDYs0csV6RS48xBl0tH'}
# actual GET request with proper header
data_df_artist = []
data_df_tracks = []
origin = "Spotify API"
for artist_id in artist_id_list:
# Get response artist and convert to json
artist = requests.get(BASE_URL + 'artists/' + artist_id, headers=headers).json()
# Change the name of 'id' to 'artist_id'
artist.update({
'artist_id': artist['id'],
'origin_from': origin
})
#--------------------------------------------
# Get response albums from artist and convert to json
albums = requests.get(BASE_URL + 'artists/' + artist_id + '/albums', headers=headers).json()
print('Albums')
print(albums)
# Save names to verify if is reapted
albums_aux = []
for album in albums['items']:
album_name = album['name']
# here's a hacky way to skip over albums we've already grabbed
trim_name = album_name.split('(')[0].strip()
if trim_name.upper() in albums: # or int(album['release_date'][:4]) > 1983:
continue
albums_aux.append(trim_name.upper()) # use upper() to standardize
# this takes a few seconds so let's keep track of progress
print(album_name)
# pull all tracks from this album
tracks = requests.get(BASE_URL + 'albums/' + album['id'] + '/tracks', headers=headers).json()
tracks = tracks['items']
print(tracks)
for track in tracks:
# get audio features (key, liveness, danceability, ...)
f = requests.get(BASE_URL + 'audio-features/' + track['id'],
headers=headers)
f = f.json()
# combine with album info
f.update({
'track_id': f['id'],
'album': album_name,
'release_date': album['release_date'],
'track_number': track['track_number'],
'popularity': artist['popularity'],
'artist_name': artist['name'],
'genres': artist['genres'],
'name': track['name'],
'type': track['type'],
'origin_from': origin
})
data_df_tracks.append(f)
data_df_artist.append(artist)
print(data_df_artist)
print(data_df_tracks)
# -------------------------------------------------------------------------------
# DATA TO DB
# Artists
data_to_db_artist = pd.DataFrame(data_df_artist)
data_to_db_artist = data_to_db_artist[["artist_id", "name", "popularity", "type", "uri", "origin_from"]]
# Tracks
data_to_db_tracks = pd.DataFrame(data_df_tracks)
data_to_db_tracks = data_to_db_tracks[["track_id", "name", "artist_name", "album", "track_number", "popularity", "uri", "release_date", "genres", "type", "origin_from"]]
data_to_db_tracks['release_date'] = pd.to_datetime(data_to_db_tracks['release_date'])
print(data_to_db_artist)
def execute_sql(sql):
connection = {}
try:
# Connection parameters to data base
connection = psycopg2.connect(user="postgres",
password="*********",
host="127.0.0.1",
port="5000",
database="spotify_db")
print(connection)
# Creating cursor
cursor = connection.cursor()
# Print PostgreSQL Connection properties
print(connection.get_dsn_parameters(), "\n")
# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record, "\n\n")
print("Executing a command(s): \n"+sql)
cursor.execute(sql)
except (Exception, psycopg2.Error) as error:
print("Error while connecting to PostgreSQL", error)
finally:
# closing database connection.
if (connection):
connection.commit()
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
def create_tables():
commands = (
"""
CREATE TABLE artists (
artist_id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255),
popularity INTEGER,
uri VARCHAR(255),
type VARCHAR(255),
origin_from VARCHAR(255),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)
""",
""" CREATE TABLE tracks (
track_id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255),
artist_name VARCHAR(255),
album VARCHAR(255),
track_number INTEGER,
popularity INTEGER,
uri VARCHAR(255),
release_date DATE,
genres VARCHAR(255),
type VARCHAR(255),
origin_from VARCHAR(255),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)
"""
)
for command in commands:
execute_sql(command)
return commands
def insert_data(table_name, data):
connection = {}
try:
# Connection parameters to data base
connection = psycopg2.connect(user="postgres",
password="cristhi@n",
host="127.0.0.1",
port="5000",
database="spotify_db")
print(connection)
# Creating cursor
cursor = connection.cursor()
# Print PostgreSQL Connection properties
print(connection.get_dsn_parameters(), "\n")
# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record, "\n\n")
# creating column list for insertion
cols = ",".join([str(i) for i in data.columns.tolist()])
# Insert DataFrame recrds one by one.
for i, row in data.iterrows():
sql = "INSERT INTO {} (".format(table_name) + cols + ") VALUES (" + "%s," * (len(row) - 1) + "%s)"
cursor.execute(sql, tuple(row))
except (Exception, psycopg2.Error) as error:
print("Error while connecting to PostgreSQL", error)
finally:
# closing database connection.
if (connection):
connection.commit()
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
execute_sql("DROP TABLE artists, tracks")
create_tables()
insert_data("ARTISTS", data_to_db_artist)
insert_data("TRACKS", data_to_db_tracks)