From e1d94644115b29e6e93e48e60ad092e939c66ba4 Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Fri, 24 Nov 2023 09:02:47 +0100 Subject: [PATCH 1/3] feat(Spotify): Add Get Artist --- Spotify/Spotify_Get_Artist.ipynb | 241 +++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 Spotify/Spotify_Get_Artist.ipynb diff --git a/Spotify/Spotify_Get_Artist.ipynb b/Spotify/Spotify_Get_Artist.ipynb new file mode 100644 index 0000000000..195c3912f3 --- /dev/null +++ b/Spotify/Spotify_Get_Artist.ipynb @@ -0,0 +1,241 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "7eedaf4a-cc66-4062-b079-f6d1c2b15ce7", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "5f7d1345-70db-4c69-a857-6c056187e443", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Spotify - Get Artist" + ] + }, + { + "cell_type": "markdown", + "id": "424b9e14-6130-407c-bf91-eea7089eb572", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #spotify #api #getartist #webapi #reference #uniqueid" + ] + }, + { + "cell_type": "markdown", + "id": "e7cee5c3-0ca2-458a-abd8-11a2e956c408", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew-749944182/)" + ] + }, + { + "cell_type": "markdown", + "id": "3585127f-0eb4-448a-83d2-19e7d1ab69fd", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-11-24 (Created: 2023-11-24)" + ] + }, + { + "cell_type": "markdown", + "id": "384c4308-0f5c-4263-8ff0-aa9689274c82", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook retrieves Spotify catalog information for a single artist identified by their unique Spotify ID. It is useful for organizations to quickly access artist information from the Spotify API." + ] + }, + { + "cell_type": "markdown", + "id": "8c611dd7-5f56-458a-bd9e-474d515b0b89", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n- [Spotify Web API Reference - Get an Artist](https://developer.spotify.com/documentation/web-api/reference/get-an-artist)\n- [Spotify Developer Documentation](https://developer.spotify.com/documentation/)" + ] + }, + { + "cell_type": "markdown", + "id": "ae2b1138-de5e-41c7-9701-ba4f8176797a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "cbda9a83-e230-4c22-918f-3b33a63a5ec0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1626996-b265-445d-842f-764d9f8a7778", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "import requests", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "8690dee4-7735-4a22-863b-559a1cd39c4f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n- `client_id`: Your Spotify API client ID. [Get your client ID](https://developer.spotify.com/documentation/general/guides/app-settings/#register-your-app)\n- `client_secret`: Your Spotify API client secret. [Get your client secret](https://developer.spotify.com/documentation/general/guides/app-settings/#register-your-app)\n- `artist_id`: The unique Spotify ID for the artist. [Find the artist ID](https://developer.spotify.com/console/get-artist/)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e6756fee-ce1a-4e99-88cf-ec27f70d65d6", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "client_id = \"your_client_id\"\nclient_secret = \"your_client_secret\"\nartist_id = \"3WrFJ7ztbogyGnTHbHJFl2\"", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "afe5173d-a5e9-442f-9a8c-85076e01bbed", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "5b9e4cc8-d26d-4437-86ec-465acbfd3e72", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Get artist" + ] + }, + { + "cell_type": "markdown", + "id": "794416b0-f57c-464d-9f2a-e43aec0c82e3", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "Retrieve Spotify catalog information for a single artist identified by their unique Spotify ID." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10ea4a46-c567-4fd5-8c3c-507c99b3b249", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "# Get an access token\nurl = \"https://accounts.spotify.com/api/token\"\ndata = {\"grant_type\": \"client_credentials\"}\nheaders = {\n \"Authorization\": \"Basic \" + (client_id + \":\" + client_secret).encode(\"utf-8\").hex()\n}\nr = requests.post(url, data=data, headers=headers)\naccess_token = r.json()[\"access_token\"]\n# Get artist\nurl = \"https://api.spotify.com/v1/artists/\" + artist_id\nheaders = {\"Authorization\": \"Bearer \" + access_token}\nr = requests.get(url, headers=headers)\nartist = r.json()", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "669c4ea6-a758-479d-acb4-df2298120f1c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "d57bf9c5-9fc1-4e24-bb5e-c1b20568fd1e", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1b83be8-4b23-4078-8a7f-7f2810dfe114", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "artist", + "outputs": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file From d7cad93a1332a6d1c5cabeb04efeff56efc0b748 Mon Sep 17 00:00:00 2001 From: Alton Liew Date: Thu, 30 Nov 2023 23:01:37 +0100 Subject: [PATCH 2/3] feat(Spotify): Get artist information through ID --- Spotify/Spotify_Get_Artist.ipynb | 126 +++++++++++++++++++++++++++---- 1 file changed, 110 insertions(+), 16 deletions(-) diff --git a/Spotify/Spotify_Get_Artist.ipynb b/Spotify/Spotify_Get_Artist.ipynb index 195c3912f3..ef20d14b19 100644 --- a/Spotify/Spotify_Get_Artist.ipynb +++ b/Spotify/Spotify_Get_Artist.ipynb @@ -74,7 +74,9 @@ "tags": [] }, "source": [ - "**References:**\n- [Spotify Web API Reference - Get an Artist](https://developer.spotify.com/documentation/web-api/reference/get-an-artist)\n- [Spotify Developer Documentation](https://developer.spotify.com/documentation/)" + "**References:**\n", + "- [Spotify Web API Reference - Get an Artist](https://developer.spotify.com/documentation/web-api/reference/get-an-artist)\n", + "- [Spotify Developer Documentation](https://developer.spotify.com/documentation/)" ] }, { @@ -101,14 +103,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "d1626996-b265-445d-842f-764d9f8a7778", "metadata": { + "execution": { + "iopub.execute_input": "2023-11-30T21:54:03.968079Z", + "iopub.status.busy": "2023-11-30T21:54:03.967580Z", + "iopub.status.idle": "2023-11-30T21:54:06.141271Z", + "shell.execute_reply": "2023-11-30T21:54:06.140310Z", + "shell.execute_reply.started": "2023-11-30T21:54:03.967987Z" + }, "papermill": {}, "tags": [] }, - "source": "import requests", - "outputs": [] + "outputs": [], + "source": [ + "try:\n", + " import spotipy\n", + "except:\n", + " !pip install spotipy --user\n", + " import spotipy\n", + "from spotipy.oauth2 import SpotifyClientCredentials\n", + "import naas" + ] }, { "cell_type": "markdown", @@ -118,19 +135,33 @@ "tags": [] }, "source": [ - "### Setup variables\n- `client_id`: Your Spotify API client ID. [Get your client ID](https://developer.spotify.com/documentation/general/guides/app-settings/#register-your-app)\n- `client_secret`: Your Spotify API client secret. [Get your client secret](https://developer.spotify.com/documentation/general/guides/app-settings/#register-your-app)\n- `artist_id`: The unique Spotify ID for the artist. [Find the artist ID](https://developer.spotify.com/console/get-artist/)" + "### Setup variables\n", + "- `client_id`: Your Spotify API client ID. [Get your client ID](https://developer.spotify.com/documentation/general/guides/app-settings/#register-your-app)\n", + "- `client_secret`: Your Spotify API client secret. [Get your client secret](https://developer.spotify.com/documentation/general/guides/app-settings/#register-your-app)\n", + "- `artist_id`: The unique Spotify ID for the artist. [Find the artist ID](https://developer.spotify.com/console/get-artist/)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "e6756fee-ce1a-4e99-88cf-ec27f70d65d6", "metadata": { + "execution": { + "iopub.execute_input": "2023-11-30T21:54:06.143155Z", + "iopub.status.busy": "2023-11-30T21:54:06.142672Z", + "iopub.status.idle": "2023-11-30T21:54:06.341082Z", + "shell.execute_reply": "2023-11-30T21:54:06.340490Z", + "shell.execute_reply.started": "2023-11-30T21:54:06.143120Z" + }, "papermill": {}, "tags": [] }, - "source": "client_id = \"your_client_id\"\nclient_secret = \"your_client_secret\"\nartist_id = \"3WrFJ7ztbogyGnTHbHJFl2\"", - "outputs": [] + "outputs": [], + "source": [ + "client_id = naas.secret.get(\"SPOTIFY_CLIENT_ID\")\n", + "client_secret = naas.secret.get(\"SPOTIFY_CLIENT_SECRET\")\n", + "artist_id = \"6vWDO969PvNqNYHIOW5v0m\" #beyonce" + ] }, { "cell_type": "markdown", @@ -162,19 +193,44 @@ "tags": [] }, "source": [ - "Retrieve Spotify catalog information for a single artist identified by their unique Spotify ID." + "* Retrieve Spotify catalog information for a single artist identified by their unique Spotify ID.\n", + "* Sets up client with client id and client secret using spotipy library and fetches information." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "10ea4a46-c567-4fd5-8c3c-507c99b3b249", "metadata": { + "execution": { + "iopub.execute_input": "2023-11-30T21:54:06.344053Z", + "iopub.status.busy": "2023-11-30T21:54:06.343875Z", + "iopub.status.idle": "2023-11-30T21:54:06.349668Z", + "shell.execute_reply": "2023-11-30T21:54:06.349107Z", + "shell.execute_reply.started": "2023-11-30T21:54:06.344033Z" + }, "papermill": {}, "tags": [] }, - "source": "# Get an access token\nurl = \"https://accounts.spotify.com/api/token\"\ndata = {\"grant_type\": \"client_credentials\"}\nheaders = {\n \"Authorization\": \"Basic \" + (client_id + \":\" + client_secret).encode(\"utf-8\").hex()\n}\nr = requests.post(url, data=data, headers=headers)\naccess_token = r.json()[\"access_token\"]\n# Get artist\nurl = \"https://api.spotify.com/v1/artists/\" + artist_id\nheaders = {\"Authorization\": \"Bearer \" + access_token}\nr = requests.get(url, headers=headers)\nartist = r.json()", - "outputs": [] + "outputs": [], + "source": [ + "def get_artist(client_id, client_secret, artist_id):\n", + " sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))\n", + " \n", + " try:\n", + " artist = sp.artist(artist_id)\n", + " \n", + " return {\n", + " \"Name\": artist['name'],\n", + " \"Genres\": artist['genres'],\n", + " \"Popularity\": artist['popularity'],\n", + " \"Followers\": artist['followers']['total'],\n", + " \"External URLs\": artist['external_urls'],\n", + " }\n", + " except spotipy.SpotifyException as e:\n", + " print(f\"Error retrieving artist information: {e}\")\n", + " return None" + ] }, { "cell_type": "markdown", @@ -198,16 +254,54 @@ "### Display result" ] }, + { + "cell_type": "markdown", + "id": "ddb5ab93-01b1-4398-92bb-5853f6a12b54", + "metadata": {}, + "source": [ + "* Calls the function and passes in the client ID, secret, and artist ID as parameters.\n", + "* If artist information is available, this will print out the required information." + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "d1b83be8-4b23-4078-8a7f-7f2810dfe114", "metadata": { + "execution": { + "iopub.execute_input": "2023-11-30T21:54:06.352042Z", + "iopub.status.busy": "2023-11-30T21:54:06.351883Z", + "iopub.status.idle": "2023-11-30T21:54:06.513641Z", + "shell.execute_reply": "2023-11-30T21:54:06.513048Z", + "shell.execute_reply.started": "2023-11-30T21:54:06.352025Z" + }, "papermill": {}, "tags": [] }, - "source": "artist", - "outputs": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Artist Information:\n", + "Name: Beyoncé\n", + "Genres: ['pop', 'r&b']\n", + "Popularity: 83\n", + "Followers: 36497674\n", + "External URLs: {'spotify': 'https://open.spotify.com/artist/6vWDO969PvNqNYHIOW5v0m'}\n" + ] + } + ], + "source": [ + "artist_info = get_artist(client_id, client_secret, artist_id)\n", + "\n", + "if artist_info:\n", + " print(\"Artist Information:\")\n", + " for key, value in artist_info.items():\n", + " print(f\"{key}: {value}\")\n", + "else:\n", + " print(\"Failed to retrieve artist information.\")" + ] } ], "metadata": { @@ -238,4 +332,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} From 87fcdd2958609486a7552dcd849928259fe18e42 Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Fri, 1 Dec 2023 09:41:04 +0100 Subject: [PATCH 3/3] feat: update result display --- Spotify/Spotify_Get_Artist.ipynb | 94 ++++++++++---------------------- 1 file changed, 29 insertions(+), 65 deletions(-) diff --git a/Spotify/Spotify_Get_Artist.ipynb b/Spotify/Spotify_Get_Artist.ipynb index ef20d14b19..3367943242 100644 --- a/Spotify/Spotify_Get_Artist.ipynb +++ b/Spotify/Spotify_Get_Artist.ipynb @@ -30,7 +30,7 @@ "tags": [] }, "source": [ - "**Tags:** #spotify #api #getartist #webapi #reference #uniqueid" + "**Tags:** #spotify #api #getartist #webapi #reference #uniqueid #snippet" ] }, { @@ -52,7 +52,7 @@ "tags": [] }, "source": [ - "**Last update:** 2023-11-24 (Created: 2023-11-24)" + "**Last update:** 2023-12-01 (Created: 2023-11-24)" ] }, { @@ -63,7 +63,7 @@ "tags": [] }, "source": [ - "**Description:** This notebook retrieves Spotify catalog information for a single artist identified by their unique Spotify ID. It is useful for organizations to quickly access artist information from the Spotify API." + "**Description:** This notebook retrieves Spotify catalog information for a single artist identified by their unique Spotify ID." ] }, { @@ -103,16 +103,9 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "d1626996-b265-445d-842f-764d9f8a7778", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-30T21:54:03.968079Z", - "iopub.status.busy": "2023-11-30T21:54:03.967580Z", - "iopub.status.idle": "2023-11-30T21:54:06.141271Z", - "shell.execute_reply": "2023-11-30T21:54:06.140310Z", - "shell.execute_reply.started": "2023-11-30T21:54:03.967987Z" - }, "papermill": {}, "tags": [] }, @@ -124,7 +117,8 @@ " !pip install spotipy --user\n", " import spotipy\n", "from spotipy.oauth2 import SpotifyClientCredentials\n", - "import naas" + "import naas\n", + "from IPython.display import Image, display" ] }, { @@ -143,16 +137,9 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "e6756fee-ce1a-4e99-88cf-ec27f70d65d6", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-30T21:54:06.143155Z", - "iopub.status.busy": "2023-11-30T21:54:06.142672Z", - "iopub.status.idle": "2023-11-30T21:54:06.341082Z", - "shell.execute_reply": "2023-11-30T21:54:06.340490Z", - "shell.execute_reply.started": "2023-11-30T21:54:06.143120Z" - }, "papermill": {}, "tags": [] }, @@ -199,37 +186,25 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "10ea4a46-c567-4fd5-8c3c-507c99b3b249", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-30T21:54:06.344053Z", - "iopub.status.busy": "2023-11-30T21:54:06.343875Z", - "iopub.status.idle": "2023-11-30T21:54:06.349668Z", - "shell.execute_reply": "2023-11-30T21:54:06.349107Z", - "shell.execute_reply.started": "2023-11-30T21:54:06.344033Z" - }, "papermill": {}, "tags": [] }, "outputs": [], "source": [ "def get_artist(client_id, client_secret, artist_id):\n", + " data = None\n", " sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))\n", - " \n", " try:\n", - " artist = sp.artist(artist_id)\n", - " \n", - " return {\n", - " \"Name\": artist['name'],\n", - " \"Genres\": artist['genres'],\n", - " \"Popularity\": artist['popularity'],\n", - " \"Followers\": artist['followers']['total'],\n", - " \"External URLs\": artist['external_urls'],\n", - " }\n", + " data = sp.artist(artist_id)\n", " except spotipy.SpotifyException as e:\n", " print(f\"Error retrieving artist information: {e}\")\n", - " return None" + " return data\n", + " \n", + "data = get_artist(client_id, client_secret, artist_id)\n", + "data" ] }, { @@ -265,40 +240,29 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "d1b83be8-4b23-4078-8a7f-7f2810dfe114", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-30T21:54:06.352042Z", - "iopub.status.busy": "2023-11-30T21:54:06.351883Z", - "iopub.status.idle": "2023-11-30T21:54:06.513641Z", - "shell.execute_reply": "2023-11-30T21:54:06.513048Z", - "shell.execute_reply.started": "2023-11-30T21:54:06.352025Z" - }, "papermill": {}, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Artist Information:\n", - "Name: Beyoncé\n", - "Genres: ['pop', 'r&b']\n", - "Popularity: 83\n", - "Followers: 36497674\n", - "External URLs: {'spotify': 'https://open.spotify.com/artist/6vWDO969PvNqNYHIOW5v0m'}\n" - ] - } - ], + "outputs": [], "source": [ - "artist_info = get_artist(client_id, client_secret, artist_id)\n", - "\n", - "if artist_info:\n", + "if data:\n", + " artist_info = {\n", + " \"Name\": data['name'],\n", + " \"Genres\": data['genres'],\n", + " \"Popularity\": data['popularity'],\n", + " \"Followers\": data['followers']['total'],\n", + " \"External URLs\": data['external_urls'].get(\"spotify\"),\n", + " \"Image\": data['images'][0].get(\"url\"),\n", + " }\n", " print(\"Artist Information:\")\n", " for key, value in artist_info.items():\n", - " print(f\"{key}: {value}\")\n", + " if key == \"Image\":\n", + " display(Image(url=value))\n", + " else:\n", + " print(f\"{key}: {value}\")\n", "else:\n", " print(\"Failed to retrieve artist information.\")" ]