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", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
albumartistsdisc_numberduration_msexplicitexternal_idsexternal_urlshrefidis_localis_playablenamepopularitypreview_urltrack_numbertypeuri
0{'album_type': 'album', 'artists': [{'external...[{'external_urls': {'spotify': 'https://open.s...1244666False{'isrc': 'GBARL9700412'}{'spotify': 'https://open.spotify.com/track/1J...https://api.spotify.com/v1/tracks/1Jaah2tmN9Hv...1Jaah2tmN9Hv81A87KZ1MUFalseTrueTorn75https://p.scdn.co/mp3-preview/0ea552243a658572...1trackspotify:track:1Jaah2tmN9Hv81A87KZ1MU
1{'album_type': 'single', 'artists': [{'externa...[{'external_urls': {'spotify': 'https://open.s...1186400False{'isrc': 'GBARL9800024'}{'spotify': 'https://open.spotify.com/track/1i...https://api.spotify.com/v1/tracks/1iPvdHIQQSNJ...1iPvdHIQQSNJ4iRm92BcbxFalseTrueTorn - Acoustic MTV Unplugged62https://p.scdn.co/mp3-preview/5d0fbf608cf7dc9b...2trackspotify:track:1iPvdHIQQSNJ4iRm92Bcbx
2{'album_type': 'album', 'artists': [{'external...[{'external_urls': {'spotify': 'https://open.s...1222653False{'isrc': 'GBHKB0500012'}{'spotify': 'https://open.spotify.com/track/2Z...https://api.spotify.com/v1/tracks/2ZudaDYcCTgq...2ZudaDYcCTgqqYPV0TrNn8FalseTrueShiver55https://p.scdn.co/mp3-preview/ad09f618ee7b88f9...2trackspotify:track:2ZudaDYcCTgqqYPV0TrNn8
3{'album_type': 'album', 'artists': [{'external...[{'external_urls': {'spotify': 'https://open.s...1256000False{'isrc': 'GBARL0100226'}{'spotify': 'https://open.spotify.com/track/2A...https://api.spotify.com/v1/tracks/2ALGjrjPZlCK...2ALGjrjPZlCKwOCh6bzXaIFalseTrueWrong Impression48https://p.scdn.co/mp3-preview/7e94cd0450a68276...5trackspotify:track:2ALGjrjPZlCKwOCh6bzXaI
4{'album_type': 'album', 'artists': [{'external...[{'external_urls': {'spotify': 'https://open.s...1232560False{'isrc': 'GBARL9700538'}{'spotify': 'https://open.spotify.com/track/7z...https://api.spotify.com/v1/tracks/7zrWPRYubwn8...7zrWPRYubwn8k8Id80LDCvFalseTrueWishing I Was There46https://p.scdn.co/mp3-preview/cb37b2b41356dd20...5trackspotify:track:7zrWPRYubwn8k8Id80LDCv
5{'album_type': 'album', 'artists': [{'external...[{'external_urls': {'spotify': 'https://open.s...1273000False{'isrc': 'GBARL9700531'}{'spotify': 'https://open.spotify.com/track/2r...https://api.spotify.com/v1/tracks/2rN6NKR8ZjMh...2rN6NKR8ZjMhcBCXsZUDWiFalseTrueBig Mistake42https://p.scdn.co/mp3-preview/997331a3153ea0e1...3trackspotify:track:2rN6NKR8ZjMhcBCXsZUDWi
6{'album_type': 'album', 'artists': [{'external...[{'external_urls': {'spotify': 'https://open.s...1249253False{'isrc': 'GBHKB0500003'}{'spotify': 'https://open.spotify.com/track/1u...https://api.spotify.com/v1/tracks/1uWEioHUYs5B...1uWEioHUYs5BD6yGdAcWZAFalseTrueCounting Down the Days41https://p.scdn.co/mp3-preview/dca674bd021abfdb...4trackspotify:track:1uWEioHUYs5BD6yGdAcWZA
7{'album_type': 'album', 'artists': [{'external...[{'external_urls': {'spotify': 'https://open.s...1277333False{'isrc': 'GBARL9700534'}{'spotify': 'https://open.spotify.com/track/4T...https://api.spotify.com/v1/tracks/4Tj6t4ZdZMks...4Tj6t4ZdZMksIyzoQyTeXeFalseTrueSmoke41https://p.scdn.co/mp3-preview/c60c1a8850188c88...6trackspotify:track:4Tj6t4ZdZMksIyzoQyTeXe
8{'album_type': 'album', 'artists': [{'external...[{'external_urls': {'spotify': 'https://open.s...1194480False{'isrc': 'GB5KW2100733'}{'spotify': 'https://open.spotify.com/track/56...https://api.spotify.com/v1/tracks/56UsKyO3TOiN...56UsKyO3TOiNHWmSWqbZiqFalseTrueNothing Missing40https://p.scdn.co/mp3-preview/6a0f94da18f00c3d...2trackspotify:track:56UsKyO3TOiNHWmSWqbZiq
9{'album_type': 'album', 'artists': [{'external...[{'external_urls': {'spotify': 'https://open.s...1203533False{'isrc': 'GB5KW2100732'}{'spotify': 'https://open.spotify.com/track/6p...https://api.spotify.com/v1/tracks/6pIGsoeExDqC...6pIGsoeExDqCajU70ulflEFalseTrueBuild It Better39https://p.scdn.co/mp3-preview/f1dd9eff5e268b54...1trackspotify:track:6pIGsoeExDqCajU70ulflE
\n", + "
" + ], + "text/plain": [ + " album \\\n", + "0 {'album_type': 'album', 'artists': [{'external... \n", + "1 {'album_type': 'single', 'artists': [{'externa... \n", + "2 {'album_type': 'album', 'artists': [{'external... \n", + "3 {'album_type': 'album', 'artists': [{'external... \n", + "4 {'album_type': 'album', 'artists': [{'external... \n", + "5 {'album_type': 'album', 'artists': [{'external... \n", + "6 {'album_type': 'album', 'artists': [{'external... \n", + "7 {'album_type': 'album', 'artists': [{'external... \n", + "8 {'album_type': 'album', 'artists': [{'external... \n", + "9 {'album_type': 'album', 'artists': [{'external... \n", + "\n", + " artists disc_number \\\n", + "0 [{'external_urls': {'spotify': 'https://open.s... 1 \n", + "1 [{'external_urls': {'spotify': 'https://open.s... 1 \n", + "2 [{'external_urls': {'spotify': 'https://open.s... 1 \n", + "3 [{'external_urls': {'spotify': 'https://open.s... 1 \n", + "4 [{'external_urls': {'spotify': 'https://open.s... 1 \n", + "5 [{'external_urls': {'spotify': 'https://open.s... 1 \n", + "6 [{'external_urls': {'spotify': 'https://open.s... 1 \n", + "7 [{'external_urls': {'spotify': 'https://open.s... 1 \n", + "8 [{'external_urls': {'spotify': 'https://open.s... 1 \n", + "9 [{'external_urls': {'spotify': 'https://open.s... 1 \n", + "\n", + " duration_ms explicit external_ids \\\n", + "0 244666 False {'isrc': 'GBARL9700412'} \n", + "1 186400 False {'isrc': 'GBARL9800024'} \n", + "2 222653 False {'isrc': 'GBHKB0500012'} \n", + "3 256000 False {'isrc': 'GBARL0100226'} \n", + "4 232560 False {'isrc': 'GBARL9700538'} \n", + "5 273000 False {'isrc': 'GBARL9700531'} \n", + "6 249253 False {'isrc': 'GBHKB0500003'} \n", + "7 277333 False {'isrc': 'GBARL9700534'} \n", + "8 194480 False {'isrc': 'GB5KW2100733'} \n", + "9 203533 False {'isrc': 'GB5KW2100732'} \n", + "\n", + " external_urls \\\n", + "0 {'spotify': 'https://open.spotify.com/track/1J... \n", + "1 {'spotify': 'https://open.spotify.com/track/1i... \n", + "2 {'spotify': 'https://open.spotify.com/track/2Z... \n", + "3 {'spotify': 'https://open.spotify.com/track/2A... \n", + "4 {'spotify': 'https://open.spotify.com/track/7z... \n", + "5 {'spotify': 'https://open.spotify.com/track/2r... \n", + "6 {'spotify': 'https://open.spotify.com/track/1u... \n", + "7 {'spotify': 'https://open.spotify.com/track/4T... \n", + "8 {'spotify': 'https://open.spotify.com/track/56... \n", + "9 {'spotify': 'https://open.spotify.com/track/6p... \n", + "\n", + " href id \\\n", + "0 https://api.spotify.com/v1/tracks/1Jaah2tmN9Hv... 1Jaah2tmN9Hv81A87KZ1MU \n", + "1 https://api.spotify.com/v1/tracks/1iPvdHIQQSNJ... 1iPvdHIQQSNJ4iRm92Bcbx \n", + "2 https://api.spotify.com/v1/tracks/2ZudaDYcCTgq... 2ZudaDYcCTgqqYPV0TrNn8 \n", + "3 https://api.spotify.com/v1/tracks/2ALGjrjPZlCK... 2ALGjrjPZlCKwOCh6bzXaI \n", + "4 https://api.spotify.com/v1/tracks/7zrWPRYubwn8... 7zrWPRYubwn8k8Id80LDCv \n", + "5 https://api.spotify.com/v1/tracks/2rN6NKR8ZjMh... 2rN6NKR8ZjMhcBCXsZUDWi \n", + "6 https://api.spotify.com/v1/tracks/1uWEioHUYs5B... 1uWEioHUYs5BD6yGdAcWZA \n", + "7 https://api.spotify.com/v1/tracks/4Tj6t4ZdZMks... 4Tj6t4ZdZMksIyzoQyTeXe \n", + "8 https://api.spotify.com/v1/tracks/56UsKyO3TOiN... 56UsKyO3TOiNHWmSWqbZiq \n", + "9 https://api.spotify.com/v1/tracks/6pIGsoeExDqC... 6pIGsoeExDqCajU70ulflE \n", + "\n", + " is_local is_playable name popularity \\\n", + "0 False True Torn 75 \n", + "1 False True Torn - Acoustic MTV Unplugged 62 \n", + "2 False True Shiver 55 \n", + "3 False True Wrong Impression 48 \n", + "4 False True Wishing I Was There 46 \n", + "5 False True Big Mistake 42 \n", + "6 False True Counting Down the Days 41 \n", + "7 False True Smoke 41 \n", + "8 False True Nothing Missing 40 \n", + "9 False True Build It Better 39 \n", + "\n", + " preview_url track_number type \\\n", + "0 https://p.scdn.co/mp3-preview/0ea552243a658572... 1 track \n", + "1 https://p.scdn.co/mp3-preview/5d0fbf608cf7dc9b... 2 track \n", + "2 https://p.scdn.co/mp3-preview/ad09f618ee7b88f9... 2 track \n", + "3 https://p.scdn.co/mp3-preview/7e94cd0450a68276... 5 track \n", + "4 https://p.scdn.co/mp3-preview/cb37b2b41356dd20... 5 track \n", + "5 https://p.scdn.co/mp3-preview/997331a3153ea0e1... 3 track \n", + "6 https://p.scdn.co/mp3-preview/dca674bd021abfdb... 4 track \n", + "7 https://p.scdn.co/mp3-preview/c60c1a8850188c88... 6 track \n", + "8 https://p.scdn.co/mp3-preview/6a0f94da18f00c3d... 2 track \n", + "9 https://p.scdn.co/mp3-preview/f1dd9eff5e268b54... 1 track \n", + "\n", + " uri \n", + "0 spotify:track:1Jaah2tmN9Hv81A87KZ1MU \n", + "1 spotify:track:1iPvdHIQQSNJ4iRm92Bcbx \n", + "2 spotify:track:2ZudaDYcCTgqqYPV0TrNn8 \n", + "3 spotify:track:2ALGjrjPZlCKwOCh6bzXaI \n", + "4 spotify:track:7zrWPRYubwn8k8Id80LDCv \n", + "5 spotify:track:2rN6NKR8ZjMhcBCXsZUDWi \n", + "6 spotify:track:1uWEioHUYs5BD6yGdAcWZA \n", + "7 spotify:track:4Tj6t4ZdZMksIyzoQyTeXe \n", + "8 spotify:track:56UsKyO3TOiNHWmSWqbZiq \n", + "9 spotify:track:6pIGsoeExDqCajU70ulflE " + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# aquí ya mostramos el df con las top tracks de un único artista. El siguiente paso ya sería iterar para obtener\n", + "# las top tracks de cada id de artista relacionado.\n", "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n" + "df = pd.DataFrame(toptracks_rel_art['tracks'])\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "708cd4aa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Natalie Imbruglia'" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['artists'][0][0]['name']" + ] + }, + { + "cell_type": "markdown", + "id": "112bf335", + "metadata": {}, + "source": [ + "# SOLUCION FINAL" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "1bbe30c7", + "metadata": {}, + "outputs": [], + "source": [ + "# hay que crear una función que saque el nombre del artista que se encuentra dentro de un diccionario en el df anterior.\n", + "def funcion_nom(a):\n", + " return df['artists'][0][0]['name']" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "7f319238", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
nameidartists
0Torn1Jaah2tmN9Hv81A87KZ1MUNatalie Imbruglia
1Torn - Acoustic MTV Unplugged1iPvdHIQQSNJ4iRm92BcbxNatalie Imbruglia
2Shiver2ZudaDYcCTgqqYPV0TrNn8Natalie Imbruglia
3Wrong Impression2ALGjrjPZlCKwOCh6bzXaINatalie Imbruglia
4Wishing I Was There7zrWPRYubwn8k8Id80LDCvNatalie Imbruglia
............
5No More \"I Love You's\"2Pn5gvVioma5LHPxgEBBmDEurythmics
6Thorn in My Side - Remastered3v9ELl9T121dhadZyIiQVnEurythmics
7There Must Be an Angel (Playing With My Heart)1YJ4vpxPL6Wgo8gmRnvqSpEurythmics
8A Whiter Shade of Pale11Mkoen07pKwyDqHiFnVU9Eurythmics
9Sweet Dreams (Are Made of This)6zIfdCmmhPCX0cMquBsC37Eurythmics
\n", + "

200 rows × 3 columns

\n", + "
" + ], + "text/plain": [ + " name id \\\n", + "0 Torn 1Jaah2tmN9Hv81A87KZ1MU \n", + "1 Torn - Acoustic MTV Unplugged 1iPvdHIQQSNJ4iRm92Bcbx \n", + "2 Shiver 2ZudaDYcCTgqqYPV0TrNn8 \n", + "3 Wrong Impression 2ALGjrjPZlCKwOCh6bzXaI \n", + "4 Wishing I Was There 7zrWPRYubwn8k8Id80LDCv \n", + ".. ... ... \n", + "5 No More \"I Love You's\" 2Pn5gvVioma5LHPxgEBBmD \n", + "6 Thorn in My Side - Remastered 3v9ELl9T121dhadZyIiQVn \n", + "7 There Must Be an Angel (Playing With My Heart) 1YJ4vpxPL6Wgo8gmRnvqSp \n", + "8 A Whiter Shade of Pale 11Mkoen07pKwyDqHiFnVU9 \n", + "9 Sweet Dreams (Are Made of This) 6zIfdCmmhPCX0cMquBsC37 \n", + "\n", + " artists \n", + "0 Natalie Imbruglia \n", + "1 Natalie Imbruglia \n", + "2 Natalie Imbruglia \n", + "3 Natalie Imbruglia \n", + "4 Natalie Imbruglia \n", + ".. ... \n", + "5 Eurythmics \n", + "6 Eurythmics \n", + "7 Eurythmics \n", + "8 Eurythmics \n", + "9 Eurythmics \n", + "\n", + "[200 rows x 3 columns]" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_df = []\n", + "for i in lista_id_rel_art:\n", + " response = requests.get(base_url + resource + '/' + i + top_tracks, headers=header).json()\n", + " df = pd.DataFrame(response['tracks'])\n", + " df = df[['name', 'id','artists']]\n", + " df['artists'] = df['artists'].apply(funcion_nom)\n", + " lista_df.append(df)\n", + " df = pd.concat(lista_df)\n", + "df" ] }, { @@ -182,9 +999,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python [conda env:.conda-ironhack]", + "display_name": "Python [conda env:.conda-m1_ih]", "language": "python", - "name": "conda-env-.conda-ironhack-py" + "name": "conda-env-.conda-m1_ih-py" }, "language_info": { "codemirror_mode": { @@ -196,7 +1013,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.11" + "version": "3.9.12" } }, "nbformat": 4,