-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_spotify_data.py
More file actions
249 lines (192 loc) · 7.5 KB
/
Copy pathextract_spotify_data.py
File metadata and controls
249 lines (192 loc) · 7.5 KB
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
"""
Extract Coldplay data from Spotify API using DLT
"""
import os
import base64
import requests
from datetime import datetime
from typing import Dict, List, Any, Iterator
import dlt
from dlt.sources.rest_api import rest_api_source
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Spotify API configuration
SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID")
SPOTIFY_CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET")
SPOTIFY_API_BASE_URL = "https://api.spotify.com/v1"
# Coldplay's Spotify artist ID
COLDPLAY_ARTIST_ID = "4gzpq5DPGxSnKTe4SA8HAU"
def get_spotify_access_token() -> str:
"""Get Spotify access token using Client Credentials flow"""
auth_url = "https://accounts.spotify.com/api/token"
# Encode client credentials
client_credentials = f"{SPOTIFY_CLIENT_ID}:{SPOTIFY_CLIENT_SECRET}"
client_credentials_b64 = base64.b64encode(client_credentials.encode()).decode()
headers = {
"Authorization": f"Basic {client_credentials_b64}",
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"grant_type": "client_credentials"
}
response = requests.post(auth_url, headers=headers, data=data)
response.raise_for_status()
return response.json()["access_token"]
@dlt.resource(name="artists", write_disposition="replace", primary_key="id")
def get_artist_info() -> Iterator[Dict[str, Any]]:
"""Fetch Coldplay artist information"""
token = get_spotify_access_token()
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(
f"{SPOTIFY_API_BASE_URL}/artists/{COLDPLAY_ARTIST_ID}",
headers=headers
)
response.raise_for_status()
artist_data = response.json()
artist_data["extracted_at"] = datetime.now().isoformat()
yield artist_data
@dlt.resource(name="albums", write_disposition="replace", primary_key="id")
def get_artist_albums() -> Iterator[Dict[str, Any]]:
"""Fetch all Coldplay albums"""
token = get_spotify_access_token()
headers = {"Authorization": f"Bearer {token}"}
offset = 0
limit = 50
while True:
response = requests.get(
f"{SPOTIFY_API_BASE_URL}/artists/{COLDPLAY_ARTIST_ID}/albums",
headers=headers,
params={
"include_groups": "album,single,compilation",
"limit": limit,
"offset": offset,
"market": "US"
}
)
response.raise_for_status()
data = response.json()
for album in data["items"]:
album["extracted_at"] = datetime.now().isoformat()
yield album
if not data.get("next"):
break
offset += limit
@dlt.resource(name="tracks", write_disposition="replace", primary_key="id")
def get_all_tracks() -> Iterator[Dict[str, Any]]:
"""Fetch all tracks from all Coldplay albums"""
token = get_spotify_access_token()
headers = {"Authorization": f"Bearer {token}"}
# First get all albums
albums = list(get_artist_albums())
for album in albums:
album_id = album["id"]
# Get tracks for each album
offset = 0
limit = 50
while True:
response = requests.get(
f"{SPOTIFY_API_BASE_URL}/albums/{album_id}/tracks",
headers=headers,
params={
"limit": limit,
"offset": offset,
"market": "US"
}
)
response.raise_for_status()
data = response.json()
for track in data["items"]:
track["album_id"] = album_id
track["album_name"] = album["name"]
track["album_release_date"] = album["release_date"]
track["extracted_at"] = datetime.now().isoformat()
yield track
if not data.get("next"):
break
offset += limit
@dlt.resource(name="audio_features", write_disposition="replace", primary_key="id")
def get_audio_features() -> Iterator[Dict[str, Any]]:
"""Fetch audio features for all tracks"""
token = get_spotify_access_token()
headers = {"Authorization": f"Bearer {token}"}
# Get all track IDs
tracks = list(get_all_tracks())
track_ids = [track["id"] for track in tracks]
# Spotify allows up to 100 tracks per request
batch_size = 100
for i in range(0, len(track_ids), batch_size):
batch_ids = track_ids[i:i + batch_size]
response = requests.get(
f"{SPOTIFY_API_BASE_URL}/audio-features",
headers=headers,
params={"ids": ",".join(batch_ids)}
)
response.raise_for_status()
data = response.json()
for features in data["audio_features"]:
if features: # Some tracks might not have audio features
features["extracted_at"] = datetime.now().isoformat()
yield features
@dlt.resource(name="top_tracks", write_disposition="replace", primary_key="id")
def get_top_tracks() -> Iterator[Dict[str, Any]]:
"""Fetch Coldplay's top tracks"""
token = get_spotify_access_token()
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(
f"{SPOTIFY_API_BASE_URL}/artists/{COLDPLAY_ARTIST_ID}/top-tracks",
headers=headers,
params={"market": "US"}
)
response.raise_for_status()
data = response.json()
for idx, track in enumerate(data["tracks"]):
track["rank"] = idx + 1
track["extracted_at"] = datetime.now().isoformat()
yield track
@dlt.resource(name="related_artists", write_disposition="replace", primary_key="id")
def get_related_artists() -> Iterator[Dict[str, Any]]:
"""Fetch artists related to Coldplay"""
token = get_spotify_access_token()
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(
f"{SPOTIFY_API_BASE_URL}/artists/{COLDPLAY_ARTIST_ID}/related-artists",
headers=headers
)
response.raise_for_status()
data = response.json()
for artist in data["artists"]:
artist["related_to_artist_id"] = COLDPLAY_ARTIST_ID
artist["extracted_at"] = datetime.now().isoformat()
yield artist
def run_pipeline():
"""Run the DLT pipeline to extract Coldplay data"""
# Create pipeline
pipeline = dlt.pipeline(
pipeline_name="spotify_coldplay",
destination="duckdb",
dataset_name="coldplay_data",
dev_mode=True # Use dev_mode instead of full_refresh
)
# Run the pipeline with available resources
# Note: audio_features and related_artists are restricted for new apps as of Nov 2024
info = pipeline.run([
get_artist_info(),
get_artist_albums(),
get_all_tracks(),
get_top_tracks()
])
print(info)
print(f"\nPipeline completed successfully!")
print(f"Data loaded to: {pipeline.pipeline_name}.duckdb")
# Print some statistics
with pipeline.sql_client() as client:
# Count records in each table
tables = ["artists", "albums", "tracks", "top_tracks"]
print("\nData Statistics:")
for table in tables:
result = client.execute_sql(f"SELECT COUNT(*) as count FROM {table}")
count = list(result)[0][0]
print(f" {table}: {count} records")
if __name__ == "__main__":
run_pipeline()