diff --git a/notebooks/spotify_rest_api_challenge.ipynb b/notebooks/spotify_rest_api_challenge.ipynb index ad5fe90..8367c4d 100644 --- a/notebooks/spotify_rest_api_challenge.ipynb +++ b/notebooks/spotify_rest_api_challenge.ipynb @@ -16,13 +16,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "17a49776", "metadata": {}, "outputs": [], "source": [ "# imports\n", - "\n" + "import pandas as pd\n", + "import requests\n", + "import re\n", + "import json\n", + "import math\n", + "from pandas.io.json import json_normalize" ] }, { @@ -37,21 +42,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "7b5d2e52", "metadata": {}, "outputs": [], "source": [ "# generate token with a POST request\n", "\n", - "client_id = # CLIENT ID\n", - "client_secret = # CLIENT SECRET\n", + "client_id = '88952c12a75844f6b3a1f5acf274c11d' # CLIENT ID\n", + "client_secret = '7fce4b1b447a4a95983343a0c54ab407' # CLIENT SECRET\n", "auth_url = 'https://accounts.spotify.com/api/token'" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "c7901d1e", "metadata": {}, "outputs": [], @@ -63,10 +68,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, + "id": "027a869d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'access_token': 'BQDEtK3tg01QpGUblIy8WcQ14g6vrbuNU_lXnNf1Wwhq0LZMPZSvJNwxQPjiFHu-pRSiHxbaoM-yBycLuZGBAuUt6Z8oe5iphZqyYFnpifbyhpzHMtE',\n", + " 'token_type': 'Bearer',\n", + " 'expires_in': 3600}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# buscamos que nos devuelva un json del token\n", + "auth_response" + ] + }, + { + "cell_type": "code", + "execution_count": 5, "id": "262e7e18", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'access_token': 'BQDEtK3tg01QpGUblIy8WcQ14g6vrbuNU_lXnNf1Wwhq0LZMPZSvJNwxQPjiFHu-pRSiHxbaoM-yBycLuZGBAuUt6Z8oe5iphZqyYFnpifbyhpzHMtE',\n", + " 'token_type': 'Bearer',\n", + " 'expires_in': 3600}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "access_token = auth_response['access_token']\n", "auth_response" @@ -84,16 +126,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "62e6d4fb", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Authorization': 'Bearer BQDEtK3tg01QpGUblIy8WcQ14g6vrbuNU_lXnNf1Wwhq0LZMPZSvJNwxQPjiFHu-pRSiHxbaoM-yBycLuZGBAuUt6Z8oe5iphZqyYFnpifbyhpzHMtE'}" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# setting the main variables\n", "\n", "base_url = 'https://api.spotify.com/v1'\n", "resource = '/artists'\n", - "header = {'Authorization': 'Bearer {token}'.format(token=access_token)}" + "parametro = '/6ogn9necmbUdCppmNnGOdi'\n", + "related_artist = '/related-artists'\n", + "top_tracks = '/top-tracks' + '?market=ES'\n", + "header = {'Authorization': 'Bearer {token}'.format(token=access_token)}\n", + "header" ] }, { @@ -134,31 +191,791 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "50ec3959", + "execution_count": 11, + "id": "e0f0c68d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'https://api.spotify.com/v1/artists/6ogn9necmbUdCppmNnGOdi'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "url = base_url+resource+parametro #+ '/top-tracks' + '?market=ES'\n", + "url" + ] + }, + { + "cell_type": "markdown", + "id": "94ab3d67", + "metadata": {}, + "source": [ + "## Vamos a encontrar el ID del artista" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "50ec3959", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['external_urls', 'followers', 'genres', 'href', 'id', 'images', 'name', 'popularity', 'type', 'uri'])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# your code\n", + "# en este caso debido al servidor de destino API, tenemos que especificar el parámetro headers para que identifique el header.\n", "\n", + "resp_art = requests.get(url, headers = header)\n", + "main_art = resp_art.json()\n", + "main_art.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "ae04b7e8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'6ogn9necmbUdCppmNnGOdi'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "id_main_art=main_art['id']\n", + "id_main_art" + ] + }, + { + "cell_type": "markdown", + "id": "6b1792f5", + "metadata": {}, + "source": [ + "## Ahora Artistas relacionados" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "ae6ac229", + "metadata": {}, + "outputs": [], + "source": [ + "url_rel_art = base_url+resource+'/'+ id_main_art + related_artist" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "6ff97ebe", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'external_urls': {'spotify': 'https://open.spotify.com/artist/0dlOr0VIysztGWvU1dpjmP'},\n", + " 'followers': {'href': None, 'total': 794941},\n", + " 'genres': ['australian pop',\n", + " 'dance pop',\n", + " 'europop',\n", + " 'lilith',\n", + " 'new wave pop',\n", + " 'pop rock'],\n", + " 'href': 'https://api.spotify.com/v1/artists/0dlOr0VIysztGWvU1dpjmP',\n", + " 'id': '0dlOr0VIysztGWvU1dpjmP',\n", + " 'images': [{'height': 640,\n", + " 'url': 'https://i.scdn.co/image/ab6761610000e5eb2e023776b822635846e1f5f1',\n", + " 'width': 640},\n", + " {'height': 320,\n", + " 'url': 'https://i.scdn.co/image/ab676161000051742e023776b822635846e1f5f1',\n", + " 'width': 320},\n", + " {'height': 160,\n", + " 'url': 'https://i.scdn.co/image/ab6761610000f1782e023776b822635846e1f5f1',\n", + " 'width': 160}],\n", + " 'name': 'Natalie Imbruglia',\n", + " 'popularity': 60,\n", + " 'type': 'artist',\n", + " 'uri': 'spotify:artist:0dlOr0VIysztGWvU1dpjmP'}" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rel_art = requests.get(url_rel_art, headers = header).json()\n", "\n", + "#miramos dentro de uno de los artistas para conocer la información e identificar donde están sus ids para acceder a sus perfiles\n", + "rel_art['artists'][0]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "0de74baf", + "metadata": {}, + "outputs": [], + "source": [ + "# buscamos ahora los ids de los artistas relacionados para luego buscar sus top tracks\n", + "def art_rel_id(x):\n", + " a = []\n", + " for i in x['artists']:\n", + " a.append(i['id'])\n", + " return a" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "2694d44f", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['0dlOr0VIysztGWvU1dpjmP',\n", + " '6FbDoZnMBTdhhhLuJBOOqP',\n", + " '4TKTii6gnOnUXQHyuo9JaD',\n", + " '2mpeljBig2IXLXRAFO9AAs',\n", + " '1KsASRNugxU85T0u6zSg32',\n", + " '1VbWUxZTRNY2gw3qZ1tg9W',\n", + " '4NgNsOXSwIzXlUIJcpnNUp',\n", + " '0cQbJU1aAzvbEmTuljWLlF',\n", + " '7t0rwkOPGlDPEhaOcVtOt9',\n", + " '1tqZaCwM57UFKjWoYwMLrw',\n", + " '73JEBdDEFeVaOLg3y0HhBD',\n", + " '6S0GHTqz5sxK5f9HtLXn9q',\n", + " '5rScKX1Sh1U67meeUyTGwk',\n", + " '5zzrJD2jXrE9dZ1AklRFcL',\n", + " '0lJlKQvuM2Sd9DPPyUXcHg',\n", + " '08DLZqQd6XDAVteF9nSEJ1',\n", + " '01Ppu7N8uYJI8SAONo2YZA',\n", + " '3g2kUQ6tHLLbmkV7T4GPtL',\n", + " '4sD9znwiVFx9cgRPZ42aQ1',\n", + " '5MspMQqdVbdwP6ax3GXqum']" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_id_rel_art = art_rel_id(rel_art)\n", + "lista_id_rel_art" + ] + }, + { + "cell_type": "markdown", + "id": "cbd33d36", + "metadata": {}, + "source": [ + "## Buscamos Top Tracks" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "aec54071", + "metadata": {}, + "outputs": [], + "source": [ + "#probamos primero con id específico del primer artista relacionado\n", "\n", + "id_art_rel = '0dlOr0VIysztGWvU1dpjmP'\n", + "url_toptracks_rel_art = base_url+resource+ '/'+ id_art_rel + top_tracks" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "4ef50613", + "metadata": {}, + "outputs": [], + "source": [ + "toptracks_rel_art = requests.get(url_toptracks_rel_art, headers = header).json()" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "105ef0fd", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
| \n", + " | album | \n", + "artists | \n", + "disc_number | \n", + "duration_ms | \n", + "explicit | \n", + "external_ids | \n", + "external_urls | \n", + "href | \n", + "id | \n", + "is_local | \n", + "is_playable | \n", + "name | \n", + "popularity | \n", + "preview_url | \n", + "track_number | \n", + "type | \n", + "uri | \n", + "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", + "{'album_type': 'album', 'artists': [{'external... | \n", + "[{'external_urls': {'spotify': 'https://open.s... | \n", + "1 | \n", + "244666 | \n", + "False | \n", + "{'isrc': 'GBARL9700412'} | \n", + "{'spotify': 'https://open.spotify.com/track/1J... | \n", + "https://api.spotify.com/v1/tracks/1Jaah2tmN9Hv... | \n", + "1Jaah2tmN9Hv81A87KZ1MU | \n", + "False | \n", + "True | \n", + "Torn | \n", + "75 | \n", + "https://p.scdn.co/mp3-preview/0ea552243a658572... | \n", + "1 | \n", + "track | \n", + "spotify:track:1Jaah2tmN9Hv81A87KZ1MU | \n", + "
| 1 | \n", + "{'album_type': 'single', 'artists': [{'externa... | \n", + "[{'external_urls': {'spotify': 'https://open.s... | \n", + "1 | \n", + "186400 | \n", + "False | \n", + "{'isrc': 'GBARL9800024'} | \n", + "{'spotify': 'https://open.spotify.com/track/1i... | \n", + "https://api.spotify.com/v1/tracks/1iPvdHIQQSNJ... | \n", + "1iPvdHIQQSNJ4iRm92Bcbx | \n", + "False | \n", + "True | \n", + "Torn - Acoustic MTV Unplugged | \n", + "62 | \n", + "https://p.scdn.co/mp3-preview/5d0fbf608cf7dc9b... | \n", + "2 | \n", + "track | \n", + "spotify:track:1iPvdHIQQSNJ4iRm92Bcbx | \n", + "
| 2 | \n", + "{'album_type': 'album', 'artists': [{'external... | \n", + "[{'external_urls': {'spotify': 'https://open.s... | \n", + "1 | \n", + "222653 | \n", + "False | \n", + "{'isrc': 'GBHKB0500012'} | \n", + "{'spotify': 'https://open.spotify.com/track/2Z... | \n", + "https://api.spotify.com/v1/tracks/2ZudaDYcCTgq... | \n", + "2ZudaDYcCTgqqYPV0TrNn8 | \n", + "False | \n", + "True | \n", + "Shiver | \n", + "55 | \n", + "https://p.scdn.co/mp3-preview/ad09f618ee7b88f9... | \n", + "2 | \n", + "track | \n", + "spotify:track:2ZudaDYcCTgqqYPV0TrNn8 | \n", + "
| 3 | \n", + "{'album_type': 'album', 'artists': [{'external... | \n", + "[{'external_urls': {'spotify': 'https://open.s... | \n", + "1 | \n", + "256000 | \n", + "False | \n", + "{'isrc': 'GBARL0100226'} | \n", + "{'spotify': 'https://open.spotify.com/track/2A... | \n", + "https://api.spotify.com/v1/tracks/2ALGjrjPZlCK... | \n", + "2ALGjrjPZlCKwOCh6bzXaI | \n", + "False | \n", + "True | \n", + "Wrong Impression | \n", + "48 | \n", + "https://p.scdn.co/mp3-preview/7e94cd0450a68276... | \n", + "5 | \n", + "track | \n", + "spotify:track:2ALGjrjPZlCKwOCh6bzXaI | \n", + "
| 4 | \n", + "{'album_type': 'album', 'artists': [{'external... | \n", + "[{'external_urls': {'spotify': 'https://open.s... | \n", + "1 | \n", + "232560 | \n", + "False | \n", + "{'isrc': 'GBARL9700538'} | \n", + "{'spotify': 'https://open.spotify.com/track/7z... | \n", + "https://api.spotify.com/v1/tracks/7zrWPRYubwn8... | \n", + "7zrWPRYubwn8k8Id80LDCv | \n", + "False | \n", + "True | \n", + "Wishing I Was There | \n", + "46 | \n", + "https://p.scdn.co/mp3-preview/cb37b2b41356dd20... | \n", + "5 | \n", + "track | \n", + "spotify:track:7zrWPRYubwn8k8Id80LDCv | \n", + "
| 5 | \n", + "{'album_type': 'album', 'artists': [{'external... | \n", + "[{'external_urls': {'spotify': 'https://open.s... | \n", + "1 | \n", + "273000 | \n", + "False | \n", + "{'isrc': 'GBARL9700531'} | \n", + "{'spotify': 'https://open.spotify.com/track/2r... | \n", + "https://api.spotify.com/v1/tracks/2rN6NKR8ZjMh... | \n", + "2rN6NKR8ZjMhcBCXsZUDWi | \n", + "False | \n", + "True | \n", + "Big Mistake | \n", + "42 | \n", + "https://p.scdn.co/mp3-preview/997331a3153ea0e1... | \n", + "3 | \n", + "track | \n", + "spotify:track:2rN6NKR8ZjMhcBCXsZUDWi | \n", + "
| 6 | \n", + "{'album_type': 'album', 'artists': [{'external... | \n", + "[{'external_urls': {'spotify': 'https://open.s... | \n", + "1 | \n", + "249253 | \n", + "False | \n", + "{'isrc': 'GBHKB0500003'} | \n", + "{'spotify': 'https://open.spotify.com/track/1u... | \n", + "https://api.spotify.com/v1/tracks/1uWEioHUYs5B... | \n", + "1uWEioHUYs5BD6yGdAcWZA | \n", + "False | \n", + "True | \n", + "Counting Down the Days | \n", + "41 | \n", + "https://p.scdn.co/mp3-preview/dca674bd021abfdb... | \n", + "4 | \n", + "track | \n", + "spotify:track:1uWEioHUYs5BD6yGdAcWZA | \n", + "
| 7 | \n", + "{'album_type': 'album', 'artists': [{'external... | \n", + "[{'external_urls': {'spotify': 'https://open.s... | \n", + "1 | \n", + "277333 | \n", + "False | \n", + "{'isrc': 'GBARL9700534'} | \n", + "{'spotify': 'https://open.spotify.com/track/4T... | \n", + "https://api.spotify.com/v1/tracks/4Tj6t4ZdZMks... | \n", + "4Tj6t4ZdZMksIyzoQyTeXe | \n", + "False | \n", + "True | \n", + "Smoke | \n", + "41 | \n", + "https://p.scdn.co/mp3-preview/c60c1a8850188c88... | \n", + "6 | \n", + "track | \n", + "spotify:track:4Tj6t4ZdZMksIyzoQyTeXe | \n", + "
| 8 | \n", + "{'album_type': 'album', 'artists': [{'external... | \n", + "[{'external_urls': {'spotify': 'https://open.s... | \n", + "1 | \n", + "194480 | \n", + "False | \n", + "{'isrc': 'GB5KW2100733'} | \n", + "{'spotify': 'https://open.spotify.com/track/56... | \n", + "https://api.spotify.com/v1/tracks/56UsKyO3TOiN... | \n", + "56UsKyO3TOiNHWmSWqbZiq | \n", + "False | \n", + "True | \n", + "Nothing Missing | \n", + "40 | \n", + "https://p.scdn.co/mp3-preview/6a0f94da18f00c3d... | \n", + "2 | \n", + "track | \n", + "spotify:track:56UsKyO3TOiNHWmSWqbZiq | \n", + "
| 9 | \n", + "{'album_type': 'album', 'artists': [{'external... | \n", + "[{'external_urls': {'spotify': 'https://open.s... | \n", + "1 | \n", + "203533 | \n", + "False | \n", + "{'isrc': 'GB5KW2100732'} | \n", + "{'spotify': 'https://open.spotify.com/track/6p... | \n", + "https://api.spotify.com/v1/tracks/6pIGsoeExDqC... | \n", + "6pIGsoeExDqCajU70ulflE | \n", + "False | \n", + "True | \n", + "Build It Better | \n", + "39 | \n", + "https://p.scdn.co/mp3-preview/f1dd9eff5e268b54... | \n", + "1 | \n", + "track | \n", + "spotify:track:6pIGsoeExDqCajU70ulflE | \n", + "
| \n", + " | name | \n", + "id | \n", + "artists | \n", + "
|---|---|---|---|
| 0 | \n", + "Torn | \n", + "1Jaah2tmN9Hv81A87KZ1MU | \n", + "Natalie Imbruglia | \n", + "
| 1 | \n", + "Torn - Acoustic MTV Unplugged | \n", + "1iPvdHIQQSNJ4iRm92Bcbx | \n", + "Natalie Imbruglia | \n", + "
| 2 | \n", + "Shiver | \n", + "2ZudaDYcCTgqqYPV0TrNn8 | \n", + "Natalie Imbruglia | \n", + "
| 3 | \n", + "Wrong Impression | \n", + "2ALGjrjPZlCKwOCh6bzXaI | \n", + "Natalie Imbruglia | \n", + "
| 4 | \n", + "Wishing I Was There | \n", + "7zrWPRYubwn8k8Id80LDCv | \n", + "Natalie Imbruglia | \n", + "
| ... | \n", + "... | \n", + "... | \n", + "... | \n", + "
| 5 | \n", + "No More \"I Love You's\" | \n", + "2Pn5gvVioma5LHPxgEBBmD | \n", + "Eurythmics | \n", + "
| 6 | \n", + "Thorn in My Side - Remastered | \n", + "3v9ELl9T121dhadZyIiQVn | \n", + "Eurythmics | \n", + "
| 7 | \n", + "There Must Be an Angel (Playing With My Heart) | \n", + "1YJ4vpxPL6Wgo8gmRnvqSp | \n", + "Eurythmics | \n", + "
| 8 | \n", + "A Whiter Shade of Pale | \n", + "11Mkoen07pKwyDqHiFnVU9 | \n", + "Eurythmics | \n", + "
| 9 | \n", + "Sweet Dreams (Are Made of This) | \n", + "6zIfdCmmhPCX0cMquBsC37 | \n", + "Eurythmics | \n", + "
200 rows × 3 columns
\n", + "