-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.py
265 lines (233 loc) · 8.6 KB
/
pipeline.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
import os
import json
import psycopg2
import psycopg2.extras
import concurrent.futures
import itertools
import time
import src.spotify_client as spotify_client
from beatportscraper.spiders.beatportspider import BeatportspiderSpider
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
def beatport_pipeline():
"""
Summary: Pipeline of the scrapy spider using CrawlerProcess.
It imports the beatportscraper spider and its settings.
"""
settings = get_project_settings()
process = CrawlerProcess(settings)
process.crawl(BeatportspiderSpider)
process.start()
def spotify_pipeline(
conn: psycopg2.extensions.connection,
cur: psycopg2.extras.RealDictCursor,
app: spotify_client.SpotifyClient,
) -> None:
"""
Summary: Pipeline of the spotify API. It retrieves records of track title
and track artist from the Postgres database and retrieves their audio features.
These are subsequently stored in the same database. If the track is not found,
or no audio features are found, then the rows stored in the table will contain
null values.
Args:
conn (psycopg2.extensions.connection): A psycopg2 connection class.
cur (psycopg2.extensions.RealDictCursor): A pscyopg2 dictionary-like cursor.
The attributes of the retrieved records from queries can be accessed
similar to Python dictionaries.
app (spotify_client.SpotifyClient): A spotify_client SpotifyCLient class.
"""
cur.execute(
"""
CREATE TABLE IF NOT EXISTS features(
track_title VARCHAR(128) NOT NULL,
track_artist VARCHAR(128) NOT NULL,
track_year DATE NOT NULL,
acousticness NUMERIC,
danceability NUMERIC,
energy NUMERIC,
instrumentalness NUMERIC,
liveness NUMERIC,
loudness NUMERIC,
speechiness NUMERIC,
tempo NUMERIC,
time_signature INT,
valence NUMERIC,
UNIQUE(track_title, track_artist),
PRIMARY KEY(track_title, track_artist, track_year)
);
"""
)
get_query = """
SELECT DISTINCT
t.track_title as title,
t.track_artist as artist,
t.track_date AS year
FROM tracks t
WHERE NOT EXISTS (
SELECT
f.track_artist,
f.track_title
FROM features f
WHERE t.track_artist = f.track_artist
AND t.track_title = f.track_title
AND t.track_date = f.track_year)
LIMIT 1000;
"""
# results are limited to 1000 because of the possibility of soft ban due to large number of requests
cur.execute(get_query)
tracks = cur.fetchall()
iter_app = itertools.repeat(app)
search_requests = []
# Search endpoint is somewhat more lenient to large number of requests than track features
with concurrent.futures.ThreadPoolExecutor() as executor:
results = executor.map(search_retrieve, tracks, iter_app)
for result in results:
search_requests.append(result)
chunksize = 100
id_chunks = [
search_requests[i : i + chunksize]
for i in range(0, len(search_requests), chunksize)
]
for chunk in id_chunks:
no_id_list = []
for i, track in enumerate(chunk):
if track[3] is None:
no_id_list.append(chunk[i])
chunk = [track for track in chunk if track not in no_id_list]
# Insert tracks that were not found
for no_id in no_id_list:
no_id_features = {
"postgres_title": no_id[0],
"postgres_artist": no_id[1],
"postgres_year": no_id[2],
}
insert_data(no_id_features, conn, cur)
# Get track features of tracks that were found then insert them
unzipped = list(zip(*chunk))
postgres_titles, postgres_artists, postgres_years, track_id_list = (
unzipped[0],
unzipped[1],
unzipped[2],
unzipped[3],
)
for title, artist, year, feature in zip(
postgres_titles,
postgres_artists,
postgres_years,
app.get_track_features(track_id_list),
):
feature["postgres_title"] = title
feature["postgres_artist"] = artist
feature["postgres_year"] = year
insert_data(feature, conn, cur)
def search_retrieve(
track_details: dict,
app: spotify_client.SpotifyClient,
) -> tuple:
"""
Summary: Searches tracks through the SpotifyClient.search_track function.
Args:
track_details (dict): a dictionary containing the values of
"title", "year", and "artist".
app (spotify_client.SpotifyClient): A spotify_client SpotifyCLient class.
Returns:
tuple: A tuple containing the track title, track artist, track year,
and Spotify track ID
"""
sptfy_track_id = app.search_track(market="PH", song_details=track_details)
return (
track_details["title"],
track_details["artist"],
track_details["year"],
sptfy_track_id,
)
def insert_data(
track_features: dict,
conn: psycopg2.extensions.connection,
cur: psycopg2.extras.RealDictCursor,
) -> None:
"""
Summary: Inserts the audio features into the Postgres database
Args:
track_features (dict): a dictionary containing the audio features
conn (psycopg2.extensions.connection): A psycopg2 connection class.
cur (psycopg2.extensions.RealDictCursor): A pscyopg2 dictionary-like cursor.
The attributes of the retrieved records from queries can be accessed
similar to Python dictionaries.
"""
try:
cur.execute(
"""
INSERT INTO features (
track_title,
track_artist,
track_year,
acousticness,
danceability,
energy,
instrumentalness,
liveness,
loudness,
speechiness,
tempo,
time_signature,
valence
) VALUES (
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s
)""",
(
track_features.get("postgres_title"),
track_features.get("postgres_artist"),
track_features.get("postgres_year"),
track_features.get("acousticness", None),
track_features.get("danceability", None),
track_features.get("energy", None),
track_features.get("instrumentalness", None),
track_features.get("liveness", None),
track_features.get("loudness", None),
track_features.get("speechiness", None),
track_features.get("tempo", None),
track_features.get("time_signature", None),
track_features.get("valence", None),
),
)
except Exception as error:
print(f"{type(error).__name__}: {error}")
print("Query:", cur.query)
conn.rollback()
else:
conn.commit()
if __name__ == "__main__":
start = time.perf_counter()
# Connection with Postgres and Spotify App
credentials_path = os.path.join(os.path.dirname(__file__), "credentials.json")
with open(credentials_path, "r") as f:
credentials = json.load(f)
conn = psycopg2.connect(
dbname=credentials["database"],
user=credentials["username"],
host=credentials["hostname"],
password=credentials["password"],
)
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
app = spotify_client.SpotifyClient.get_credentials(credentials_path)
# Run Pipelines
beatport_pipeline()
spotify_pipeline(conn, cur, app)
# Close connection after running script
cur.close()
conn.close()
finish = time.perf_counter()
print(f"Process finished in {round(finish - start, 2)} seconds")