From 7091f89013858dee55efe02ccad18ea2b82d523f Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Fri, 10 Nov 2023 09:19:47 +0100 Subject: [PATCH 1/6] feat(Spotify): Add Get Recently Played Tracks --- .../Spotify_Get_Recently_Played_Tracks.ipynb | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 Spotify/Spotify_Get_Recently_Played_Tracks.ipynb diff --git a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb new file mode 100644 index 0000000000..18a4882c6b --- /dev/null +++ b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb @@ -0,0 +1,164 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "32592bfa-1cf3-41ba-a3de-e818fcd04a98", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "8753843b-4031-412c-8318-ede7474d982f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Spotify - Get Recently Played Tracks" + ] + }, + { + "cell_type": "markdown", + "id": "24ae204c-3131-4a95-ba33-e52b015b7b80", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #spotify #api #web-api #recently-played #get #tracks" + ] + }, + { + "cell_type": "markdown", + "id": "34ba67bd-7d4a-4e47-a399-0f44d627d828", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew-749944182/)" + ] + }, + { + "cell_type": "markdown", + "id": "0d3ad6ac-143b-41f4-a5e0-fda6f692549e", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-11-10 (Created: 2023-11-10)" + ] + }, + { + "cell_type": "markdown", + "id": "9bd83a34-4993-4925-9cf6-3252b043b1bf", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook will get tracks from the current user's recently played tracks. It is useful for organizations to keep track of the user's music preferences." + ] + }, + { + "cell_type": "markdown", + "id": "024e529a-c9f2-4957-a81f-f7049aeeda87", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n- [Spotify Web API Reference - Get Recently Played Tracks](https://developer.spotify.com/documentation/web-api/reference/get-recently-played)\n- [Spotify Authorization Guide](https://developer.spotify.com/documentation/general/guides/authorization-guide/)" + ] + }, + { + "cell_type": "markdown", + "id": "f17acda3-f0ff-42fa-9bcc-1f03ccd4e0ab", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "f376d209-a7be-4c4d-a6b8-4855563a3a31", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dd2add74-2e0e-466c-a90c-a24f73cfe525", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "import requests\nimport json", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "2d20e100-eb10-45ae-9fca-2829e8b35cf8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n- **token**: Access token to be used for authorization. [Follow this guide to get the token](https://developer.spotify.com/documentation/general/guides/authorization-guide/)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4fd005ee-de77-4c57-bec6-2e2ba039a801", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "token = \"BQD_YjyVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX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3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3y", + "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 6d51df9c49ed6e5ea42ebdd2d4db304291dc14ae Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Fri, 10 Nov 2023 09:22:13 +0100 Subject: [PATCH 2/6] feat: add missing markdown and update variables --- .../Spotify_Get_Recently_Played_Tracks.ipynb | 71 +++++++++++++++++-- 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb index 18a4882c6b..8e42e50e91 100644 --- a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb +++ b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb @@ -74,7 +74,9 @@ "tags": [] }, "source": [ - "**References:**\n- [Spotify Web API Reference - Get Recently Played Tracks](https://developer.spotify.com/documentation/web-api/reference/get-recently-played)\n- [Spotify Authorization Guide](https://developer.spotify.com/documentation/general/guides/authorization-guide/)" + "**References:**\n", + "- [Spotify Web API Reference - Get Recently Played Tracks](https://developer.spotify.com/documentation/web-api/reference/get-recently-played)\n", + "- [Spotify Authorization Guide](https://developer.spotify.com/documentation/general/guides/authorization-guide/)" ] }, { @@ -107,8 +109,11 @@ "papermill": {}, "tags": [] }, - "source": "import requests\nimport json", - "outputs": [] + "outputs": [], + "source": [ + "import requests\n", + "import json" + ] }, { "cell_type": "markdown", @@ -118,7 +123,8 @@ "tags": [] }, "source": [ - "### Setup variables\n- **token**: Access token to be used for authorization. [Follow this guide to get the token](https://developer.spotify.com/documentation/general/guides/authorization-guide/)" + "### Setup variables\n", + "- **token**: Access token to be used for authorization. [Follow this guide to get the token](https://developer.spotify.com/documentation/general/guides/authorization-guide/)" ] }, { @@ -129,8 +135,59 @@ "papermill": {}, "tags": [] }, - "source": "token = \"BQD_YjyVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX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3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3yVX3yV_3y", - "outputs": [] + "outputs": [], + "source": [ + "token = \"\"\n", + "limit = 10" + ] + }, + { + "cell_type": "markdown", + "id": "3abae558-7ddd-476f-9116-f62aa1396dc8", + "metadata": {}, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "21f1e2a3-35dd-4a95-ae44-6ab5bebaf29d", + "metadata": {}, + "source": [ + "### Get Recently Played Tracks" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "81dfb3ae-61c9-4098-a390-8c2db0fb4950", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "399a05c3-9180-49d3-8041-43facb9ca38e", + "metadata": {}, + "source": [ + "### Output" + ] + }, + { + "cell_type": "markdown", + "id": "05c038d8-04a0-4b6f-87f6-c9825e05ae88", + "metadata": {}, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8913840c-1192-4335-8a11-5e9c1c513915", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -161,4 +218,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} From 15287287a8fbed3af9953a5412a97294d15826e7 Mon Sep 17 00:00:00 2001 From: Alton Liew Date: Mon, 20 Nov 2023 09:03:44 +0100 Subject: [PATCH 3/6] feat: Get recently played tracks on Spotify This is a template used to get the recently played tracks on your Spotify account using client ID and client secret on the Spotify developer's page. --- .../Spotify_Get_Recently_Played_Tracks.ipynb | 147 ++++++++++++++++-- 1 file changed, 133 insertions(+), 14 deletions(-) diff --git a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb index 8e42e50e91..1224c9c048 100644 --- a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb +++ b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb @@ -103,16 +103,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "dd2add74-2e0e-466c-a90c-a24f73cfe525", "metadata": { + "execution": { + "iopub.execute_input": "2023-11-19T18:17:18.502959Z", + "iopub.status.busy": "2023-11-19T18:17:18.502693Z", + "iopub.status.idle": "2023-11-19T18:17:20.718907Z", + "shell.execute_reply": "2023-11-19T18:17:20.718274Z", + "shell.execute_reply.started": "2023-11-19T18:17:18.502885Z" + }, "papermill": {}, "tags": [] }, "outputs": [], "source": [ - "import requests\n", - "import json" + "try:\n", + " import spotipy\n", + "except:\n", + " !pip install spotipy --user\n", + " import spotipy\n", + "from spotipy.oauth2 import SpotifyOAuth\n", + "import naas" ] }, { @@ -124,21 +136,31 @@ }, "source": [ "### Setup variables\n", - "- **token**: Access token to be used for authorization. [Follow this guide to get the token](https://developer.spotify.com/documentation/general/guides/authorization-guide/)" + "### Mandatory\n", + "- `client_id` & `client_secret`: retrieve from Spotify Developers website.\n", + "- `redirect_uri`: redirect user to a page for authentication. Example: \"localhost: http://localhost:8888/callback\". Make sure this is the same URI used in the Spotify Developers website." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "4fd005ee-de77-4c57-bec6-2e2ba039a801", "metadata": { + "execution": { + "iopub.execute_input": "2023-11-19T18:17:20.720745Z", + "iopub.status.busy": "2023-11-19T18:17:20.720282Z", + "iopub.status.idle": "2023-11-19T18:17:21.008286Z", + "shell.execute_reply": "2023-11-19T18:17:21.005527Z", + "shell.execute_reply.started": "2023-11-19T18:17:20.720712Z" + }, "papermill": {}, "tags": [] }, "outputs": [], "source": [ - "token = \"\"\n", - "limit = 10" + "client_id = naas.secret.get('spotify_client_id')\n", + "client_secret = naas.secret.get('spotify_client_secret')\n", + "redirect_uri = naas.secret.get('spotify_redirect_uri')" ] }, { @@ -159,11 +181,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "81dfb3ae-61c9-4098-a390-8c2db0fb4950", - "metadata": {}, + "metadata": { + "execution": { + "iopub.execute_input": "2023-11-19T18:17:21.010482Z", + "iopub.status.busy": "2023-11-19T18:17:21.009954Z", + "iopub.status.idle": "2023-11-19T18:17:21.015733Z", + "shell.execute_reply": "2023-11-19T18:17:21.014972Z", + "shell.execute_reply.started": "2023-11-19T18:17:21.010449Z" + }, + "tags": [] + }, "outputs": [], - "source": [] + "source": [ + "def get_recently_played_tracks(client_id, client_secret, redirect_uri):\n", + " # Set up the Spotify OAuth manager\n", + " sp_oauth = SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope='user-read-recently-played')\n", + "\n", + " # Get the authorization token\n", + " token_info = sp_oauth.get_cached_token()\n", + "\n", + " # Use the obtained token to authenticate with the Spotify API\n", + " sp = spotipy.Spotify(auth=token_info['access_token'])\n", + "\n", + " # Get the current user's recently played tracks\n", + " recently_played = sp.current_user_recently_played()\n", + "\n", + " return recently_played" + ] }, { "cell_type": "markdown", @@ -183,11 +229,84 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "8913840c-1192-4335-8a11-5e9c1c513915", - "metadata": {}, - "outputs": [], - "source": [] + "metadata": { + "execution": { + "iopub.execute_input": "2023-11-19T18:17:21.017731Z", + "iopub.status.busy": "2023-11-19T18:17:21.017182Z", + "iopub.status.idle": "2023-11-19T18:17:21.475869Z", + "shell.execute_reply": "2023-11-19T18:17:21.475228Z", + "shell.execute_reply.started": "2023-11-19T18:17:21.017701Z" + }, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1: dothatshit!, Artist: Playboi Carti, Album: Playboi Carti\n", + "2: Choppa Won't Miss (feat. Young Thug), Artist: Playboi Carti, Album: Die Lit\n", + "3: Foreign, Artist: Playboi Carti, Album: Die Lit\n", + "4: Beno!, Artist: Playboi Carti, Album: Whole Lotta Red\n", + "5: Meh, Artist: Playboi Carti, Album: Whole Lotta Red\n", + "6: Poke It Out (with Nicki Minaj), Artist: Playboi Carti, Album: Die Lit\n", + "7: Over, Artist: Playboi Carti, Album: Whole Lotta Red\n", + "8: Yah Mean, Artist: Playboi Carti, Album: Playboi Carti\n", + "9: FlatBed Freestyle, Artist: Playboi Carti, Album: Die Lit\n", + "10: Die4Guy, Artist: Playboi Carti, Album: Whole Lotta Red\n", + "11: Let It Go, Artist: Playboi Carti, Album: Playboi Carti\n", + "12: Go2DaMoon (feat. Kanye West), Artist: Playboi Carti, Album: Whole Lotta Red\n", + "13: Teen X (feat. Future), Artist: Playboi Carti, Album: Whole Lotta Red\n", + "14: Sky, Artist: Playboi Carti, Album: Whole Lotta Red\n", + "15: Fell In Luv (feat. Bryson Tiller), Artist: Playboi Carti, Album: Die Lit\n", + "16: Vamp Anthem, Artist: Playboi Carti, Album: Whole Lotta Red\n", + "17: Miss The Rage, Artist: Trippie Redd, Album: Trip At Knight\n", + "18: Rockstar Made, Artist: Playboi Carti, Album: Whole Lotta Red\n", + "19: New Tank, Artist: Playboi Carti, Album: Whole Lotta Red\n", + "20: Mileage (feat. Chief Keef), Artist: Playboi Carti, Album: Die Lit\n", + "21: @ MEH, Artist: Playboi Carti, Album: @ MEH\n", + "22: R.I.P. Fredo (feat. Young Nudy) - Notice Me, Artist: Playboi Carti, Album: Die Lit\n", + "23: ILoveUIHateU, Artist: Playboi Carti, Album: Whole Lotta Red\n", + "24: wokeuplikethis*, Artist: Playboi Carti, Album: Playboi Carti\n", + "25: Shoota (feat. Lil Uzi Vert), Artist: Playboi Carti, Album: Die Lit\n", + "26: Flex, Artist: Playboi Carti, Album: Playboi Carti\n", + "27: New Choppa, Artist: Playboi Carti, Album: Playboi Carti\n", + "28: Lean 4 Real (feat. Skepta), Artist: Playboi Carti, Album: Die Lit\n", + "29: R.I.P., Artist: Playboi Carti, Album: Die Lit\n", + "30: Location, Artist: Playboi Carti, Album: Playboi Carti\n", + "31: Wildin', Artist: Berhana, Album: Wildin'\n", + "32: Grey Luh, Artist: Berhana, Album: Berhana\n", + "33: Streetcar, Artist: Daniel Caesar, Album: Pilgrim's Paradise\n", + "34: Take Me Away (feat. Syd), Artist: Daniel Caesar, Album: Freudian\n", + "35: Donchano, Artist: Steve Lacy, Album: The Lo-Fis\n", + "36: Ghost in the Machine (feat. Phoebe Bridgers), Artist: SZA, Album: SOS\n", + "37: Easy v2, Artist: Baird, Album: Easy v2\n", + "38: SANTA MONICA, Artist: Aries, Album: WELCOME HOME\n", + "39: The Need to Know (feat. SZA), Artist: Wale, Album: The Album About Nothing\n", + "40: Blessed, Artist: Daniel Caesar, Album: Freudian\n", + "41: Legends, Artist: Juice WRLD, Album: Legends\n", + "42: Sick, Artist: Dominic Fike, Album: Sunburn\n", + "43: INDIGO GIRL, Artist: A.CHAL, Album: EXOTIGAZ\n", + "44: THE zone~, Artist: Lil Yachty, Album: Let’s Start Here.\n", + "45: Life is Like a Dice Game - Spotify Singles, Artist: Nas, Album: Life is Like a Dice Game - Spotify Singles\n", + "46: SUPERPOSITION, Artist: Daniel Caesar, Album: CASE STUDY 01\n", + "47: GEEKIN, Artist: Ski Mask The Slump God, Album: BEWARE THE BOOK OF ELI\n", + "48: Ring Ring (with Rvssian feat. Clever), Artist: Juice WRLD, Album: Death Race For Love (Bonus Track Version)\n", + "49: Cherry Hill, Artist: Russ, Album: There's Really A Wolf\n", + "50: Matt Hardy 999, Artist: Trippie Redd, Album: Trip At Knight\n" + ] + } + ], + "source": [ + "index = 1\n", + "recently_played = get_recently_played_tracks(client_id, client_secret, redirect_uri)\n", + "for item in recently_played['items']:\n", + " track = item['track']\n", + " print(f\"{index}: {track['name']}, Artist: {track['artists'][0]['name']}, Album: {track['album']['name']}\")\n", + " index+=1" + ] } ], "metadata": { From 63d099274ace8c4266b92886b03236cdfedcd0a7 Mon Sep 17 00:00:00 2001 From: Alton Liew Date: Wed, 22 Nov 2023 09:42:15 +0100 Subject: [PATCH 4/6] fix: Updated output markdown --- Spotify/Spotify_Get_Recently_Played_Tracks.ipynb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb index 1224c9c048..7816a2eeb9 100644 --- a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb +++ b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb @@ -136,8 +136,9 @@ }, "source": [ "### Setup variables\n", - "### Mandatory\n", - "- `client_id` & `client_secret`: retrieve from Spotify Developers website.\n", + "**Mandatory**\n", + "- `client_id`: retrieve from Spotify Developers website. https://developer.spotify.com/\n", + "- `client_secret`: Spotify client secret from Spotify Developers website. https://developer.spotify.com/\n", "- `redirect_uri`: redirect user to a page for authentication. Example: \"localhost: http://localhost:8888/callback\". Make sure this is the same URI used in the Spotify Developers website." ] }, From c10ec880060af317f9d5ca72c21ec5833275ae18 Mon Sep 17 00:00:00 2001 From: Alton Liew Date: Wed, 22 Nov 2023 10:19:39 +0100 Subject: [PATCH 5/6] feat: Update output markdown --- Spotify/Spotify_Get_Recently_Played_Tracks.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb index 7816a2eeb9..4fd7b88aef 100644 --- a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb +++ b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb @@ -217,7 +217,7 @@ "id": "399a05c3-9180-49d3-8041-43facb9ca38e", "metadata": {}, "source": [ - "### Output" + "## Output" ] }, { From 2b0b51066d99b3aa6c362653e8816c09d09b669d Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Wed, 22 Nov 2023 17:26:02 +0100 Subject: [PATCH 6/6] feat: add spotify templates --- .../Spotify_Get_Recently_Played_Tracks.ipynb | 113 +------ Spotify/Spotify_Setup_connection.ipynb | 285 ++++++++++++++++++ 2 files changed, 300 insertions(+), 98 deletions(-) create mode 100644 Spotify/Spotify_Setup_connection.ipynb diff --git a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb index 4fd7b88aef..22349f43d2 100644 --- a/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb +++ b/Spotify/Spotify_Get_Recently_Played_Tracks.ipynb @@ -2,13 +2,13 @@ "cells": [ { "cell_type": "markdown", - "id": "32592bfa-1cf3-41ba-a3de-e818fcd04a98", + "id": "ae4e5f38-8ec3-42df-a16d-b7ffe17ae776", "metadata": { "papermill": {}, "tags": [] }, "source": [ - "\"Naas\"" + "\"Spotify\"" ] }, { @@ -30,7 +30,7 @@ "tags": [] }, "source": [ - "**Tags:** #spotify #api #web-api #recently-played #get #tracks" + "**Tags:** #spotify #api #web-api #recently-played #get #tracks #snippet" ] }, { @@ -103,16 +103,9 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "dd2add74-2e0e-466c-a90c-a24f73cfe525", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-19T18:17:18.502959Z", - "iopub.status.busy": "2023-11-19T18:17:18.502693Z", - "iopub.status.idle": "2023-11-19T18:17:20.718907Z", - "shell.execute_reply": "2023-11-19T18:17:20.718274Z", - "shell.execute_reply.started": "2023-11-19T18:17:18.502885Z" - }, "papermill": {}, "tags": [] }, @@ -144,24 +137,17 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "4fd005ee-de77-4c57-bec6-2e2ba039a801", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-19T18:17:20.720745Z", - "iopub.status.busy": "2023-11-19T18:17:20.720282Z", - "iopub.status.idle": "2023-11-19T18:17:21.008286Z", - "shell.execute_reply": "2023-11-19T18:17:21.005527Z", - "shell.execute_reply.started": "2023-11-19T18:17:20.720712Z" - }, "papermill": {}, "tags": [] }, "outputs": [], "source": [ - "client_id = naas.secret.get('spotify_client_id')\n", - "client_secret = naas.secret.get('spotify_client_secret')\n", - "redirect_uri = naas.secret.get('spotify_redirect_uri')" + "client_id = naas.secret.get('SPOTIFY_CLIENT_ID')\n", + "client_secret = naas.secret.get('SPOTIFY_CLIENT_SECRET')\n", + "redirect_uri = naas.secret.get('SPOTIFY_REDIRECT_URI')" ] }, { @@ -182,16 +168,9 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "81dfb3ae-61c9-4098-a390-8c2db0fb4950", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-19T18:17:21.010482Z", - "iopub.status.busy": "2023-11-19T18:17:21.009954Z", - "iopub.status.idle": "2023-11-19T18:17:21.015733Z", - "shell.execute_reply": "2023-11-19T18:17:21.014972Z", - "shell.execute_reply.started": "2023-11-19T18:17:21.010449Z" - }, "tags": [] }, "outputs": [], @@ -209,7 +188,10 @@ " # Get the current user's recently played tracks\n", " recently_played = sp.current_user_recently_played()\n", "\n", - " return recently_played" + " return recently_played\n", + "\n", + "recently_played = get_recently_played_tracks(client_id, client_secret, redirect_uri)\n", + "recently_played" ] }, { @@ -230,79 +212,14 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "8913840c-1192-4335-8a11-5e9c1c513915", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-19T18:17:21.017731Z", - "iopub.status.busy": "2023-11-19T18:17:21.017182Z", - "iopub.status.idle": "2023-11-19T18:17:21.475869Z", - "shell.execute_reply": "2023-11-19T18:17:21.475228Z", - "shell.execute_reply.started": "2023-11-19T18:17:21.017701Z" - }, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1: dothatshit!, Artist: Playboi Carti, Album: Playboi Carti\n", - "2: Choppa Won't Miss (feat. Young Thug), Artist: Playboi Carti, Album: Die Lit\n", - "3: Foreign, Artist: Playboi Carti, Album: Die Lit\n", - "4: Beno!, Artist: Playboi Carti, Album: Whole Lotta Red\n", - "5: Meh, Artist: Playboi Carti, Album: Whole Lotta Red\n", - "6: Poke It Out (with Nicki Minaj), Artist: Playboi Carti, Album: Die Lit\n", - "7: Over, Artist: Playboi Carti, Album: Whole Lotta Red\n", - "8: Yah Mean, Artist: Playboi Carti, Album: Playboi Carti\n", - "9: FlatBed Freestyle, Artist: Playboi Carti, Album: Die Lit\n", - "10: Die4Guy, Artist: Playboi Carti, Album: Whole Lotta Red\n", - "11: Let It Go, Artist: Playboi Carti, Album: Playboi Carti\n", - "12: Go2DaMoon (feat. Kanye West), Artist: Playboi Carti, Album: Whole Lotta Red\n", - "13: Teen X (feat. Future), Artist: Playboi Carti, Album: Whole Lotta Red\n", - "14: Sky, Artist: Playboi Carti, Album: Whole Lotta Red\n", - "15: Fell In Luv (feat. Bryson Tiller), Artist: Playboi Carti, Album: Die Lit\n", - "16: Vamp Anthem, Artist: Playboi Carti, Album: Whole Lotta Red\n", - "17: Miss The Rage, Artist: Trippie Redd, Album: Trip At Knight\n", - "18: Rockstar Made, Artist: Playboi Carti, Album: Whole Lotta Red\n", - "19: New Tank, Artist: Playboi Carti, Album: Whole Lotta Red\n", - "20: Mileage (feat. Chief Keef), Artist: Playboi Carti, Album: Die Lit\n", - "21: @ MEH, Artist: Playboi Carti, Album: @ MEH\n", - "22: R.I.P. Fredo (feat. Young Nudy) - Notice Me, Artist: Playboi Carti, Album: Die Lit\n", - "23: ILoveUIHateU, Artist: Playboi Carti, Album: Whole Lotta Red\n", - "24: wokeuplikethis*, Artist: Playboi Carti, Album: Playboi Carti\n", - "25: Shoota (feat. Lil Uzi Vert), Artist: Playboi Carti, Album: Die Lit\n", - "26: Flex, Artist: Playboi Carti, Album: Playboi Carti\n", - "27: New Choppa, Artist: Playboi Carti, Album: Playboi Carti\n", - "28: Lean 4 Real (feat. Skepta), Artist: Playboi Carti, Album: Die Lit\n", - "29: R.I.P., Artist: Playboi Carti, Album: Die Lit\n", - "30: Location, Artist: Playboi Carti, Album: Playboi Carti\n", - "31: Wildin', Artist: Berhana, Album: Wildin'\n", - "32: Grey Luh, Artist: Berhana, Album: Berhana\n", - "33: Streetcar, Artist: Daniel Caesar, Album: Pilgrim's Paradise\n", - "34: Take Me Away (feat. Syd), Artist: Daniel Caesar, Album: Freudian\n", - "35: Donchano, Artist: Steve Lacy, Album: The Lo-Fis\n", - "36: Ghost in the Machine (feat. Phoebe Bridgers), Artist: SZA, Album: SOS\n", - "37: Easy v2, Artist: Baird, Album: Easy v2\n", - "38: SANTA MONICA, Artist: Aries, Album: WELCOME HOME\n", - "39: The Need to Know (feat. SZA), Artist: Wale, Album: The Album About Nothing\n", - "40: Blessed, Artist: Daniel Caesar, Album: Freudian\n", - "41: Legends, Artist: Juice WRLD, Album: Legends\n", - "42: Sick, Artist: Dominic Fike, Album: Sunburn\n", - "43: INDIGO GIRL, Artist: A.CHAL, Album: EXOTIGAZ\n", - "44: THE zone~, Artist: Lil Yachty, Album: Let’s Start Here.\n", - "45: Life is Like a Dice Game - Spotify Singles, Artist: Nas, Album: Life is Like a Dice Game - Spotify Singles\n", - "46: SUPERPOSITION, Artist: Daniel Caesar, Album: CASE STUDY 01\n", - "47: GEEKIN, Artist: Ski Mask The Slump God, Album: BEWARE THE BOOK OF ELI\n", - "48: Ring Ring (with Rvssian feat. Clever), Artist: Juice WRLD, Album: Death Race For Love (Bonus Track Version)\n", - "49: Cherry Hill, Artist: Russ, Album: There's Really A Wolf\n", - "50: Matt Hardy 999, Artist: Trippie Redd, Album: Trip At Knight\n" - ] - } - ], + "outputs": [], "source": [ "index = 1\n", - "recently_played = get_recently_played_tracks(client_id, client_secret, redirect_uri)\n", "for item in recently_played['items']:\n", " track = item['track']\n", " print(f\"{index}: {track['name']}, Artist: {track['artists'][0]['name']}, Album: {track['album']['name']}\")\n", diff --git a/Spotify/Spotify_Setup_connection.ipynb b/Spotify/Spotify_Setup_connection.ipynb new file mode 100644 index 0000000000..d9c333f632 --- /dev/null +++ b/Spotify/Spotify_Setup_connection.ipynb @@ -0,0 +1,285 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "3af4bfda-c533-4945-9cd8-60cbc52cfeaa", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Spotify\"" + ] + }, + { + "cell_type": "markdown", + "id": "a5b2c509-2c29-49e8-af91-4f3f1e386da3", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Spotify - Setup connection\n", + "

Give Feedback | Bug report" + ] + }, + { + "cell_type": "markdown", + "id": "d77fe283-4edd-42d3-a909-8e207d4b842f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #spotify #naas #secret #connection #setup" + ] + }, + { + "cell_type": "markdown", + "id": "b90f2d91-c886-4e36-8265-b09d06bb1c7f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)" + ] + }, + { + "cell_type": "markdown", + "id": "6bbf7807-dda4-4b8a-b016-fe258a0fa33f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-11-22 (Created: 2023-11-22)" + ] + }, + { + "cell_type": "markdown", + "id": "214749fe-7f0b-4755-b7ea-1d200c234cc6", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook help you setup your connection with Spotify and add required credentials to Naas secret. You will be able to establish a seamless connection to Spotify without the need to repeatedly redefine your credentials in variables by using `naas.secret.get()` in your notebook." + ] + }, + { + "cell_type": "markdown", + "id": "a1329bea-ad80-4981-874c-8776b11f89a8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n", + "- [Spotify Authorization Guide](https://developer.spotify.com/documentation/general/guides/authorization-guide/)\n", + "- [Naas Secret Documentation](https://site.naas.ai/docs/developers/jobs/secret)" + ] + }, + { + "cell_type": "markdown", + "id": "cff349c8-2816-4ae4-9229-027c068eeb51", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "80266a9e-fe54-4f3e-aeb2-01483bbc53f9", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cc038b10-2679-42bc-909e-09a298339df4", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "import naas" + ] + }, + { + "cell_type": "markdown", + "id": "30127040-e487-4115-8317-5084823b3a6f", + "metadata": { + "papermill": {}, + "tags": [ + "variables" + ] + }, + "source": [ + "### Setup variables\n", + "**Mandatory**\n", + "- `client_id`: retrieve from Spotify Developers website. This value will be stored under the secret 'SPOTIFY_CLIENT_ID'.\n", + "- `client_secret`: Spotify client secret from Spotify Developers website. This value will be stored under the secret 'SPOTIFY_CLIENT_SECRET'.\n", + "- `redirect_uri`: redirect user to a page for authentication. Example: \"localhost: http://localhost:8888/callback\". Make sure this is the same URI used in the Spotify Developers website. This value will be stored under the secret 'SPOTIFY_REDIRECT_URI'.\n", + "\n", + "**Optional**\n", + "- `secrets`: Dict to add secrets to naas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8d86b8fb-2576-426d-a305-50043c24c23e", + "metadata": { + "papermill": {}, + "tags": [ + "parameters" + ] + }, + "outputs": [], + "source": [ + "# Mandatory\n", + "client_id = None\n", + "client_secret = None\n", + "redirect_uri = None\n", + "\n", + "# Optional\n", + "secrets = {\n", + " \"SPOTIFY_CLIENT_ID\": client_id,\n", + " \"SPOTIFY_CLIENT_SECRET\": client_secret,\n", + " \"SPOTIFY_REDIRECT_URI\": redirect_uri,\n", + "}" + ] + }, + { + "cell_type": "markdown", + "id": "1b0ae044-cac2-479f-b1fc-a3318696adf5", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "3aadae7f-fbe5-4f97-a6a9-6efe9dd1a42f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Add secret" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e235794a-6907-4f0e-ab2b-5aa6668a8d3b", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "for secret in secrets:\n", + " new_value = secrets.get(secret)\n", + " if new_value:\n", + " naas.secret.add(secret, secrets.get(secret))" + ] + }, + { + "cell_type": "markdown", + "id": "7c97931b-4ce8-46f9-a70c-45393c5669ee", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "760ec150-d79a-4208-adfe-d0336d443dc8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4005faa2-a781-4805-9c08-74951e900d12", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "df = naas.secret.list()\n", + "df = df[df[\"name\"].isin(secrets.keys())]\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3c817635-d953-4203-9a2e-87ab32f4878c", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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" + }, + "naas": { + "notebook_id": "c997c508df0523de3380a6ebbc1524a6eb496f90f04a657e36c53c7dc7517302", + "notebook_path": "Qdrant/Qdrant_Setup_connection.ipynb" + }, + "papermill": { + "default_parameters": {}, + "environment_variables": {}, + "parameters": {}, + "version": "2.4.0" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}