diff --git a/notebooks/spotify_rest_api_challenge.ipynb b/notebooks/spotify_rest_api_challenge.ipynb index 5e4f8f7..16268fb 100644 --- a/notebooks/spotify_rest_api_challenge.ipynb +++ b/notebooks/spotify_rest_api_challenge.ipynb @@ -16,13 +16,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "17a49776", "metadata": {}, "outputs": [], "source": [ "# Imports\n", - "\n", + "import requests\n", + "import pandas as pd \n", "\n" ] }, @@ -38,21 +39,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "7b5d2e52", "metadata": {}, "outputs": [], "source": [ "# Generate token with a POST request\n", "\n", - "client_id = # CLIENT ID\n", - "client_secret = # CLIENT SECRET\n", - "auth_url = 'https://accounts.spotify.com/api/token'" + "#Note that I remove ID details for security reasons" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "c7901d1e", "metadata": {}, "outputs": [], @@ -68,10 +67,7 @@ "id": "262e7e18", "metadata": {}, "outputs": [], - "source": [ - "access_token = auth_response['access_token']\n", - "auth_response" - ] + "source": [] }, { "cell_type": "markdown", @@ -85,7 +81,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "62e6d4fb", "metadata": {}, "outputs": [], @@ -143,27 +139,777 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "04f85940", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'https://api.spotify.com/v1/artists/0WwSkZ7LtFUFjGjMZBMt6T/related-artists'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Here you can complete your endpoint URI\n", - "\n", - "full_endpoint = \n", + "artist_id = \"0WwSkZ7LtFUFjGjMZBMt6T\"\n", + "method = \"/related-artists\"\n", + "full_endpoint = base_url + resource + artist_id + method\n", "full_endpoint" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "50ec3959", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "200" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Start building your playlist!!!\n", - "\n", - "\n" + "related_artist_response = requests.get(full_endpoint, headers = header_info)\n", + "related_artist_response.status_code\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "1a83ec5d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "b'{\\n \"artists\" : [ {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/0FI0kxP0BWurTz8cB8BBug\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 1481511\\n },\\n \"genres\" : [ \"soft rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/0FI0kxP0BWurTz8cB8BBug\",\\n \"id\" : \"0FI0kxP0BWurTz8cB8BBug\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5eb685c708602ec164b04e38e26\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab67616100005174685c708602ec164b04e38e26\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f178685c708602ec164b04e38e26\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"Mark Knopfler\",\\n \"popularity\" : 58,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:0FI0kxP0BWurTz8cB8BBug\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/3JsMj0DEzyWc0VDlHuy9Bx\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 3262406\\n },\\n \"genres\" : [ \"album rock\", \"art rock\", \"classic rock\", \"glam rock\", \"mellow gold\", \"piano rock\", \"progressive rock\", \"rock\", \"soft rock\", \"symphonic rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/3JsMj0DEzyWc0VDlHuy9Bx\",\\n \"id\" : \"3JsMj0DEzyWc0VDlHuy9Bx\",\\n \"images\" : [ {\\n \"height\" : 977,\\n \"url\" : \"https://i.scdn.co/image/eebed895cad509b17c7c6faf47f21f1fc35e8829\",\\n \"width\" : 1000\\n }, {\\n \"height\" : 625,\\n \"url\" : \"https://i.scdn.co/image/b0183fa5aa1cc1f8972bf49e2fc0ecce6bdb184d\",\\n \"width\" : 640\\n }, {\\n \"height\" : 195,\\n \"url\" : \"https://i.scdn.co/image/52dfb5d8e545c42dfe97cbb2f9ebe825799c60b1\",\\n \"width\" : 200\\n }, {\\n \"height\" : 63,\\n \"url\" : \"https://i.scdn.co/image/c835c0c90dde46223c23ed9a01b9671e174de708\",\\n \"width\" : 64\\n } ],\\n \"name\" : \"Supertramp\",\\n \"popularity\" : 68,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:3JsMj0DEzyWc0VDlHuy9Bx\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/3pFCERyEiP5xeN2EsPXhjI\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 2209756\\n },\\n \"genres\" : [ \"album rock\", \"blues rock\", \"classic rock\", \"folk rock\", \"heartland rock\", \"mellow gold\", \"rock\", \"singer-songwriter\", \"soft rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/3pFCERyEiP5xeN2EsPXhjI\",\\n \"id\" : \"3pFCERyEiP5xeN2EsPXhjI\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5eb97996e2b05450ffbeafe15da\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000517497996e2b05450ffbeafe15da\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f17897996e2b05450ffbeafe15da\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"Joe Cocker\",\\n \"popularity\" : 62,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:3pFCERyEiP5xeN2EsPXhjI\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/5KEG7G8LDYlHgFDqZyEEs2\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 956442\\n },\\n \"genres\" : [ \"soft rock\", \"sophisti-pop\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/5KEG7G8LDYlHgFDqZyEEs2\",\\n \"id\" : \"5KEG7G8LDYlHgFDqZyEEs2\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5eb81aacad025d8338d125debd0\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000517481aacad025d8338d125debd0\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f17881aacad025d8338d125debd0\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"Chris Rea\",\\n \"popularity\" : 58,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:5KEG7G8LDYlHgFDqZyEEs2\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/5NGO30tJxFlKixkPSgXcFE\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 5716188\\n },\\n \"genres\" : [ \"album rock\", \"classic rock\", \"permanent wave\", \"rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/5NGO30tJxFlKixkPSgXcFE\",\\n \"id\" : \"5NGO30tJxFlKixkPSgXcFE\",\\n \"images\" : [ {\\n \"height\" : 641,\\n \"url\" : \"https://i.scdn.co/image/1f73a61faca2942cd5ea29c2143184db8645f0b3\",\\n \"width\" : 1000\\n }, {\\n \"height\" : 410,\\n \"url\" : \"https://i.scdn.co/image/72fe2cadb69ab59960ae3dbd29618549e4432699\",\\n \"width\" : 640\\n }, {\\n \"height\" : 128,\\n \"url\" : \"https://i.scdn.co/image/078c3d6784e53ca7490fb63218560d20100a18f2\",\\n \"width\" : 200\\n }, {\\n \"height\" : 41,\\n \"url\" : \"https://i.scdn.co/image/e7a6ecc704516f1bdc3ff67e96fb51435ebeebbb\",\\n \"width\" : 64\\n } ],\\n \"name\" : \"The Police\",\\n \"popularity\" : 74,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:5NGO30tJxFlKixkPSgXcFE\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/3CkvROUTQ6nRi9yQOcsB50\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 3162853\\n },\\n \"genres\" : [ \"album rock\", \"art rock\", \"classic rock\", \"hard rock\", \"mellow gold\", \"progressive rock\", \"rock\", \"soft rock\", \"symphonic rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/3CkvROUTQ6nRi9yQOcsB50\",\\n \"id\" : \"3CkvROUTQ6nRi9yQOcsB50\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5ebdfb2b3c69199bc2dfbf3e5b9\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab67616100005174dfb2b3c69199bc2dfbf3e5b9\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f178dfb2b3c69199bc2dfbf3e5b9\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"Genesis\",\\n \"popularity\" : 65,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:3CkvROUTQ6nRi9yQOcsB50\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/6PAt558ZEZl0DmdXlnjMgD\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 5586702\\n },\\n \"genres\" : [ \"album rock\", \"blues rock\", \"classic rock\", \"electric blues\", \"mellow gold\", \"rock\", \"singer-songwriter\", \"soft rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/6PAt558ZEZl0DmdXlnjMgD\",\\n \"id\" : \"6PAt558ZEZl0DmdXlnjMgD\",\\n \"images\" : [ {\\n \"height\" : 1000,\\n \"url\" : \"https://i.scdn.co/image/ab6772690000c46ca60e8f215103f6841d8a83f0\",\\n \"width\" : 1000\\n }, {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6772690000dd22a60e8f215103f6841d8a83f0\",\\n \"width\" : 640\\n }, {\\n \"height\" : 200,\\n \"url\" : \"https://i.scdn.co/image/ab6772690000bac3a60e8f215103f6841d8a83f0\",\\n \"width\" : 200\\n }, {\\n \"height\" : 64,\\n \"url\" : \"https://i.scdn.co/image/ab67726900008f74a60e8f215103f6841d8a83f0\",\\n \"width\" : 64\\n } ],\\n \"name\" : \"Eric Clapton\",\\n \"popularity\" : 68,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:6PAt558ZEZl0DmdXlnjMgD\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/3eqjTLE0HfPfh78zjh6TqT\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 5930110\\n },\\n \"genres\" : [ \"heartland rock\", \"mellow gold\", \"permanent wave\", \"rock\", \"singer-songwriter\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/3eqjTLE0HfPfh78zjh6TqT\",\\n \"id\" : \"3eqjTLE0HfPfh78zjh6TqT\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5ebf7cac48c6b587b927ba73f4e\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab67616100005174f7cac48c6b587b927ba73f4e\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f178f7cac48c6b587b927ba73f4e\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"Bruce Springsteen\",\\n \"popularity\" : 72,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:3eqjTLE0HfPfh78zjh6TqT\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/4KWTAlx2RvbpseOGMEmROg\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 4246695\\n },\\n \"genres\" : [ \"alternative rock\", \"athens indie\", \"permanent wave\", \"rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/4KWTAlx2RvbpseOGMEmROg\",\\n \"id\" : \"4KWTAlx2RvbpseOGMEmROg\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5eb6334ab6a83196f36475ada7f\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab676161000051746334ab6a83196f36475ada7f\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f1786334ab6a83196f36475ada7f\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"R.E.M.\",\\n \"popularity\" : 71,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:4KWTAlx2RvbpseOGMEmROg\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/568ZhdwyaiCyOGJRtNYhWf\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 5252682\\n },\\n \"genres\" : [ \"album rock\", \"classic rock\", \"hard rock\", \"metal\", \"rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/568ZhdwyaiCyOGJRtNYhWf\",\\n \"id\" : \"568ZhdwyaiCyOGJRtNYhWf\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5eb607e7523c28dd40288a08df4\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab67616100005174607e7523c28dd40288a08df4\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f178607e7523c28dd40288a08df4\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"Deep Purple\",\\n \"popularity\" : 63,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:568ZhdwyaiCyOGJRtNYhWf\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/2m62cc253Xvd9qYQ8d2X3d\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 1387870\\n },\\n \"genres\" : [ \"album rock\", \"art rock\", \"classic rock\", \"mellow gold\", \"progressive rock\", \"soft rock\", \"symphonic rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/2m62cc253Xvd9qYQ8d2X3d\",\\n \"id\" : \"2m62cc253Xvd9qYQ8d2X3d\",\\n \"images\" : [ {\\n \"height\" : 1028,\\n \"url\" : \"https://i.scdn.co/image/3e90d18b7375a0e78340d23f72822646c4cc3b70\",\\n \"width\" : 1000\\n }, {\\n \"height\" : 658,\\n \"url\" : \"https://i.scdn.co/image/112b078d34b51f977720805963334611383ee1c1\",\\n \"width\" : 640\\n }, {\\n \"height\" : 206,\\n \"url\" : \"https://i.scdn.co/image/63e9f6ec84b77cd5d8631c7d6e9c2f3273693254\",\\n \"width\" : 200\\n }, {\\n \"height\" : 66,\\n \"url\" : \"https://i.scdn.co/image/50beb6c019cacb758e07f102168add254b0acd18\",\\n \"width\" : 64\\n } ],\\n \"name\" : \"The Alan Parsons Project\",\\n \"popularity\" : 60,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:2m62cc253Xvd9qYQ8d2X3d\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/6hN9F0iuULZYWXppob22Aj\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 1888397\\n },\\n \"genres\" : [ \"album rock\", \"dance rock\", \"new romantic\", \"new wave\", \"new wave pop\", \"rock\", \"scottish new wave\", \"soft rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/6hN9F0iuULZYWXppob22Aj\",\\n \"id\" : \"6hN9F0iuULZYWXppob22Aj\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5eb10ed5b26df1c762604436c92\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000517410ed5b26df1c762604436c92\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f17810ed5b26df1c762604436c92\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"Simple Minds\",\\n \"popularity\" : 64,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:6hN9F0iuULZYWXppob22Aj\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/2FcC4sDMXme2ziI7tGKMK8\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 1802581\\n },\\n \"genres\" : [ \"album rock\", \"art rock\", \"classic rock\", \"progressive rock\", \"symphonic rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/2FcC4sDMXme2ziI7tGKMK8\",\\n \"id\" : \"2FcC4sDMXme2ziI7tGKMK8\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5eb1dfc5a9665cd11bc0dd026fc\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab676161000051741dfc5a9665cd11bc0dd026fc\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f1781dfc5a9665cd11bc0dd026fc\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"David Gilmour\",\\n \"popularity\" : 50,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:2FcC4sDMXme2ziI7tGKMK8\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/7C4sUpWGlTy7IANjruj02I\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 1616027\\n },\\n \"genres\" : [ \"album rock\", \"art rock\", \"classic rock\", \"mellow gold\", \"new romantic\", \"new wave\", \"permanent wave\", \"progressive rock\", \"rock\", \"singer-songwriter\", \"soft rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/7C4sUpWGlTy7IANjruj02I\",\\n \"id\" : \"7C4sUpWGlTy7IANjruj02I\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5eb997116abcd93201746831544\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab67616100005174997116abcd93201746831544\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f178997116abcd93201746831544\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"Peter Gabriel\",\\n \"popularity\" : 62,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:7C4sUpWGlTy7IANjruj02I\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/6w6z8m4WXX7Tub4Rb6Lu7R\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 1802426\\n },\\n \"genres\" : [ \"album rock\", \"art rock\", \"blues rock\", \"classic rock\", \"flute rock\", \"folk rock\", \"hard rock\", \"progressive rock\", \"psychedelic rock\", \"rock\", \"singer-songwriter\", \"symphonic rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/6w6z8m4WXX7Tub4Rb6Lu7R\",\\n \"id\" : \"6w6z8m4WXX7Tub4Rb6Lu7R\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5ebb94631f08038eac322c090f0\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab67616100005174b94631f08038eac322c090f0\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f178b94631f08038eac322c090f0\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"Jethro Tull\",\\n \"popularity\" : 57,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:6w6z8m4WXX7Tub4Rb6Lu7R\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/0k17h0D3J5VfsdmQ1iZtE9\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 19762421\\n },\\n \"genres\" : [ \"album rock\", \"art rock\", \"classic rock\", \"progressive rock\", \"psychedelic rock\", \"rock\", \"symphonic rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/0k17h0D3J5VfsdmQ1iZtE9\",\\n \"id\" : \"0k17h0D3J5VfsdmQ1iZtE9\",\\n \"images\" : [ {\\n \"height\" : 977,\\n \"url\" : \"https://i.scdn.co/image/e69f71e2be4b67b82af90fb8e9d805715e0684fa\",\\n \"width\" : 1000\\n }, {\\n \"height\" : 625,\\n \"url\" : \"https://i.scdn.co/image/d011c95081cd9a329e506abd7ded47535d524a07\",\\n \"width\" : 640\\n }, {\\n \"height\" : 195,\\n \"url\" : \"https://i.scdn.co/image/f0a39a8a196a87a7236bdcf8a8708f6d5d3547cc\",\\n \"width\" : 200\\n }, {\\n \"height\" : 63,\\n \"url\" : \"https://i.scdn.co/image/ec1fb7127168dbaa962404031409c5a293b95ec6\",\\n \"width\" : 64\\n } ],\\n \"name\" : \"Pink Floyd\",\\n \"popularity\" : 76,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:0k17h0D3J5VfsdmQ1iZtE9\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/51Blml2LZPmy7TTiAg47vQ\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 11672271\\n },\\n \"genres\" : [ \"irish rock\", \"permanent wave\", \"rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/51Blml2LZPmy7TTiAg47vQ\",\\n \"id\" : \"51Blml2LZPmy7TTiAg47vQ\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5eb76e8d4043d65d5b90dd620c6\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000517476e8d4043d65d5b90dd620c6\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f17876e8d4043d65d5b90dd620c6\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"U2\",\\n \"popularity\" : 75,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:51Blml2LZPmy7TTiAg47vQ\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/67ea9eGLXYMsO2eYQRui3w\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 5216854\\n },\\n \"genres\" : [ \"album rock\", \"british invasion\", \"classic rock\", \"hard rock\", \"rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/67ea9eGLXYMsO2eYQRui3w\",\\n \"id\" : \"67ea9eGLXYMsO2eYQRui3w\",\\n \"images\" : [ {\\n \"height\" : 1000,\\n \"url\" : \"https://i.scdn.co/image/9cd709cabb4a614b4f1dd9ec256a5f30e21f0150\",\\n \"width\" : 1000\\n }, {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/e70c8cbc5eb682b35da8dbc90c5c3631f4df9e23\",\\n \"width\" : 640\\n }, {\\n \"height\" : 200,\\n \"url\" : \"https://i.scdn.co/image/4d7d0709054a22a2f67ecc139fec046b1f4cc880\",\\n \"width\" : 200\\n }, {\\n \"height\" : 64,\\n \"url\" : \"https://i.scdn.co/image/d2607544c5d6ad7a77571f704621a038a947243a\",\\n \"width\" : 64\\n } ],\\n \"name\" : \"The Who\",\\n \"popularity\" : 65,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:67ea9eGLXYMsO2eYQRui3w\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/4lxfqrEsLX6N1N4OCSkILp\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 5470636\\n },\\n \"genres\" : [ \"rock drums\", \"soft rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/4lxfqrEsLX6N1N4OCSkILp\",\\n \"id\" : \"4lxfqrEsLX6N1N4OCSkILp\",\\n \"images\" : [ {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000e5ebd4e38c4e3e5c82774740e28d\",\\n \"width\" : 640\\n }, {\\n \"height\" : 320,\\n \"url\" : \"https://i.scdn.co/image/ab67616100005174d4e38c4e3e5c82774740e28d\",\\n \"width\" : 320\\n }, {\\n \"height\" : 160,\\n \"url\" : \"https://i.scdn.co/image/ab6761610000f178d4e38c4e3e5c82774740e28d\",\\n \"width\" : 160\\n } ],\\n \"name\" : \"Phil Collins\",\\n \"popularity\" : 73,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:4lxfqrEsLX6N1N4OCSkILp\"\\n }, {\\n \"external_urls\" : {\\n \"spotify\" : \"https://open.spotify.com/artist/2AM4ilv6UzW0uMRuqKtDgN\"\\n },\\n \"followers\" : {\\n \"href\" : null,\\n \"total\" : 4152283\\n },\\n \"genres\" : [ \"album rock\", \"blues rock\", \"classic rock\", \"hard rock\", \"rock\" ],\\n \"href\" : \"https://api.spotify.com/v1/artists/2AM4ilv6UzW0uMRuqKtDgN\",\\n \"id\" : \"2AM4ilv6UzW0uMRuqKtDgN\",\\n \"images\" : [ {\\n \"height\" : 1000,\\n \"url\" : \"https://i.scdn.co/image/ab6772690000c46ca56aa897505a21aebecf7ea3\",\\n \"width\" : 1000\\n }, {\\n \"height\" : 640,\\n \"url\" : \"https://i.scdn.co/image/ab6772690000dd22a56aa897505a21aebecf7ea3\",\\n \"width\" : 640\\n }, {\\n \"height\" : 200,\\n \"url\" : \"https://i.scdn.co/image/ab6772690000bac3a56aa897505a21aebecf7ea3\",\\n \"width\" : 200\\n }, {\\n \"height\" : 64,\\n \"url\" : \"https://i.scdn.co/image/ab67726900008f74a56aa897505a21aebecf7ea3\",\\n \"width\" : 64\\n } ],\\n \"name\" : \"ZZ Top\",\\n \"popularity\" : 66,\\n \"type\" : \"artist\",\\n \"uri\" : \"spotify:artist:2AM4ilv6UzW0uMRuqKtDgN\"\\n } ]\\n}'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "related_artist_response.content" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "be37ccf8", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "related_artists_json = related_artist_response.json()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "9fe73c86", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Mark Knopfler',\n", + " 'Supertramp',\n", + " 'Joe Cocker',\n", + " 'Chris Rea',\n", + " 'The Police',\n", + " 'Genesis',\n", + " 'Eric Clapton',\n", + " 'Bruce Springsteen',\n", + " 'R.E.M.',\n", + " 'Deep Purple',\n", + " 'The Alan Parsons Project',\n", + " 'Simple Minds',\n", + " 'David Gilmour',\n", + " 'Peter Gabriel',\n", + " 'Jethro Tull',\n", + " 'Pink Floyd',\n", + " 'U2',\n", + " 'The Who',\n", + " 'Phil Collins',\n", + " 'ZZ Top']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[related_artists_json[\"artists\"][i][\"name\"] for i in range(len(related_artists_json[\"artists\"]))]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "bde836e1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'https://api.spotify.com/v1/artists/0WwSkZ7LtFUFjGjMZBMt6T/top-tracks?market=ES'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "artist_id = \"0WwSkZ7LtFUFjGjMZBMt6T\"\n", + "method = \"/top-tracks\"\n", + "argument = \"?market=ES\"\n", + "full_endpoint_tracks = base_url + resource + artist_id + method + argument\n", + "full_endpoint_tracks" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "4a523b5c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "200" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "related_artist_response_tracks = requests.get(full_endpoint_tracks, headers = header_info)\n", + "related_artist_response_tracks.status_code" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "61efdcf1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Sultans Of Swing'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "related_artist_response_tracks_json = related_artist_response_tracks.json()\n", + "related_artist_response_tracks_json[\"tracks\"][0][\"name\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "66ae104b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['0FI0kxP0BWurTz8cB8BBug',\n", + " '3JsMj0DEzyWc0VDlHuy9Bx',\n", + " '3pFCERyEiP5xeN2EsPXhjI',\n", + " '5KEG7G8LDYlHgFDqZyEEs2',\n", + " '5NGO30tJxFlKixkPSgXcFE',\n", + " '3CkvROUTQ6nRi9yQOcsB50',\n", + " '6PAt558ZEZl0DmdXlnjMgD',\n", + " '3eqjTLE0HfPfh78zjh6TqT',\n", + " '4KWTAlx2RvbpseOGMEmROg',\n", + " '568ZhdwyaiCyOGJRtNYhWf',\n", + " '2m62cc253Xvd9qYQ8d2X3d',\n", + " '6hN9F0iuULZYWXppob22Aj',\n", + " '2FcC4sDMXme2ziI7tGKMK8',\n", + " '7C4sUpWGlTy7IANjruj02I',\n", + " '6w6z8m4WXX7Tub4Rb6Lu7R',\n", + " '0k17h0D3J5VfsdmQ1iZtE9',\n", + " '51Blml2LZPmy7TTiAg47vQ',\n", + " '67ea9eGLXYMsO2eYQRui3w',\n", + " '4lxfqrEsLX6N1N4OCSkILp',\n", + " '2AM4ilv6UzW0uMRuqKtDgN']" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "related_artists_id = [related_artists_json[\"artists\"][i][\"id\"] for i in range(len(related_artists_json[\"artists\"]))]\n", + "related_artists_id" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "1352656a", + "metadata": {}, + "outputs": [], + "source": [ + "def get_top_tracks(id_artist):\n", + " resource = \"/artists\"\n", + " parameters = f\"/{id_artist}/top-tracks?market=ES\"\n", + " url = base_url + resource + parameters\n", + " response = requests.get(url, headers = header_info).json()\n", + " df_tracks = pd.DataFrame(response[\"tracks\"])[[\"id\", \"href\", \"name\", \"uri\"]]\n", + " df_tracks[\"related_id\"]= id_artist\n", + " return df_tracks" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "0bcd5726", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'tracks'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[21], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mget_top_tracks\u001b[49m\u001b[43m(\u001b[49m\u001b[43mid_artist\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m67ea9eGLXYMsO2eYQRui3w\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "Cell \u001b[0;32mIn[20], line 6\u001b[0m, in \u001b[0;36mget_top_tracks\u001b[0;34m(id_artist)\u001b[0m\n\u001b[1;32m 4\u001b[0m url \u001b[38;5;241m=\u001b[39m base_url \u001b[38;5;241m+\u001b[39m resource \u001b[38;5;241m+\u001b[39m parameters\n\u001b[1;32m 5\u001b[0m response \u001b[38;5;241m=\u001b[39m requests\u001b[38;5;241m.\u001b[39mget(url, headers \u001b[38;5;241m=\u001b[39m header_info)\u001b[38;5;241m.\u001b[39mjson()\n\u001b[0;32m----> 6\u001b[0m df_tracks \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mDataFrame(\u001b[43mresponse\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtracks\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m)[[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mid\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhref\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124muri\u001b[39m\u001b[38;5;124m\"\u001b[39m]]\n\u001b[1;32m 7\u001b[0m df_tracks[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrelated_id\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m=\u001b[39m id_artist\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m df_tracks\n", + "\u001b[0;31mKeyError\u001b[0m: 'tracks'" + ] + } + ], + "source": [ + "get_top_tracks(id_artist ='67ea9eGLXYMsO2eYQRui3w')" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "7bbd55ed", + "metadata": {}, + "outputs": [], + "source": [ + "id_selected ='568ZhdwyaiCyOGJRtNYhWf'" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "7bc32606", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "https://api.spotify.com/v1/artists/568ZhdwyaiCyOGJRtNYhWf\n" + ] + } + ], + "source": [ + "resource = \"artists\"\n", + "parameters = f'/{id_selected}'\n", + "url = base_url + resource + parameters\n", + "print (url)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "c2f2f7ad", + "metadata": {}, + "outputs": [], + "source": [ + "response = requests.get(url, headers = header_info).json()" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "4aaa82ee", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'external_urls': {'spotify': 'https://open.spotify.com/artist/568ZhdwyaiCyOGJRtNYhWf'},\n", + " 'followers': {'href': None, 'total': 5238333},\n", + " 'genres': ['album rock', 'classic rock', 'hard rock', 'metal', 'rock'],\n", + " 'href': 'https://api.spotify.com/v1/artists/568ZhdwyaiCyOGJRtNYhWf',\n", + " 'id': '568ZhdwyaiCyOGJRtNYhWf',\n", + " 'images': [{'height': 640,\n", + " 'url': 'https://i.scdn.co/image/ab6761610000e5eb607e7523c28dd40288a08df4',\n", + " 'width': 640},\n", + " {'height': 320,\n", + " 'url': 'https://i.scdn.co/image/ab67616100005174607e7523c28dd40288a08df4',\n", + " 'width': 320},\n", + " {'height': 160,\n", + " 'url': 'https://i.scdn.co/image/ab6761610000f178607e7523c28dd40288a08df4',\n", + " 'width': 160}],\n", + " 'name': 'Deep Purple',\n", + " 'popularity': 63,\n", + " 'type': 'artist',\n", + " 'uri': 'spotify:artist:568ZhdwyaiCyOGJRtNYhWf'}" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "response" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "2cb2f431", + "metadata": {}, + "outputs": [], + "source": [ + "dict_artist = dict((key, [response[key]])for key in [\"id\", \"name\", \"genres\", \"popularity\",\"followers\"] if key in response)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "69cdbbe5", + "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", + "
idnamegenrespopularityfollowers
0568ZhdwyaiCyOGJRtNYhWfDeep Purple[album rock, classic rock, hard rock, metal, r...63{'href': None, 'total': 5238333}
\n", + "
" + ], + "text/plain": [ + " id name \\\n", + "0 568ZhdwyaiCyOGJRtNYhWf Deep Purple \n", + "\n", + " genres popularity \\\n", + "0 [album rock, classic rock, hard rock, metal, r... 63 \n", + "\n", + " followers \n", + "0 {'href': None, 'total': 5238333} " + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_artist = pd.DataFrame(dict_artist)\n", + "df_artist" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "7fc6731b", + "metadata": {}, + "outputs": [], + "source": [ + "df_artist = df_artist.rename(columns = {\"id\" : \"artist_id\",\n", + " \"name\" : \"artist_name\",\n", + " \"genres\" : \"artist_genres\",\n", + " \"popularity\": \"artist_popularity\",\n", + " \"followers\": \"artist_followers\"})" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "e33a26e7", + "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", + "
artist_idartist_nameartist_genresartist_popularityartist_followers
0568ZhdwyaiCyOGJRtNYhWfDeep Purple[album rock, classic rock, hard rock, metal, r...63{'href': None, 'total': 5238333}
\n", + "
" + ], + "text/plain": [ + " artist_id artist_name \\\n", + "0 568ZhdwyaiCyOGJRtNYhWf Deep Purple \n", + "\n", + " artist_genres artist_popularity \\\n", + "0 [album rock, classic rock, hard rock, metal, r... 63 \n", + "\n", + " artist_followers \n", + "0 {'href': None, 'total': 5238333} " + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_artist" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "7317d4af", + "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", + "
artist_idartist_nameartist_genresartist_popularityartist_followersfollowers_clean
0568ZhdwyaiCyOGJRtNYhWfDeep Purple[album rock, classic rock, hard rock, metal, r...63{'href': None, 'total': 5238333}5238333
\n", + "
" + ], + "text/plain": [ + " artist_id artist_name \\\n", + "0 568ZhdwyaiCyOGJRtNYhWf Deep Purple \n", + "\n", + " artist_genres artist_popularity \\\n", + "0 [album rock, classic rock, hard rock, metal, r... 63 \n", + "\n", + " artist_followers followers_clean \n", + "0 {'href': None, 'total': 5238333} 5238333 " + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_artist[\"followers_clean\"] = df_artist[\"artist_followers\"].apply(lambda x: x[\"total\"] if \"total\" in x else None)\n", + "df_artist" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "e3d5732a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "https://api.spotify.com/v1/artists/51Blml2LZPmy7TTiAg47vQ/related-artists\n" + ] + } + ], + "source": [ + "id_related = \"51Blml2LZPmy7TTiAg47vQ\"\n", + "resource = \"artists\"\n", + "parameters_related = f\"/{id_related}/related-artists\"\n", + "url_related = base_url + resource + parameters_related\n", + "print (url_related)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "722e3a82", + "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", + "
related_idrelated_namerelated_genresrelated_popularityrelated_followersartist_idartist_nameartist_genresartist_popularityartist_followersfollowers_clean
04KWTAlx2RvbpseOGMEmROgR.E.M.[alternative rock, athens indie, permanent wav...71{'href': None, 'total': 4233689}568ZhdwyaiCyOGJRtNYhWfDeep Purple[album rock, classic rock, hard rock, metal, r...63{'href': None, 'total': 5238333}5238333
11eClJfHLoDI4rZe5HxzBFvINXS[australian rock, dance rock, funk rock, new r...68{'href': None, 'total': 2499365}568ZhdwyaiCyOGJRtNYhWfDeep Purple[album rock, classic rock, hard rock, metal, r...63{'href': None, 'total': 5238333}5238333
25NGO30tJxFlKixkPSgXcFEThe Police[album rock, classic rock, permanent wave, rock]74{'href': None, 'total': 5697330}568ZhdwyaiCyOGJRtNYhWfDeep Purple[album rock, classic rock, hard rock, metal, r...63{'href': None, 'total': 5238333}5238333
36hN9F0iuULZYWXppob22AjSimple Minds[album rock, dance rock, new romantic, new wav...64{'href': None, 'total': 1883060}568ZhdwyaiCyOGJRtNYhWfDeep Purple[album rock, classic rock, hard rock, metal, r...63{'href': None, 'total': 5238333}5238333
45gznATMVO85ZcLTkE9ULU7Lenny Kravitz[permanent wave, rock]67{'href': None, 'total': 3048167}568ZhdwyaiCyOGJRtNYhWfDeep Purple[album rock, classic rock, hard rock, metal, r...63{'href': None, 'total': 5238333}5238333
\n", + "
" + ], + "text/plain": [ + " related_id related_name \\\n", + "0 4KWTAlx2RvbpseOGMEmROg R.E.M. \n", + "1 1eClJfHLoDI4rZe5HxzBFv INXS \n", + "2 5NGO30tJxFlKixkPSgXcFE The Police \n", + "3 6hN9F0iuULZYWXppob22Aj Simple Minds \n", + "4 5gznATMVO85ZcLTkE9ULU7 Lenny Kravitz \n", + "\n", + " related_genres related_popularity \\\n", + "0 [alternative rock, athens indie, permanent wav... 71 \n", + "1 [australian rock, dance rock, funk rock, new r... 68 \n", + "2 [album rock, classic rock, permanent wave, rock] 74 \n", + "3 [album rock, dance rock, new romantic, new wav... 64 \n", + "4 [permanent wave, rock] 67 \n", + "\n", + " related_followers artist_id artist_name \\\n", + "0 {'href': None, 'total': 4233689} 568ZhdwyaiCyOGJRtNYhWf Deep Purple \n", + "1 {'href': None, 'total': 2499365} 568ZhdwyaiCyOGJRtNYhWf Deep Purple \n", + "2 {'href': None, 'total': 5697330} 568ZhdwyaiCyOGJRtNYhWf Deep Purple \n", + "3 {'href': None, 'total': 1883060} 568ZhdwyaiCyOGJRtNYhWf Deep Purple \n", + "4 {'href': None, 'total': 3048167} 568ZhdwyaiCyOGJRtNYhWf Deep Purple \n", + "\n", + " artist_genres artist_popularity \\\n", + "0 [album rock, classic rock, hard rock, metal, r... 63 \n", + "1 [album rock, classic rock, hard rock, metal, r... 63 \n", + "2 [album rock, classic rock, hard rock, metal, r... 63 \n", + "3 [album rock, classic rock, hard rock, metal, r... 63 \n", + "4 [album rock, classic rock, hard rock, metal, r... 63 \n", + "\n", + " artist_followers followers_clean \n", + "0 {'href': None, 'total': 5238333} 5238333 \n", + "1 {'href': None, 'total': 5238333} 5238333 \n", + "2 {'href': None, 'total': 5238333} 5238333 \n", + "3 {'href': None, 'total': 5238333} 5238333 \n", + "4 {'href': None, 'total': 5238333} 5238333 " + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "response = requests.get(url_related, headers = header_info).json()\n", + "df_related_artist = pd.DataFrame(response [\"artists\"])[[\"id\",\"name\",\"genres\",\"popularity\",\"followers\"]]\n", + "df_related_artist = df_related_artist.rename(columns = {\"id\" : \"related_id\",\n", + " \"name\" : \"related_name\",\n", + " \"genres\" : \"related_genres\",\n", + " \"popularity\": \"related_popularity\",\n", + " \"followers\": \"related_followers\"})\n", + "df_related_artist[\"artist_id\"]= id_selected\n", + "df_related_artists = pd.merge (df_related_artist, df_artist, on = \"artist_id\")\n", + "df_related_artists.head()" ] }, { @@ -188,8 +934,149 @@ "outputs": [], "source": [ "# Bonus\n", - "\n", - "\n" + "from dotenv import dotenv_value\n", + "config = dotenv_values (\".env\")\n", + "print(config)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00f8a595", + "metadata": {}, + "outputs": [], + "source": [ + "client_id = config[\"client_ID\"]\n", + "client_secret = config[\"client_secret\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3460a069", + "metadata": {}, + "outputs": [], + "source": [ + "auth_response = requests.post(auth_url{\"grant_type\": \"client_credentials\"})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e702c4f9", + "metadata": {}, + "outputs": [], + "source": [ + "import base64\n", + "user_id = \n", + "auth_header = base64.b64encode(f'{client_id}:{client_secret}'. encode(\"utf-8\")).decode(\"utf-8\")\n", + "auth_options = {\n", + " \"url\":\"https://accounts.spotify.com/api/token\"\n", + " , \"headers\":{\n", + " \"Authorization\" : f'Basic {auth_header}'\n", + " }, \"data\": {\n", + " \"grant_type\": \"client_credentials\"\n", + " }\n", + "}\n", + "response = requests.post(**auth_options)\n", + "if response.status_code == 200: \n", + " token = response.json(). get (\"access_token\")\n", + " api_url" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "91fedd1f", + "metadata": {}, + "outputs": [], + "source": [ + "import webbrowser\n", + "from urllib.pase import urlparse, parse_qs\n", + "client_id = client_id\n", + "redirect_uri = #copy url from app\n", + "scopes = \"playlist-modify-public\" #type of list to create, public or private\n", + "authorization_url = #look in documentation! https://accounts.spotify.com/authorize?client_id=%7Bclient_id%7D&response_type=token&redirect_uri=%7Bredirect_uri%7D&scope=%7Bscopes%7D\n", + "webbrowser.open(authorization_url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "acbacab7", + "metadata": {}, + "outputs": [], + "source": [ + "def get_access_token(): \n", + " auth_url = input(\"Enter the url after being directed: \")\n", + " parsed_url = urlparse" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4bd89b1e", + "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", + "access_token = url.replace('https://example.com/callback?code=','').split('&state')[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4eb3cf15", + "metadata": {}, + "outputs": [], + "source": [ + "# Replace these variables with your own values\n", + "#access_token = “your_access_token_here”\n", + "user_id = “1147225367\" # “your_user_id_here”\n", + "playlist_name = input(“Enter a playlist name:“)\n", + "playlist_description = f”Description of your playlist {playlist_name}”\n", + "# Define the API endpoint and headers\n", + "api_endpoint = f”https://api.spotify.com/v1/users/{user_id}/playlists”\n", + "headers = {\n", + " “Authorization”: f”Bearer {access_token}“,\n", + " “Content-Type”: “application/json”\n", + "}\n", + "# Create the playlist\n", + "playlist_data = {\n", + " “name”: playlist_name,\n", + " “description”: playlist_description,\n", + " “public”: True # You can change the visibility as needed\n", + "}\n", + "response = requests.post(api_endpoint, headers=headers, json=playlist_data)\n", + "if response.status_code == 201:\n", + " playlist_info = response.json()\n", + " print(“Playlist created successfully:“)\n", + " print(playlist_info)\n", + "else:\n", + " print(“Failed to create the playlist.“)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "032a594c", + "metadata": {}, + "outputs": [], + "source": [ + "def add_tracks_to_playlist(access_token, playlist_id, track_uris):\n", + " api_endpoint = f”https://api.spotify.com/v1/playlists/{playlist_id}/tracks”\n", + " headers = {\n", + " “Authorization”: f”Bearer {access_token}“,\n", + " “Content-Type”: “application/json”\n", + " }\n", + " data = {\n", + " “uris”: track_uris\n", + " }\n", + " response = requests.post(api_endpoint, headers=headers, json=data)\n", + " if response.status_code == 201:\n", + " print(“Tracks added to the playlist successfully.“)\n", + " else:\n", + " print(“Failed to add tracks to the playlist\")" ] }, { @@ -221,9 +1108,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python [conda env:.conda-ironhack]", + "display_name": "Python (jupyter_env)", "language": "python", - "name": "conda-env-.conda-ironhack-py" + "name": "jupyter_env" }, "language_info": { "codemirror_mode": { @@ -235,7 +1122,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.11" + "version": "3.10.13" } }, "nbformat": 4,