-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_db.py
299 lines (237 loc) · 10 KB
/
scrape_db.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import json
import zlib
from db import DataBase
import math
import datetime
import requests
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
SPOTIFY_API_URL = "https://api.spotify.com"
# All of the API endpoints that we are interested in
API_ENDPOINTS = {
"user_profile" : "/v1/me",
"user_albums" : "/v1/me/albums",
"user_tracks" : "/v1/me/tracks",
"user_playlists" : "/v1/me/playlists",
"audio_features" : "/v1/audio-features",
"audio_analysis" : "/v1/audio-analysis",
"tracks" : "/v1/tracks",
"artists" : "/v1/artists",
}
# Replace endpoints with full access URL
for key, value in API_ENDPOINTS.items():
API_ENDPOINTS[key] = SPOTIFY_API_URL + value
def get(endpoint, token, max_retries=10, delay=4):
authorization_header = {"Authorization": "Bearer {}".format(token)}
try:
logger.info("GET: " + endpoint)
response = requests.get(endpoint, headers=authorization_header)
except requests.exceptions.ConnectionError as e:
response = {}
response.status_code = 429
logger.warn("Could not fulfill GET.\n" + e)
# If status is anything but OK, raise an error
if response.status_code != 200:
if response.status_code == 429:
if max_retries > 0:
time.sleep(delay)
return get(endpoint, token, max_retries=max_retries - 1, delay=delay + 1)
else:
msg = "Connection refused after {} retries".format(max_retries)
logger.error(msg)
raise RuntimeError(msg)
logger.error(response.reason)
raise RuntimeError(response.reason)
return response
def scrape_profile(token):
key = "user_profile"
endpoint = API_ENDPOINTS[key]
response = get(endpoint, token)
profile = response.json()
return profile
def generate_paginated_urls(endpoint, total, limit):
urls = []
for i in range(int(total / limit)):
offset = (i + 1) * limit
urls.append(endpoint + f"?offset={offset}&limit={limit}")
return urls
def scrape_paginated_data(endpoint, token, limit=None):
if limit:
url = endpoint + f"?limit={limit}"
else:
url = endpoint
response = get(url, token)
page = response.json()
all_pages = [page]
total = page['total']
if not limit:
limit = page['limit']
urls = generate_paginated_urls(endpoint, total, limit)
# try to perform all of these asynchronously
for i, url in enumerate(urls):
response = get(url, token)
page = response.json()
all_pages.append(page)
return all_pages
def scrape_user_playlists(token):
key = "user_playlists"
endpoint = API_ENDPOINTS[key]
return scrape_paginated_data(endpoint, token)
def scrape_user_albums(token):
key = "user_albums"
endpoint = API_ENDPOINTS[key]
return scrape_paginated_data(endpoint, token)
# Construct a dictionary from a track JSON object that contains
# a subset of the data that we choose
def parse_track(track):
keys_to_keep = ["name", "id", "popularity", "uri", "artists"]
ret_track = {}
ret_track["date"] = track["added_at"]
for key in keys_to_keep:
ret_track[key] = track["track"][key]
artist_keys = ["name", "id"]
ret_track["artists"] = [{ key : artist[key] for key in artist_keys } for artist in track["track"]["artists"]]
return ret_track
def scrape_user_tracks(token):
key = "user_tracks"
endpoint = API_ENDPOINTS[key]
pages = scrape_paginated_data(endpoint, token, limit=50)
tracks = []
for page in pages:
page_tracks = page['items']
for track in page_tracks:
tracks.append(parse_track(track))
return tracks
def parse_features(features):
features_to_keep = ['energy', 'liveness', 'speechiness', 'acousticness', 'instrumentalness',
'danceability', 'loudness', 'valence', 'tempo']
ret_features = {}
for feature in features_to_keep:
ret_features[feature] = features[feature]
return ret_features
# Gets audio features for every track id given
# don't technically need the user's token to scrape these since they are publically available
def scrape_audio_features(track_ids, token, limit=100):
key = "audio_features"
endpoint = API_ENDPOINTS[key]
total = len(track_ids)
urls = []
for i in range(int(total / limit) + 1):
low = i * limit
high = (i + 1) * limit
urls.append(endpoint + "?ids=" + ",".join(track_ids[low:high]))
all_features = []
for url in urls:
response = get(url, token)
track_features = response.json()['audio_features']
for features in track_features:
all_features.append(parse_features(features))
return all_features
def scrape_genres(artist_ids, token, limit=50):
key = "artists"
endpoint = API_ENDPOINTS[key]
total = len(artist_ids)
urls = []
for i in range(int(total / limit) + 1):
low = i * limit
high = (i + 1) * limit
urls.append(endpoint + "?ids=" + ",".join(artist_ids[low:high]))
all_genres = []
for url in urls:
response = get(url, token)
artists = response.json()['artists']
for artist in artists:
all_genres.append(artist['genres'])
return all_genres
def compile_library(tracks, audio_features, genres):
# compile into data structure we want
genre_idx = 0
for track, features in zip(tracks, audio_features):
for feature in features.keys():
track[feature] = features[feature]
track_genres = []
for artist in track['artists']:
track_genres.append(genres[genre_idx])
genre_idx += 1
track["genres"] = list(set(sum(track_genres, [])))
def compress_data(data):
return zlib.compress(json.dumps(data).encode('utf-8'))
def partition_and_compress_list(data_list):
# max size of a parition in KB
MAX_PARTITION_SIZE = 350
compressed = compress_data(data_list)
data_size = compressed.__sizeof__()
data_size_kb = data_size / 1024
# DynamoDB has an item limit of 400 KB
# if we go beyond this, need to partition the data up into
# pieces and store those pieces separately
# Use 350 KB to keep a 50 KB overhead for database key sizes
# and changes in compression efficiency over different partitions
num_partitions = math.ceil(data_size_kb / MAX_PARTITION_SIZE)
total_items = len(data_list)
tracks_per_partition = math.ceil(total_items / num_partitions)
# need to split the data into multiple partitions and store them separately
partitions = []
for i in range(num_partitions):
lower = i * tracks_per_partition
upper = (i + 1) * tracks_per_partition
partition = data_list[lower:upper]
partition_compressed = compress_data(partition)
partitions.append(partition_compressed)
return partitions
def get_current_time():
return int((datetime.datetime.now() - datetime.datetime(1970,1,1)).total_seconds() * 1e6)
# need try / catch to check if token has expired and ask for a new one
def scrape_library(token):
db = DataBase("Wayfinder")
profile = scrape_profile(token)
user_id = profile['id']
logger.info(f"writing {user_id} --> 'profile' : {profile} ({profile.__sizeof__()/1024:.1f} KB) ")
db.update(user_id, "profile", profile)
scrape_time = get_current_time()
logger.info(f"writing {user_id} --> 'scrape_time' : {scrape_time} ({scrape_time.__sizeof__()/1024:.1f} KB) ")
db.update(user_id, "scrape_time", scrape_time)
message = "Scraping library"
logger.info(f"writing {user_id} --> 'message' : {message} ({message.__sizeof__()/1024:.1f} KB) ")
db.update(user_id, "message", message)
tracks = scrape_user_tracks(token)
track_ids = [track["id"] for track in tracks]
message = "Scraping song features"
logger.info(f"writing {user_id} --> 'message' : {message} ({message.__sizeof__()/1024:.1f} KB) ")
db.update(user_id, "message", message)
audio_features = scrape_audio_features(track_ids, token)
artist_ids = sum([ [ artist["id"] for artist in track["artists"] ] for track in tracks ], [])
message = "Scraping artist genres"
logger.info(f"writing {user_id} --> 'message' : {message} ({message.__sizeof__()/1024:.1f} KB) ")
db.update(user_id, "message", message)
genres = scrape_genres(artist_ids, token)
compile_library(tracks, audio_features, genres)
tracks_partitioned = partition_and_compress_list(tracks)
message = "Writing to data base"
logger.info(f"writing {user_id} --> 'message' : {message} ({message.__sizeof__()/1024:.1f} KB) ")
db.update(user_id, "message", message)
partition_keys = []
for i, partition in enumerate(tracks_partitioned):
partition_key = f"{user_id}_tracks_{i}"
partition_keys.append(partition_key)
logger.info(f"writing {partition_key} --> 'tracks' : Bytes(partition) ({partition.__sizeof__()/1024:.1f} KB) ")
db.update(partition_key, "tracks", partition)
logger.info(f"writing {user_id} --> 'partition_keys' : {partition_keys} ({partition_keys.__sizeof__()/1024:.1f} KB) ")
db.update(user_id, "partition_keys", partition_keys)
message = "Done"
logger.info(f"writing {user_id} --> 'message' : {message} ({message.__sizeof__()/1024:.1f} KB) ")
db.update(user_id, "message", message)
def lambda_handler(event, context):
assert("token" in event.keys())
token = event["token"]
try:
scrape_library(token)
return { "statusCode" : 200 }
except Exception as e:
raise Exception("Error scraping library:", e)
def main():
token = "BQCJvZ6F58Jaqf5VkY1nnzL5MN20TF7a3ffVhc4oLPOSpbn2xcu3jO_HWwAqG8I6w-xMxDP0CVitcRrSrvPkUDV5tgm-u6GlngdZ6TRJXga5mR548YKmFKexxjYtvZGCg3bQjXfL4O3kkmOYDPzeGfUuEjjJZJIavR4v7IjIIT_ethXLyE8hs7aCyw6R57P93QUFGvkSXkaAyvaN22piFTM2DG_J5yOQOhRYTR-YrceY"
scrape_library(token)
if __name__ == "__main__":
main()