diff --git a/notebooks/spotify_rest_api_challenge.ipynb b/notebooks/spotify_rest_api_challenge.ipynb index 5e4f8f7..feb328b 100644 --- a/notebooks/spotify_rest_api_challenge.ipynb +++ b/notebooks/spotify_rest_api_challenge.ipynb @@ -16,14 +16,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 139, "id": "17a49776", "metadata": {}, "outputs": [], "source": [ "# Imports\n", - "\n", - "\n" + "import pandas as pd\n", + "import requests\n", + "from dotenv import dotenv_values\n", + "import random\n", + "import string\n", + "import requests\n", + "import webbrowser" ] }, { @@ -38,21 +43,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 143, "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 = dotenv_values(\"./.env\")['CLIENT_ID']\n", + "client_secret = dotenv_values(\"./.env\")['CLIENT_SECRET']\n", "auth_url = 'https://accounts.spotify.com/api/token'" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 144, "id": "c7901d1e", "metadata": {}, "outputs": [], @@ -64,10 +68,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 145, "id": "262e7e18", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'access_token': 'BQCEHHU8phUn8jmqfrwJi4EZHDD5o9WRoJDntoJIuMg26iO3G7Nwzype9tKw9TDD1hiLnm9XqHu_e7yZRxlUl5dPdA0PNmHRq-QNCbdNUfkc0IBj4Cg',\n", + " 'token_type': 'Bearer',\n", + " 'expires_in': 3600}" + ] + }, + "execution_count": 145, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "access_token = auth_response['access_token']\n", "auth_response" @@ -85,7 +102,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "62e6d4fb", "metadata": {}, "outputs": [], @@ -143,27 +160,577 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "04f85940", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'https://api.spotify.com/v1/artists/2Wwiu1wnq1W8AMYbWuRFDH'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Here you can complete your endpoint URI\n", + "artist = '2Wwiu1wnq1W8AMYbWuRFDH'\n", + "\n", + "full_endpoint = base_url + resource + artist\n", "\n", - "full_endpoint = \n", "full_endpoint" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "50ec3959", "metadata": {}, "outputs": [], "source": [ "# Start building your playlist!!!\n", "\n", - "\n" + "relres = requests.get(full_endpoint + '/related-artists', headers=header_info)\n", + "related = relres.json()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c395900d", + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
genreshrefidimagesnamepopularitytypeuriexternal_urls.spotifyfollowers.hreffollowers.total
0[murcia indie, rock alternativo espanol, spani...https://api.spotify.com/v1/artists/57s0ep3eNSg...57s0ep3eNSg81D7ZxiuHbC[{'height': 640, 'url': 'https://i.scdn.co/ima...Viva Suecia56artistspotify:artist:57s0ep3eNSg81D7ZxiuHbChttps://open.spotify.com/artist/57s0ep3eNSg81D...None112431
1[rock alternativo espanol, spanish indie pop, ...https://api.spotify.com/v1/artists/6rRTrEHzGSD...6rRTrEHzGSDqhmFJQrNFMO[{'height': 640, 'url': 'https://i.scdn.co/ima...Shinova49artistspotify:artist:6rRTrEHzGSDqhmFJQrNFMOhttps://open.spotify.com/artist/6rRTrEHzGSDqhm...None53126
2[spanish indie pop, spanish modern rock, tonti...https://api.spotify.com/v1/artists/2k60o7SVpOK...2k60o7SVpOKmzpuTEAG3L8[{'height': 640, 'url': 'https://i.scdn.co/ima...Veintiuno47artistspotify:artist:2k60o7SVpOKmzpuTEAG3L8https://open.spotify.com/artist/2k60o7SVpOKmzp...None28290
3[spanish modern rock]https://api.spotify.com/v1/artists/3wcfgYTtdxm...3wcfgYTtdxmXrhuUuq3kEe[{'height': 640, 'url': 'https://i.scdn.co/ima...Carmen 11337artistspotify:artist:3wcfgYTtdxmXrhuUuq3kEehttps://open.spotify.com/artist/3wcfgYTtdxmXrh...None5172
4[spanish indie pop]https://api.spotify.com/v1/artists/5q3atpVIFpX...5q3atpVIFpXkHlcOKyK0H4[{'height': 640, 'url': 'https://i.scdn.co/ima...Siloé47artistspotify:artist:5q3atpVIFpXkHlcOKyK0H4https://open.spotify.com/artist/5q3atpVIFpXkHl...None32821
5[spanish indie pop, spanish modern rock]https://api.spotify.com/v1/artists/7JrMDzLZQ25...7JrMDzLZQ25BJwc4tD8jl8[{'height': 640, 'url': 'https://i.scdn.co/ima...ELYELLA46artistspotify:artist:7JrMDzLZQ25BJwc4tD8jl8https://open.spotify.com/artist/7JrMDzLZQ25BJw...None28997
6[murcia indie, spanish indie pop, spanish mode...https://api.spotify.com/v1/artists/3Ju3bW8OaeU...3Ju3bW8OaeUmfQKqMtATKr[{'height': 640, 'url': 'https://i.scdn.co/ima...Second45artistspotify:artist:3Ju3bW8OaeUmfQKqMtATKrhttps://open.spotify.com/artist/3Ju3bW8OaeUmfQ...None92945
7[]https://api.spotify.com/v1/artists/4H3BAAfpEcL...4H3BAAfpEcLHQkaQKfvoW8[{'height': 640, 'url': 'https://i.scdn.co/ima...Mikel Izal51artistspotify:artist:4H3BAAfpEcLHQkaQKfvoW8https://open.spotify.com/artist/4H3BAAfpEcLHQk...None31895
8[spanish indie pop]https://api.spotify.com/v1/artists/2FN1YrHU65d...2FN1YrHU65dhPZKl3GD7Vd[{'height': 640, 'url': 'https://i.scdn.co/ima...Sexy Zebras40artistspotify:artist:2FN1YrHU65dhPZKl3GD7Vdhttps://open.spotify.com/artist/2FN1YrHU65dhPZ...None28051
9[granada indie, spanish indie pop]https://api.spotify.com/v1/artists/3mOsjj1Mhoc...3mOsjj1MhocRVwOejIZlTi[{'height': 640, 'url': 'https://i.scdn.co/ima...Lori Meyers51artistspotify:artist:3mOsjj1MhocRVwOejIZlTihttps://open.spotify.com/artist/3mOsjj1MhocRVw...None274606
10[spanish indie pop, spanish rock]https://api.spotify.com/v1/artists/1vBn5Puz4md...1vBn5Puz4mdZopZEHq1QDq[{'height': 640, 'url': 'https://i.scdn.co/ima...La Maravillosa Orquesta del Alcohol56artistspotify:artist:1vBn5Puz4mdZopZEHq1QDqhttps://open.spotify.com/artist/1vBn5Puz4mdZop...None221054
11[cantautor, spanish indie pop, spanish modern ...https://api.spotify.com/v1/artists/61C3cEhdoJ9...61C3cEhdoJ9YiQSQSwYB4K[{'height': 1000, 'url': 'https://i.scdn.co/im...Supersubmarina49artistspotify:artist:61C3cEhdoJ9YiQSQSwYB4Khttps://open.spotify.com/artist/61C3cEhdoJ9YiQ...None291467
12[tontipop]https://api.spotify.com/v1/artists/7wItEsGHPEa...7wItEsGHPEaFKnb1iJhbmW[{'height': 640, 'url': 'https://i.scdn.co/ima...Niña Polaca42artistspotify:artist:7wItEsGHPEaFKnb1iJhbmWhttps://open.spotify.com/artist/7wItEsGHPEaFKn...None15289
13[spanish indie pop, spanish modern rock]https://api.spotify.com/v1/artists/1uvhwixGrZk...1uvhwixGrZkuT2gpQQWgYb[{'height': 640, 'url': 'https://i.scdn.co/ima...Miss Caffeina46artistspotify:artist:1uvhwixGrZkuT2gpQQWgYbhttps://open.spotify.com/artist/1uvhwixGrZkuT2...None193569
14[spanish indie pop, spanish pop, spanish rock]https://api.spotify.com/v1/artists/2hazSY4Ef3a...2hazSY4Ef3aB9ATXW7F5w3[{'height': 640, 'url': 'https://i.scdn.co/ima...IZAL58artistspotify:artist:2hazSY4Ef3aB9ATXW7F5w3https://open.spotify.com/artist/2hazSY4Ef3aB9A...None588384
15[murcia indie, spanish indie pop]https://api.spotify.com/v1/artists/6kkQkBRrPcQ...6kkQkBRrPcQGUN8g2grxwb[{'height': 640, 'url': 'https://i.scdn.co/ima...Varry Brava41artistspotify:artist:6kkQkBRrPcQGUN8g2grxwbhttps://open.spotify.com/artist/6kkQkBRrPcQGUN...None54122
16[spanish indie pop, spanish indie rock, tontipop]https://api.spotify.com/v1/artists/5w3bs8jNvbb...5w3bs8jNvbb0QHf5Lo0sva[{'height': 640, 'url': 'https://i.scdn.co/ima...Ginebras46artistspotify:artist:5w3bs8jNvbb0QHf5Lo0svahttps://open.spotify.com/artist/5w3bs8jNvbb0QH...None53935
17[spanish indie pop, tontipop]https://api.spotify.com/v1/artists/5Q30fhTc0Sl...5Q30fhTc0Sl0Q4C5dOjhhQ[{'height': 640, 'url': 'https://i.scdn.co/ima...La La Love You56artistspotify:artist:5Q30fhTc0Sl0Q4C5dOjhhQhttps://open.spotify.com/artist/5Q30fhTc0Sl0Q4...None97075
18[spanish indie pop, spanish modern rock]https://api.spotify.com/v1/artists/2pMVWkucVS1...2pMVWkucVS130nJx59uAEa[{'height': 640, 'url': 'https://i.scdn.co/ima...Sidonie47artistspotify:artist:2pMVWkucVS130nJx59uAEahttps://open.spotify.com/artist/2pMVWkucVS130n...None178750
19[tontipop]https://api.spotify.com/v1/artists/6SShgjYwZQZ...6SShgjYwZQZ8Nx2wo5IKdd[{'height': 640, 'url': 'https://i.scdn.co/ima...Karavana41artistspotify:artist:6SShgjYwZQZ8Nx2wo5IKddhttps://open.spotify.com/artist/6SShgjYwZQZ8Nx...None18083
\n", + "
" + ], + "text/plain": [ + " genres \\\n", + "0 [murcia indie, rock alternativo espanol, spani... \n", + "1 [rock alternativo espanol, spanish indie pop, ... \n", + "2 [spanish indie pop, spanish modern rock, tonti... \n", + "3 [spanish modern rock] \n", + "4 [spanish indie pop] \n", + "5 [spanish indie pop, spanish modern rock] \n", + "6 [murcia indie, spanish indie pop, spanish mode... \n", + "7 [] \n", + "8 [spanish indie pop] \n", + "9 [granada indie, spanish indie pop] \n", + "10 [spanish indie pop, spanish rock] \n", + "11 [cantautor, spanish indie pop, spanish modern ... \n", + "12 [tontipop] \n", + "13 [spanish indie pop, spanish modern rock] \n", + "14 [spanish indie pop, spanish pop, spanish rock] \n", + "15 [murcia indie, spanish indie pop] \n", + "16 [spanish indie pop, spanish indie rock, tontipop] \n", + "17 [spanish indie pop, tontipop] \n", + "18 [spanish indie pop, spanish modern rock] \n", + "19 [tontipop] \n", + "\n", + " href id \\\n", + "0 https://api.spotify.com/v1/artists/57s0ep3eNSg... 57s0ep3eNSg81D7ZxiuHbC \n", + "1 https://api.spotify.com/v1/artists/6rRTrEHzGSD... 6rRTrEHzGSDqhmFJQrNFMO \n", + "2 https://api.spotify.com/v1/artists/2k60o7SVpOK... 2k60o7SVpOKmzpuTEAG3L8 \n", + "3 https://api.spotify.com/v1/artists/3wcfgYTtdxm... 3wcfgYTtdxmXrhuUuq3kEe \n", + "4 https://api.spotify.com/v1/artists/5q3atpVIFpX... 5q3atpVIFpXkHlcOKyK0H4 \n", + "5 https://api.spotify.com/v1/artists/7JrMDzLZQ25... 7JrMDzLZQ25BJwc4tD8jl8 \n", + "6 https://api.spotify.com/v1/artists/3Ju3bW8OaeU... 3Ju3bW8OaeUmfQKqMtATKr \n", + "7 https://api.spotify.com/v1/artists/4H3BAAfpEcL... 4H3BAAfpEcLHQkaQKfvoW8 \n", + "8 https://api.spotify.com/v1/artists/2FN1YrHU65d... 2FN1YrHU65dhPZKl3GD7Vd \n", + "9 https://api.spotify.com/v1/artists/3mOsjj1Mhoc... 3mOsjj1MhocRVwOejIZlTi \n", + "10 https://api.spotify.com/v1/artists/1vBn5Puz4md... 1vBn5Puz4mdZopZEHq1QDq \n", + "11 https://api.spotify.com/v1/artists/61C3cEhdoJ9... 61C3cEhdoJ9YiQSQSwYB4K \n", + "12 https://api.spotify.com/v1/artists/7wItEsGHPEa... 7wItEsGHPEaFKnb1iJhbmW \n", + "13 https://api.spotify.com/v1/artists/1uvhwixGrZk... 1uvhwixGrZkuT2gpQQWgYb \n", + "14 https://api.spotify.com/v1/artists/2hazSY4Ef3a... 2hazSY4Ef3aB9ATXW7F5w3 \n", + "15 https://api.spotify.com/v1/artists/6kkQkBRrPcQ... 6kkQkBRrPcQGUN8g2grxwb \n", + "16 https://api.spotify.com/v1/artists/5w3bs8jNvbb... 5w3bs8jNvbb0QHf5Lo0sva \n", + "17 https://api.spotify.com/v1/artists/5Q30fhTc0Sl... 5Q30fhTc0Sl0Q4C5dOjhhQ \n", + "18 https://api.spotify.com/v1/artists/2pMVWkucVS1... 2pMVWkucVS130nJx59uAEa \n", + "19 https://api.spotify.com/v1/artists/6SShgjYwZQZ... 6SShgjYwZQZ8Nx2wo5IKdd \n", + "\n", + " images \\\n", + "0 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "1 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "2 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "3 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "4 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "5 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "6 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "7 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "8 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "9 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "10 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "11 [{'height': 1000, 'url': 'https://i.scdn.co/im... \n", + "12 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "13 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "14 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "15 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "16 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "17 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "18 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "19 [{'height': 640, 'url': 'https://i.scdn.co/ima... \n", + "\n", + " name popularity type \\\n", + "0 Viva Suecia 56 artist \n", + "1 Shinova 49 artist \n", + "2 Veintiuno 47 artist \n", + "3 Carmen 113 37 artist \n", + "4 Siloé 47 artist \n", + "5 ELYELLA 46 artist \n", + "6 Second 45 artist \n", + "7 Mikel Izal 51 artist \n", + "8 Sexy Zebras 40 artist \n", + "9 Lori Meyers 51 artist \n", + "10 La Maravillosa Orquesta del Alcohol 56 artist \n", + "11 Supersubmarina 49 artist \n", + "12 Niña Polaca 42 artist \n", + "13 Miss Caffeina 46 artist \n", + "14 IZAL 58 artist \n", + "15 Varry Brava 41 artist \n", + "16 Ginebras 46 artist \n", + "17 La La Love You 56 artist \n", + "18 Sidonie 47 artist \n", + "19 Karavana 41 artist \n", + "\n", + " uri \\\n", + "0 spotify:artist:57s0ep3eNSg81D7ZxiuHbC \n", + "1 spotify:artist:6rRTrEHzGSDqhmFJQrNFMO \n", + "2 spotify:artist:2k60o7SVpOKmzpuTEAG3L8 \n", + "3 spotify:artist:3wcfgYTtdxmXrhuUuq3kEe \n", + "4 spotify:artist:5q3atpVIFpXkHlcOKyK0H4 \n", + "5 spotify:artist:7JrMDzLZQ25BJwc4tD8jl8 \n", + "6 spotify:artist:3Ju3bW8OaeUmfQKqMtATKr \n", + "7 spotify:artist:4H3BAAfpEcLHQkaQKfvoW8 \n", + "8 spotify:artist:2FN1YrHU65dhPZKl3GD7Vd \n", + "9 spotify:artist:3mOsjj1MhocRVwOejIZlTi \n", + "10 spotify:artist:1vBn5Puz4mdZopZEHq1QDq \n", + "11 spotify:artist:61C3cEhdoJ9YiQSQSwYB4K \n", + "12 spotify:artist:7wItEsGHPEaFKnb1iJhbmW \n", + "13 spotify:artist:1uvhwixGrZkuT2gpQQWgYb \n", + "14 spotify:artist:2hazSY4Ef3aB9ATXW7F5w3 \n", + "15 spotify:artist:6kkQkBRrPcQGUN8g2grxwb \n", + "16 spotify:artist:5w3bs8jNvbb0QHf5Lo0sva \n", + "17 spotify:artist:5Q30fhTc0Sl0Q4C5dOjhhQ \n", + "18 spotify:artist:2pMVWkucVS130nJx59uAEa \n", + "19 spotify:artist:6SShgjYwZQZ8Nx2wo5IKdd \n", + "\n", + " external_urls.spotify followers.href \\\n", + "0 https://open.spotify.com/artist/57s0ep3eNSg81D... None \n", + "1 https://open.spotify.com/artist/6rRTrEHzGSDqhm... None \n", + "2 https://open.spotify.com/artist/2k60o7SVpOKmzp... None \n", + "3 https://open.spotify.com/artist/3wcfgYTtdxmXrh... None \n", + "4 https://open.spotify.com/artist/5q3atpVIFpXkHl... None \n", + "5 https://open.spotify.com/artist/7JrMDzLZQ25BJw... None \n", + "6 https://open.spotify.com/artist/3Ju3bW8OaeUmfQ... None \n", + "7 https://open.spotify.com/artist/4H3BAAfpEcLHQk... None \n", + "8 https://open.spotify.com/artist/2FN1YrHU65dhPZ... None \n", + "9 https://open.spotify.com/artist/3mOsjj1MhocRVw... None \n", + "10 https://open.spotify.com/artist/1vBn5Puz4mdZop... None \n", + "11 https://open.spotify.com/artist/61C3cEhdoJ9YiQ... None \n", + "12 https://open.spotify.com/artist/7wItEsGHPEaFKn... None \n", + "13 https://open.spotify.com/artist/1uvhwixGrZkuT2... None \n", + "14 https://open.spotify.com/artist/2hazSY4Ef3aB9A... None \n", + "15 https://open.spotify.com/artist/6kkQkBRrPcQGUN... None \n", + "16 https://open.spotify.com/artist/5w3bs8jNvbb0QH... None \n", + "17 https://open.spotify.com/artist/5Q30fhTc0Sl0Q4... None \n", + "18 https://open.spotify.com/artist/2pMVWkucVS130n... None \n", + "19 https://open.spotify.com/artist/6SShgjYwZQZ8Nx... None \n", + "\n", + " followers.total \n", + "0 112431 \n", + "1 53126 \n", + "2 28290 \n", + "3 5172 \n", + "4 32821 \n", + "5 28997 \n", + "6 92945 \n", + "7 31895 \n", + "8 28051 \n", + "9 274606 \n", + "10 221054 \n", + "11 291467 \n", + "12 15289 \n", + "13 193569 \n", + "14 588384 \n", + "15 54122 \n", + "16 53935 \n", + "17 97075 \n", + "18 178750 \n", + "19 18083 " + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "related_df = pd.DataFrame(pd.json_normalize(related['artists']))\n", + "related_df" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "2cb96c04", + "metadata": {}, + "outputs": [], + "source": [ + "new_endpoint= 'https://api.spotify.com/v1/artists/'\n", + "argument= '?market=ES'\n", + "\n", + "top_tracks = pd.concat([pd.DataFrame(requests.get(new_endpoint + artist + '/top-tracks' + argument, headers=header_info).json()['tracks'])for artist in related_df['id']])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "id": "a04dc38b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 6IuZFiKCeyQINqL0Id3xF2\n", + "1 6PLIfuZYh2IrRNqJIEH26S\n", + "2 0olbdwIJ3oSRERKdlmky4r\n", + "3 1C2aofmIOndmXAtrHeJ3YC\n", + "4 23EYaP0VXowC6oT9DiY3QR\n", + " ... \n", + "5 4sacahljPfSxkQNHPt0HT0\n", + "6 1siNUKYwbKIun0Yhy6dSwd\n", + "7 59mdTFpz1XcfhKVT3OiDCS\n", + "8 2L7aGGeiirg7A2c0yRR8Ns\n", + "9 16ktCjV9gie8ux2JrBu7iS\n", + "Name: id, Length: 200, dtype: object" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_tracks['id']" ] }, { @@ -182,16 +749,353 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 148, "id": "2e5f0905", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 148, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Bonus\n", + "# granting permissions\n", + "\n", + "def generate_random_string(length):\n", + " characters = string.ascii_letters + string.digits\n", + " return ''.join(random.choice(characters) for _ in range(length))\n", + "\n", + "def login():\n", + " state = generate_random_string(16)\n", + " scope = 'playlist-read-private playlist-read-collaborative playlist-modify-private playlist-modify-public'\n", + " client_id = '1b8cd0572b464b8987f87cc3e4c992e5' # Replace with your actual client ID\n", + " redirect_uri = 'https://example.com/callback'\n", + "\n", + " authorization_url = 'https://accounts.spotify.com/authorize'\n", + " params = {\n", + " 'response_type': 'code',\n", + " 'client_id': client_id,\n", + " 'scope': scope,\n", + " 'redirect_uri': redirect_uri,\n", + " 'state': state\n", + " }\n", + "\n", + " authorization_url_with_params = f\"{authorization_url}?{'&'.join([f'{key}={value}' for key, value in params.items()])}\"\n", + " return requests.get(authorization_url_with_params)\n", + "\n", + "auth_res = login()\n", + "\n", + "\n", + "webbrowser.open(auth_res.url)" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "8d4218b7", + "metadata": {}, + "outputs": [], + "source": [ + "url = 'https://example.com/callback?code=https://example.com/callback?code=AQAVvyDgpT2w1GwMABedjC75Sd2UJs3bCZRe6SjKuiLfgHtq9SQ6mpv-5Hqb9uL4QskKccuBQozqoLWc9HhYhgOzPsi_3pN2cpJFpFxNt39JVsOPHQg-98gGhgqgmZChtBsJuBqVES1JPYW9eYUy5UoZ8j-ev0VI5l8k-o-uGqk9ABbpjSsyrM-RhLwNis1pinDMKHFuC6sJGhyDcR-iwUN1AOcuTdm2C1NBsqAdW2TTHGge68gvt_VqmHnZCPn5vcCurWXhLot4NQ9xqBDNIFXXtV5nR78rYFD95DSYwTh6ePNxPw&state=gGqVja50MMklpaHc-_eK1UtjXPJyeQxjl_sQ-SwTZaF0txfqBaQ&state=bSCPsLTi7HItyicy'\n", + "code = url.replace('https://example.com/callback?code=','').split('&state')[0]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "fbb658c9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'AQAVvyDgpT2w1GwMABedjC75Sd2UJs3bCZRe6SjKuiLfgHtq9SQ6mpv-5Hqb9uL4QskKccuBQozqoLWc9HhYhgOzPsi_3pN2cpJFpFxNt39JVsOPHQg-98gGhgqgmZChtBsJuBqVES1JPYW9eYUy5UoZ8j-ev0VI5l8k-o-uGqk9ABbpjSsyrM-RhLwNis1pinDMKHFuC6sJGhyDcR-iwUN1AOcuTdm2C1NBsqAdW2TTHGge68gvt_VqmHnZCPn5vcCurWXhLot4NQ9xqBDNIFXXtV5nR78rYFD95DSYwTh6ePNxPw'" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "code" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "32171caf", + "metadata": {}, + "outputs": [], + "source": [ + "import base64\n", + "\n", + "def callback():\n", + " redirect_uri='https://example.com/callback'\n", + " auth_options = {\n", + " 'url': 'https://accounts.spotify.com/api/token',\n", + " 'data': {\n", + " 'code': code,\n", + " 'redirect_uri': redirect_uri,\n", + " 'grant_type': 'authorization_code'\n", + " },\n", + " 'headers': {\n", + " 'Content-Type': 'application/x-www-form-urlencoded',\n", + " 'Authorization': 'Basic ' + base64.b64encode(f'{client_id}:{client_secret}'.encode()).decode()\n", + " }\n", + " }\n", + "\n", + " response = requests.post(auth_options['url'], data=auth_options['data'], headers=auth_options['headers'])\n", + " return response.json()\n", + "\n", + "token_res = callback()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "id": "5c5bd898", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'BQA1ODj804-rBAYMFJSkKhIAZjA7aSJg74kDrfG9aIkAiUTw3vGJdcEhmFsvjarnZLlrqSPZLIQkju7pPOc8nbFTtqxLdPTjaNHb-M0UJbb-UayuGRaB7kgD_1Y863p-aAP-VqkeMHeGVOt7kmnqTEZZriYGvrIyFN6zYzcOkVq91lOoX0VTqBKjE5KjR0spgCp7U13_Ia4pEJDxd9t2zIgFrRyIU7wnWLT05wMmUMqQZUD9I5_xiTmx3g'" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "access_token2 = token_res['access_token']\n", + "access_token2" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "fa1fd643", + "metadata": {}, + "outputs": [], + "source": [ + "playlist_name = 'La otra de Victor'\n", + "playlist_description = 'Por el camino duro' \n", + "\n", + "headers = {\n", + " 'Authorization': f'Bearer {access_token2}',\n", + " 'Content-Type': 'application/json'\n", + "}\n", + "\n", + "playlist_data = {\n", + " 'name': playlist_name,\n", + " 'public': True, \n", + " 'description': playlist_description\n", + "}\n", + "\n", + "create_playlist_url = f'https://api.spotify.com/v1/users/rjcolgut/playlists'\n", + "response = requests.post(create_playlist_url, json=playlist_data, headers=headers)\n", + "playlist_data = response.json()\n", + "\n", + "\n", "\n", "\n" ] }, + { + "cell_type": "code", + "execution_count": 103, + "id": "0a157123", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'collaborative': False,\n", + " 'description': 'Por el camino duro',\n", + " 'external_urls': {'spotify': 'https://open.spotify.com/playlist/1Zvw5jXRhGuQEnStNacSXh'},\n", + " 'followers': {'href': None, 'total': 0},\n", + " 'href': 'https://api.spotify.com/v1/playlists/1Zvw5jXRhGuQEnStNacSXh',\n", + " 'id': '1Zvw5jXRhGuQEnStNacSXh',\n", + " 'images': [],\n", + " 'name': 'La otra de Victor',\n", + " 'owner': {'display_name': 'rjcolgut',\n", + " 'external_urls': {'spotify': 'https://open.spotify.com/user/rjcolgut'},\n", + " 'href': 'https://api.spotify.com/v1/users/rjcolgut',\n", + " 'id': 'rjcolgut',\n", + " 'type': 'user',\n", + " 'uri': 'spotify:user:rjcolgut'},\n", + " 'primary_color': None,\n", + " 'public': True,\n", + " 'snapshot_id': 'MSw4Njg4Nzk1NmY0Yzc1M2VjM2RjOGYyOTJjYTY2YTRmZmQ0ZGM1NDdh',\n", + " 'tracks': {'href': 'https://api.spotify.com/v1/playlists/1Zvw5jXRhGuQEnStNacSXh/tracks',\n", + " 'items': [],\n", + " 'limit': 100,\n", + " 'next': None,\n", + " 'offset': 0,\n", + " 'previous': None,\n", + " 'total': 0},\n", + " 'type': 'playlist',\n", + " 'uri': 'spotify:playlist:1Zvw5jXRhGuQEnStNacSXh'}" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "playlist_data[]" + ] + }, + { + "cell_type": "code", + "execution_count": 134, + "id": "d45c0fc0", + "metadata": {}, + "outputs": [], + "source": [ + "playlist_id= '1Zvw5jXRhGuQEnStNacSXh'\n", + "uris = '?uris=' + ','.join(list(top_tracks['id'])[:99])\n", + "\n", + "headers = {\n", + " 'Authorization': f'Bearer {access_token2}',\n", + " 'Content-Type': 'application/json'\n", + "}\n", + "\n", + "add_tracks_data = {\n", + " 'uris': list(top_tracks['uri'])[:99]\n", + "}\n", + "modify_playlist_url = f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks'\n", + "response = requests.post(modify_playlist_url, headers=headers, json=add_tracks_data)\n", + "playlist_data = response.json() " + ] + }, + { + "cell_type": "code", + "execution_count": 135, + "id": "0384c43f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'snapshot_id': 'MywxOGJmMWYyZjkxNTA3ZDgyYTU4ODM4NTUwYTA3Y2VkZDI3MTg3Nzhh'}" + ] + }, + "execution_count": 135, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "playlist_data" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "63118550", + "metadata": {}, + "outputs": [], + "source": [ + "import spotipy\n", + "from spotipy.oauth2 import SpotifyOAuth\n", + "\n", + "sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id= client_id,\n", + " client_secret=client_secret,\n", + " redirect_uri='https://example.com/callback',\n", + " scope='playlist-read-private playlist-read-collaborative playlist-modify-private playlist-modify-public'))\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "abfc9fe5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'collaborative': False,\n", + " 'description': 'La mejor playlist del mundo',\n", + " 'external_urls': {'spotify': 'https://open.spotify.com/playlist/3E2KsluXD3ljsAi3tcYZll'},\n", + " 'followers': {'href': None, 'total': 0},\n", + " 'href': 'https://api.spotify.com/v1/playlists/3E2KsluXD3ljsAi3tcYZll',\n", + " 'id': '3E2KsluXD3ljsAi3tcYZll',\n", + " 'images': [],\n", + " 'name': 'la_de_victor',\n", + " 'owner': {'display_name': 'rjcolgut',\n", + " 'external_urls': {'spotify': 'https://open.spotify.com/user/rjcolgut'},\n", + " 'href': 'https://api.spotify.com/v1/users/rjcolgut',\n", + " 'id': 'rjcolgut',\n", + " 'type': 'user',\n", + " 'uri': 'spotify:user:rjcolgut'},\n", + " 'primary_color': None,\n", + " 'public': True,\n", + " 'snapshot_id': 'MSxkMmMzY2UyZTI0ZjU4N2YyYWFlMTFlOGUxYWFlMjkwNTkxNGJkMmUw',\n", + " 'tracks': {'href': 'https://api.spotify.com/v1/playlists/3E2KsluXD3ljsAi3tcYZll/tracks',\n", + " 'items': [],\n", + " 'limit': 100,\n", + " 'next': None,\n", + " 'offset': 0,\n", + " 'previous': None,\n", + " 'total': 0},\n", + " 'type': 'playlist',\n", + " 'uri': 'spotify:playlist:3E2KsluXD3ljsAi3tcYZll'}" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sp.user_playlist_create(user='rjcolgut', name='la_de_victor', public=True, description='La mejor playlist del mundo')\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "0f680313", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'snapshot_id': 'Myw3YWVkOWE4MGZlNWYxOWFiNDk4ZTliZTg1NmM5MTMzYmUyM2NkYzRj'}" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sp.playlist_add_items(playlist_id='3E2KsluXD3ljsAi3tcYZll', items=list(top_tracks['uri'])[:99])" + ] + }, + { + "cell_type": "markdown", + "id": "d1767287", + "metadata": {}, + "source": [] + }, + { + "cell_type": "markdown", + "id": "8947e8a4", + "metadata": {}, + "source": [] + }, { "cell_type": "markdown", "id": "9436b623", @@ -221,9 +1125,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python [conda env:.conda-ironhack]", + "display_name": "m1_env", "language": "python", - "name": "conda-env-.conda-ironhack-py" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -235,7 +1139,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.11" + "version": "3.10.13" } }, "nbformat": 4,