-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetMetadata.py
370 lines (310 loc) · 12.8 KB
/
getMetadata.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from time import sleep
from urllib.parse import urlparse, parse_qs
import requests
import json
from random import shuffle
def get_proxy_list():
base_url = "https://raw.githubusercontent.com/afkarxyz/proxies/main/"
proxy_types = ["http", "https", "socks4", "socks5"]
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
}
all_proxies = []
for proxy_type in proxy_types:
try:
response = requests.get(f"{base_url}{proxy_type}", headers=headers)
if response.status_code == 200:
proxies = response.text.splitlines()
formatted_proxies = [(proxy, proxy_type) for proxy in proxies]
all_proxies.extend(formatted_proxies)
except:
continue
if all_proxies:
shuffle(all_proxies)
return all_proxies
return None
token_url = 'https://open.spotify.com/get_access_token?reason=transport&productType=web_player'
playlist_base_url = 'https://api.spotify.com/v1/playlists/{}'
album_base_url = 'https://api.spotify.com/v1/albums/{}'
track_base_url = 'https://api.spotify.com/v1/tracks/{}'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
'Accept': 'application/json',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'Referer': 'https://open.spotify.com/',
'Origin': 'https://open.spotify.com'
}
class SpotifyInvalidUrlException(Exception):
pass
class SpotifyWebsiteParserException(Exception):
pass
def parse_uri(uri):
u = urlparse(uri)
if u.netloc == "embed.spotify.com":
if not u.query:
raise SpotifyInvalidUrlException("ERROR: url {} is not supported".format(uri))
qs = parse_qs(u.query)
return parse_uri(qs['uri'][0])
if not u.scheme and not u.netloc:
return {"type": "playlist", "id": u.path}
if u.scheme == "spotify":
parts = uri.split(":")
else:
if u.netloc != "open.spotify.com" and u.netloc != "play.spotify.com":
raise SpotifyInvalidUrlException("ERROR: url {} is not supported".format(uri))
parts = u.path.split("/")
if parts[1] == "embed":
parts = parts[1:]
l = len(parts)
if l == 3 and parts[1] in ["album", "track", "playlist"]:
return {"type": parts[1], "id": parts[2]}
if l == 5 and parts[3] == "playlist":
return {"type": parts[3], "id": parts[4]}
raise SpotifyInvalidUrlException("ERROR: unable to determine Spotify URL type or type is unsupported.")
def get_json_from_api(api_url, access_token, proxy, proxy_type):
headers.update({'Authorization': 'Bearer {}'.format(access_token)})
req = requests.get(
api_url,
headers=headers,
proxies={proxy_type: proxy},
timeout=10
)
if req.status_code == 429:
seconds = int(req.headers.get("Retry-After")) + 1
print(f"INFO: rate limited! Sleeping for {seconds} seconds")
sleep(seconds)
return None
if req.status_code != 200:
raise SpotifyWebsiteParserException(f"ERROR: {api_url} gave us not a 200. Instead: {req.status_code}")
return req.json()
def get_raw_spotify_data(spotify_url):
url_info = parse_uri(spotify_url)
proxies = get_proxy_list()
if not proxies:
return {"error": "Failed to get proxy list"}
token = None
for proxy, proxy_type in proxies:
try:
req = requests.get(
token_url,
headers=headers,
proxies={proxy_type: proxy},
timeout=10
)
if req.status_code == 200:
token = req.json()
break
except:
continue
if not token:
return {"error": "Failed to get access token with available proxies"}
raw_data = {}
if url_info['type'] == "playlist":
playlist_data = None
for proxy, proxy_type in proxies:
try:
playlist_data = get_json_from_api(
playlist_base_url.format(url_info["id"]),
token["accessToken"],
proxy,
proxy_type
)
if playlist_data:
break
except:
continue
if not playlist_data:
return {"error": "Failed to get playlist data with available proxies"}
raw_data = playlist_data
tracks = []
tracks_url = f'https://api.spotify.com/v1/playlists/{url_info["id"]}/tracks?limit=100'
while tracks_url:
track_data = None
for proxy, proxy_type in proxies:
try:
track_data = get_json_from_api(
tracks_url,
token["accessToken"],
proxy,
proxy_type
)
if track_data:
break
except:
continue
if not track_data:
break
tracks.extend(track_data['items'])
tracks_url = track_data.get('next')
raw_data['tracks']['items'] = tracks
elif url_info["type"] == "album":
album_data = None
for proxy, proxy_type in proxies:
try:
album_data = get_json_from_api(
album_base_url.format(url_info["id"]),
token["accessToken"],
proxy,
proxy_type
)
if album_data:
album_data['_token'] = token["accessToken"]
break
except:
continue
if not album_data:
return {"error": "Failed to get album data with available proxies"}
raw_data = album_data
tracks = []
tracks_url = f'{album_base_url.format(url_info["id"])}/tracks?limit=50'
while tracks_url:
track_data = None
for proxy, proxy_type in proxies:
try:
track_data = get_json_from_api(
tracks_url,
token["accessToken"],
proxy,
proxy_type
)
if track_data:
break
except:
continue
if not track_data:
break
tracks.extend(track_data['items'])
tracks_url = track_data.get('next')
raw_data['tracks']['items'] = tracks
elif url_info["type"] == "track":
track_data = None
for proxy, proxy_type in proxies:
try:
track_data = get_json_from_api(
track_base_url.format(url_info["id"]),
token["accessToken"],
proxy,
proxy_type
)
if track_data:
break
except:
continue
if not track_data:
return {"error": "Failed to get track data with available proxies"}
raw_data = track_data
return raw_data
def format_track_data(track_data):
artists = []
for artist in track_data['artists']:
artists.append(artist['name'])
image_url = track_data.get('album', {}).get('images', [{}])[0].get('url', '')
return {
"track": {
"artists": ", ".join(artists),
"name": track_data.get('name', ''),
"album_name": track_data.get('album', {}).get('name', ''),
"duration_ms": track_data.get('duration_ms', 0),
"images": image_url,
"release_date": track_data.get('album', {}).get('release_date', ''),
"track_number": track_data.get('track_number', 0),
"external_urls": track_data.get('external_urls', {}).get('spotify', '')
}
}
def format_album_data(album_data):
artists = []
for artist in album_data['artists']:
artists.append(artist['name'])
image_url = album_data.get('images', [{}])[0].get('url', '')
track_list = []
for track in album_data.get('tracks', {}).get('items', []):
track_artists = []
for artist in track.get('artists', []):
track_artists.append(artist['name'])
track_list.append({
"artists": ", ".join(track_artists),
"name": track.get('name', ''),
"album_name": album_data.get('name', ''),
"duration_ms": track.get('duration_ms', 0),
"images": image_url,
"release_date": album_data.get('release_date', ''),
"track_number": track.get('track_number', 0),
"external_urls": track.get('external_urls', {}).get('spotify', '')
})
return {
"album_info": {
"total_tracks": album_data.get('total_tracks', 0),
"name": album_data.get('name', ''),
"release_date": album_data.get('release_date', ''),
"artists": ", ".join(artists),
"images": image_url
},
"track_list": track_list
}
def format_playlist_data(playlist_data):
image_url = playlist_data.get('images', [{}])[0].get('url', '')
track_list = []
for item in playlist_data.get('tracks', {}).get('items', []):
track = item.get('track', {})
artists = []
for artist in track.get('artists', []):
artists.append(artist['name'])
track_image = track.get('album', {}).get('images', [{}])[0].get('url', '')
track_list.append({
"artists": ", ".join(artists),
"name": track.get('name', ''),
"album_name": track.get('album', {}).get('name', ''),
"duration_ms": track.get('duration_ms', 0),
"images": track_image,
"release_date": track.get('album', {}).get('release_date', ''),
"track_number": track.get('track_number', 0),
"external_urls": track.get('external_urls', {}).get('spotify', '')
})
return {
"playlist_info": {
"tracks": {"total": playlist_data.get('tracks', {}).get('total', 0)},
"followers": {"total": playlist_data.get('followers', {}).get('total', 0)},
"owner": {
"display_name": playlist_data.get('owner', {}).get('display_name', ''),
"name": playlist_data.get('name', ''),
"images": image_url
}
},
"track_list": track_list
}
def process_spotify_data(raw_data, data_type):
if not raw_data or "error" in raw_data:
return {"error": "Invalid data provided"}
try:
if data_type == "track":
return format_track_data(raw_data)
elif data_type == "album":
return format_album_data(raw_data)
elif data_type == "playlist":
return format_playlist_data(raw_data)
else:
return {"error": "Invalid data type"}
except Exception as e:
return {"error": f"Error processing data: {str(e)}"}
def get_filtered_data(spotify_url):
raw_data = get_raw_spotify_data(spotify_url)
if raw_data and "error" not in raw_data:
url_info = parse_uri(spotify_url)
filtered_data = process_spotify_data(raw_data, url_info['type'])
return filtered_data
return {"error": "Failed to get raw data"}
if __name__ == '__main__':
playlist = "https://open.spotify.com/playlist/37i9dQZEVXbNG2KDcFcKOF"
album = "https://open.spotify.com/album/7kFyd5oyJdVX2pIi6P4iHE"
song = "https://open.spotify.com/track/4wJ5Qq0jBN4ajy7ouZIV1c"
filtered_playlist = get_filtered_data(playlist)
print(json.dumps(filtered_playlist, indent=2))
filtered_album = get_filtered_data(album)
print(json.dumps(filtered_album, indent=2))
filtered_track = get_filtered_data(song)
print(json.dumps(filtered_track, indent=2))