Data processing
Data Processing
This file forms the basis of Spotify data processing.
Data Processing Documentation
add_playlist_tracking(name, store)
Method assigns the playlist name to each of the tracks to allow for traceback
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
The name of the playlist |
required |
store |
dict
|
The object in which to store extracted information |
required |
Source code in src/data_processing.py
293 294 295 296 297 298 299 300 | |
construct_storage()
Method constructs the storage dictionary in which collected track information is stored during collection, and enables saving as a csv file.
Returns:
| Type | Description |
|---|---|
dict
|
An dictionary with each of the features initialized as keys, with associated empty list values. |
Source code in src/data_processing.py
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 | |
extract_artist_info(store, sp)
Method deals with extracting artist information from the artists() API call through Spotipy
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store |
dict
|
The object in which to store extracted information |
required |
sp |
Spotipy Authorization
|
The authorized spotipy credentials object |
required |
Source code in src/data_processing.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
extract_audio_features(store, sp)
Method deal with extracting audio analysis features for a given batch of tracks
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store |
dict
|
The object in which to store extracted information |
required |
sp |
Spotipy Authorization
|
The authorized spotipy credentials object |
required |
Source code in src/data_processing.py
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 | |
extract_tracks(sp, playlist_uri, store)
Method deals with extracting tracks from a given playlist Note, this method forms the cornerstone of extraction, providing track access from a playlist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sp |
Spotipy Authorization
|
The authorized spotipy credentials object |
required |
playlist_uri |
str
|
The URI of the Spotify playlist |
required |
store |
dict
|
The object in which to store extracted information |
required |
Source code in src/data_processing.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
find_top_playlists(sp, country)
Method finds the top-20 playlists in a given country
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sp |
Spotipy Authorization
|
The authorized spotipy credentials object |
required |
country |
str
|
The ISO 3166-1 alpha-2 country code of where the playlist should be extracted from. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
uris |
list
|
A list of uris linking to each of the found playlists |
names |
list
|
A related list of playlist names corresponding to the uris |
Source code in src/data_processing.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
merge_stores(tracks_store, store)
Method deals with merging one store into another Args: store (dict): The track storage object to be merged. tracks_store (dict): The larger object in which to merge store into
Source code in src/data_processing.py
237 238 239 240 241 242 243 244 | |
process_items(store, items)
This method extracts the information from the Spotipy tracks() API call.
Information includes:
- Track uri
- Track name
- Track album
- Track popularity
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store |
dict
|
The object in which to store extracted information |
required |
items |
list
|
The list of track information generated from the Spotipy tracks API call |
required |
Returns:
| Type | Description |
|---|---|
dict
|
An updated store of information |
Source code in src/data_processing.py
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 | |
retrieve_batch_info(playlist, store)
Method retrieves the essential information from the batch API call to enable simplified extraction
Returns:
| Type | Description |
|---|---|
dict
|
A partially updated store of track information. This required further data extraction. |
Source code in src/data_processing.py
137 138 139 140 141 142 143 144 145 | |
save_data(tracks_store, name='tracks.csv')
Method deals with saving collected track data
Note, this method removes all duplicate tracks, such that all tracks within the dataset are unique, always keeping most up-to-date representation of each track.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tracks_store |
dict
|
The dictionary containing all information extracted about the tracks |
required |
name |
str
|
The name of the file to save the information to. Default is the tracks.csv dataset file. |
'tracks.csv'
|
Source code in src/data_processing.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
target_playlist_extraction(sp, url, name)
This method extracts all track information from a given target playlist Args: sp (Spotipy Authorization): The authorized spotipy credentials object url (str): The url of the playlist from which to extract information name (str): The name of the playlist
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing all features and information pertaining to the target playlist. |
Source code in src/data_processing.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
top_playlist_extraction(sp)
Method extracts the tracks in the 20 top-performing playlists from a selection of countries The countries include: Australia, UK, USA, Canada, Jamaica, South Africa
This method does not return any information, but stores it in the tracks.csv dataset file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sp |
Spotipy Authorization
|
The authorized spotipy credentials object |
required |
Source code in src/data_processing.py
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 | |
update_tracking(df)
Method updates the dataset_growth.csv file when new tracks are added to the dataset to record dataset growth
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
The dataframe containing all stored tracks, including new additions |
required |
Source code in src/data_processing.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
url2uri(url)
Method extracts the uri from a given Spotify url
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url |
str
|
The Spotify playlist url |
required |
Returns: (str): The uri of the Spotify playlist
Source code in src/data_processing.py
31 32 33 34 35 36 37 38 39 | |