File size: 9,429 Bytes
a852755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importing the libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import json\n",
    "import spotipy\n",
    "import spotipy.oauth2 as oauth2\n",
    "from spotipy.oauth2 import SpotifyOAuth,SpotifyClientCredentials\n",
    "import yaml\n",
    "import re\n",
    "from tqdm import tqdm\n",
    "import multiprocessing as mp\n",
    "import time\n",
    "import random\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stream= open(\"spotify/spotify.yaml\")\n",
    "spotify_details = yaml.safe_load(stream)\n",
    "auth_manager = SpotifyClientCredentials(client_id=spotify_details['Client_id'],\n",
    "                                        client_secret=spotify_details['client_secret'])\n",
    "sp = spotipy.client.Spotify(auth_manager=auth_manager)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importing the dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Reading the 1 million playlists and keeping only the unique track URIs for the content-based recommendation system.\n",
    "- The first for loop Read the 1,000 JSON files one at a time.\n",
    "- The second for loop is for getting only the unique track."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loop_slices(path, num_slices=20):\n",
    "  cnt = 0\n",
    "  cnt1 = 0\n",
    "  mpd_playlists = []\n",
    "  unique_tracks= pd.DataFrame()\n",
    "  filenames = os.listdir(path)\n",
    "  for fname in tqdm(sorted(filenames, key=len)):\n",
    "    if fname.startswith(\"mpd.slice.\") and fname.endswith(\".json\"):\n",
    "      cnt += 1\n",
    "      fullpath = os.sep.join((path, fname))\n",
    "      f = open(fullpath)\n",
    "      js = f.read()\n",
    "      f.close()\n",
    "      current_slice = json.loads(js)\n",
    "      # Create a list of all playlists\n",
    "      for playlist in current_slice['playlists']:\n",
    "        cnt1 +=1\n",
    "        mpd_playlists.append(playlist)\n",
    "        if cnt1 == 1000:\n",
    "          cnt1=0\n",
    "          temp=pd.DataFrame(mpd_playlists)\n",
    "          temp=temp.explode('tracks')\n",
    "          temp=pd.DataFrame(temp['tracks'].apply(pd.Series))\n",
    "          unique_tracks=pd.concat([unique_tracks,temp],axis=0)\n",
    "          unique_tracks.drop_duplicates(subset=['track_uri'],inplace=True)\n",
    "          mpd_playlists = []\n",
    "      if cnt == num_slices:\n",
    "        break\n",
    "  return unique_tracks\n",
    "# Path where the json files are extracted\n",
    "path = 'spotify_million_playlist_dataset/data/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = loop_slices(path, num_slices=1000)\n",
    "df.to_csv('data/1m.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"track_uri\"] = df[\"track_uri\"].apply(lambda x: re.findall(r'\\w+$', x)[0])\n",
    "df[\"artist_uri\"] = df[\"artist_uri\"].apply(lambda x: re.findall(r'\\w+$', x)[0])\n",
    "df[\"album_uri\"] = df[\"album_uri\"].apply(lambda x: re.findall(r'\\w+$', x)[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.read_csv('Data/1mV3.csv') #Dropped all columns except ['track_uri', 'artist_uri', 'album_uri']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['track_uri', 'artist_uri', 'album_uri'], dtype='object')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t_uri=df[\"track_uri\"].unique()\n",
    "a_uri=df[\"artist_uri\"].unique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Feature extraction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the Spotify API for Feature Extraction and Saving Results to a CSV File and Errors to a Log File\n",
    "\n",
    "I was using SP.track first, but I realised that it would take a lot of time and I would have to counter a lot of Api rate limits, so I used SP.tracks and SP.artists instead. They accept lists with a 50-URI maximum and handle them in a single request, so it took a lot less time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('data/audio_features.csv','a')\n",
    "e=0\n",
    "for i in tqdm(range(0,len(t_uri),100)):\n",
    "    try:\n",
    "     track_feature = sp.audio_features(t_uri[i:i+100])\n",
    "     track_df = pd.DataFrame(track_feature)\n",
    "     csv_data = track_df.to_csv(header=False,index=False)\n",
    "     f.write(csv_data)\n",
    "    except Exception as error:\n",
    "        e+=1\n",
    "        r = open(\"audio_features_log.txt\", \"a\")\n",
    "        r.write(datetime.datetime.now().strftime(\"%d.%b %Y %H:%M:%S\")+\": \"+str(error)+'\\n')\n",
    "        r.close()\n",
    "        time.sleep(3)\n",
    "        continue\n",
    "r = open(\"audio_features_log.txt\", \"a\")\n",
    "r.write(datetime.datetime.now().strftime(\"%d.%b %Y %H:%M:%S\")+\" _________________________ \"+\"Total Number Of Errors : \"+str(e)+\" _________________________ \"+'\\n')\n",
    "r.close()\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('data/track_features.csv','a')\n",
    "e=0\n",
    "for i in tqdm(range(0,len(t_uri),50)):\n",
    "    try:\n",
    "        track_features = sp.tracks(t_uri[i:i+50])\n",
    "        for x in range(50):\n",
    "            track_pop=pd.DataFrame([t_uri[i+x]])\n",
    "            track_pop['release_date']=track_features['tracks'][x]['album']['release_date']\n",
    "            track_pop['pop'] = track_features['tracks'][x][\"popularity\"]\n",
    "            csv_data = track_pop.to_csv(header=False,index=False)\n",
    "            f.write(csv_data)\n",
    "    except Exception as error:\n",
    "        e+=1\n",
    "        r = open(\"track_features.txt\", \"a\")\n",
    "        r.write(datetime.datetime.now().strftime(\"%d.%b %Y %H:%M:%S\")+\": \"+str(error)+'\\n')\n",
    "        r.close()\n",
    "        time.sleep(3)\n",
    "        continue\n",
    "r = open(\"track_features.txt\", \"a\")\n",
    "r.write(datetime.datetime.now().strftime(\"%d.%b %Y %H:%M:%S\")+\" _________________________ \"+\"Total Number Of Errors : \"+str(e)+\" _________________________ \"+'\\n')\n",
    "r.close()\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('data/artist_features.csv','a')\n",
    "e=0\n",
    "for i in tqdm(range(0,len(a_uri),50)):\n",
    "    try:\n",
    "        artist_features = sp.artists(a_uri[i:i+50])\n",
    "        for x in range(50):\n",
    "            artist_df=pd.DataFrame([a_uri[i+x]])\n",
    "            artist_pop = artist_features['artists'][x][\"popularity\"]\n",
    "            artist_genres = artist_features['artists'][x][\"genres\"]\n",
    "            artist_df[\"artist_pop\"] = artist_pop\n",
    "            if artist_genres: \n",
    "                artist_df[\"genres\"] = \" \".join([re.sub(' ','_',i) for i in artist_genres])\n",
    "            else:\n",
    "              artist_df[\"genres\"] = \"unknown\"\n",
    "            csv_data = artist_df.to_csv(header=False,index=False)\n",
    "            f.write(csv_data)\n",
    "    except Exception as error:\n",
    "        e+=1\n",
    "        r = open(\"artist_features.txt\", \"a\")\n",
    "        r.write(datetime.datetime.now().strftime(\"%d.%b %Y %H:%M:%S\")+\": \"+str(error)+'\\n')\n",
    "        r.close()\n",
    "        time.sleep(3)\n",
    "        continue\n",
    "r = open(\"artist_features.txt\", \"a\")\n",
    "r.write(datetime.datetime.now().strftime(\"%d.%b %Y %H:%M:%S\")+\" _________________________ \"+\"Total Number Of Errors : \"+str(e)+\" _________________________ \"+'\\n')\n",
    "r.close()\n",
    "f.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.13 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "e246d2215c418239c9316a1ebf2d8abb44dc50b2e5b0e29defd87143398aa387"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}