-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
248 lines (224 loc) · 7.68 KB
/
scraper.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
import sys
import spotipy
import spotipy.util as util
import _pickle as pickle
import os
from collections import defaultdict
from collections import Counter
import graph_builder
import graph_analyzer
from track import Track
'''Originally forked from https://github.com/plamere/playlistminer for importing spotify playlist data with spotipy'''
#Set number of playlists to query for
max_playlists = 10000
data = {}
playlist_names = {}
#Link a track ID to a track object
track_data = {}
album_genres = {}
#Test if a playlist has more than one artist and album to avoid useless ones
def is_good_playlist(items):
artists = set()
albums = set()
for item in items:
track = item['track']
if track:
artists.add(track['artists'][0]['id'])
albums.add(track['album']['id'])
return len(artists) > 1 and len(albums) > 1
'''
Removed genre details
Made queries longer, lack of spotify genre data, difficult comparisons with genre lists
def get_album_genre(album_id):
global album_genres
if album_id in album_genres.keys():
return album_genres[album_id]
album = sp.album(album_id)
genre_list = album['genres']
if len(genre_list) < 1:
album_genres[album_id] = "N/A"
return "N/A"
else:
genre_string = ""
for genre in genre_list:
genre_string += ","
genre_string += genre
album_genres[album_id] = genre_string
return genre_string
'''
# Process data from a playlist id into data structures for building the graph
def process_playlist(playlist):
global data
global track_data
global playlist_names
pid = playlist['id']
name = playlist['name']
playlist_names[pid] = name
uid = playlist['owner']['id']
#Query the playlist id from spotify
try:
results = sp.user_playlist_tracks(uid, playlist['id'])
# fields="items.track(!album)")
# Check if we got a good playlist
if results and 'items' in results and is_good_playlist(results['items']):
data[pid] = {}
# Look at every track in the playlist
for item in results['items']:
track = item['track']
# Check if the track has the data it should
if track and (track['name'] is not None) and (track['artists'] is not None):
# Create a track item with the title and artist
tid = track['id']
title = track['name']
album_id = track['album']['id']
artist = track['artists'][0]['name']
track_data[tid] = Track(title, artist)
# Add the playlist and associated tracks to the data dict
data[pid][tid] = Track(title, artist)
else:
print('mono playlist skipped')
except spotipy.SpotifyException:
print('trouble, skipping')
except ConnectionError:
print('Connection error, skipping')
except:
print("Skipping due to other error")
# Save the global data
def save():
playlists_out = open('playlists.pkl', 'wb')
tracks_out = open('tracks.pkl', 'wb')
pickle.dump(data, playlists_out, -1)
pickle.dump(track_data, tracks_out, -1)
playlists_out.close()
tracks_out.close()
# Save the processed graph data
def save_graph(G):
graph_out = open('graph_bin.pkl', 'wb')
pickle.dump(G, graph_out, -1)
graph_out.close()
# Load the graph if one already exists
def load_graph():
if os.path.exists('graph_bin.pkl'):
graph_in = open('graph_bin.pkl', 'rb')
graph = pickle.load(graph_in)
graph_in.close()
return graph
else:
return None
# Load data files if they already exist
def load_data():
global data
global track_data
global playlist_names
if os.path.exists('tracks.pkl') and os.path.exists('playlists.pkl'):
playlist_file = open('playlists.pkl', 'rb')
track_file = open('tracks.pkl', 'rb')
data = pickle.load(playlist_file)
track_data = pickle.load(track_file)
else:
data = {}
track_data = {}
playlist_names = {}
return data
# Query for playlists and process their data
def crawl_playlists():
# Search for playlists for the word "the"
# There is probably a cleaner way to get random playlists
# Could be used to limit graph size
queries = ['the']
# Max number we can get from the spotify api per query
limit = 50
# The number of times we need to query
num_queries = max_playlists/limit
count = 0
for query in queries:
which = 0
results = sp.search(query, limit=limit, type='playlist')
playlist = results['playlists']
while playlist and count < num_queries:
count += 1
# For each playlist we get
for item in playlist['items']:
# Turn it into usable data
print("Processing playlist: ", which)
process_playlist(item)
which += 1
# If the query returned more, use those
if playlist['next']:
try:
results = sp.next(playlist)
playlist = results['playlists']
except spotipy.client.SpotifyException:
playlist = None
else:
playlist = None
# Print all the playlist data
def print_playlists():
for key in data:
print("Playlist: ", key, "\t", playlist_names[key])
for track in data[key]:
song = data[key][track].title
artist = data[key][track].artist
print("\t Track: ", track, "\t", song, "\t", artist)
# Build an edge list off the stored data
def build_edge_list():
edges = defaultdict(list)
count = 1
# Look at a playlist and add an edge for every track to every other.
for tracklist in data.values():
print("Building edges for playlist number: ", count)
for track in tracklist:
for other_track in tracklist:
# No self-loops
if track != other_track:
edges[track].append(other_track)
count += 1
return edges
# Turn repeated connections into weights
def combine_edges(edges):
new_edges = defaultdict(list)
for edge in edges.keys():
track_1 = edge
counts = Counter(edges[edge])
for next_track in counts.keys():
info = (next_track, counts[next_track])
new_edges[track_1].append(info)
return new_edges
# Create graph given a username with spotify access
def create_graph(username):
# Set up the global spotipy and data objects
global sp
global data
scope = 'user-library-read'
token = util.prompt_for_user_token(username, scope)
# If we can get spotify access
if token:
sp = spotipy.Spotify(auth=token)
# See if we already downloaded the data
data = load_data()
if len(data) == 0:
crawl_playlists()
save()
# Format the data to build the graph
edges = build_edge_list()
edges = combine_edges(edges)
# Get all the graph info into the right format
G = graph_builder.build_graph(edges, track_data)
save_graph(G)
else:
print("Can't get token for", username)
sys.exit()
return G
if __name__ == '__main__':
if len(sys.argv) > 1:
# Need to be given a spotify username
username = sys.argv[1]
else:
print("Usage: %s username" % (sys.argv[0],))
sys.exit()
# Create a graph if it doesn't exist
graph = load_graph()
if graph is None:
graph = create_graph(username)
# Analyze the graph
graph_analyzer.analyze(graph)