From 59bddec852fa2a91820a6a07efa80b21fdd37bb1 Mon Sep 17 00:00:00 2001 From: Alton Liew Date: Fri, 22 Dec 2023 07:15:33 +0100 Subject: [PATCH] feat(Spotify): Get Users Playlists --- Spotify/Spotify_Get_Users_Playlists.ipynb | 487 ++++++++++++++++++++++ 1 file changed, 487 insertions(+) create mode 100644 Spotify/Spotify_Get_Users_Playlists.ipynb diff --git a/Spotify/Spotify_Get_Users_Playlists.ipynb b/Spotify/Spotify_Get_Users_Playlists.ipynb new file mode 100644 index 0000000000..09c8b909e8 --- /dev/null +++ b/Spotify/Spotify_Get_Users_Playlists.ipynb @@ -0,0 +1,487 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "250e36e5-4fd6-46e0-ba0f-053a36a63415", + "metadata": {}, + "source": [ + "\"Spotify.png\"" + ] + }, + { + "cell_type": "markdown", + "id": "595f753b-520e-4780-bf73-3b6a304e2312", + "metadata": {}, + "source": [ + "# Spotify - Get User's Playlists\n", + "

Give Feedback | Bug report" + ] + }, + { + "cell_type": "markdown", + "id": "de390462-b4cd-4754-be80-d3dc885897d2", + "metadata": {}, + "source": [ + "**Tags:** #spotify #api #web-api #users-playlists #get #snippet" + ] + }, + { + "cell_type": "markdown", + "id": "24f6cc32-ebff-44f0-aa75-b410faee64ec", + "metadata": {}, + "source": [ + "**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew-b099b0289/)" + ] + }, + { + "cell_type": "markdown", + "id": "094eebe7-34f8-456a-967a-e59e0c4e44b6", + "metadata": {}, + "source": [ + "**Last update:** 2023-12-22 (Created: 2023-12-22)" + ] + }, + { + "cell_type": "markdown", + "id": "3e1aaf33-f2ae-487a-abf3-8dc67ce4c642", + "metadata": {}, + "source": [ + "**Description:** This notebook will get a list of the playlists owned or followed by a Spotify user." + ] + }, + { + "cell_type": "markdown", + "id": "fb4fa312-cfc7-4433-9709-157d97a2888d", + "metadata": {}, + "source": [ + "**References:**\n", + "- [Spotify Web API Reference - Get User's Playlists](https://developer.spotify.com/documentation/web-api/reference/get-a-list-of-current-users-playlists)\n", + "- [Spotify Authorization Guide](https://developer.spotify.com/documentation/general/guides/authorization-guide/)" + ] + }, + { + "cell_type": "markdown", + "id": "6a7978a7-5884-4a09-8642-ef068864d2b9", + "metadata": {}, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "ef8e6dec-e764-4fa7-9e2b-b52735c9717b", + "metadata": {}, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "cfc83d47-339b-41b9-aabe-d42ce9af75f4", + "metadata": { + "execution": { + "iopub.execute_input": "2023-12-22T06:12:50.254538Z", + "iopub.status.busy": "2023-12-22T06:12:50.253987Z", + "iopub.status.idle": "2023-12-22T06:12:50.790120Z", + "shell.execute_reply": "2023-12-22T06:12:50.789305Z", + "shell.execute_reply.started": "2023-12-22T06:12:50.254458Z" + } + }, + "outputs": [], + "source": [ + "try:\n", + " import spotipy\n", + "except:\n", + " !pip install spotipy --user\n", + " import spotipy\n", + "from spotipy.oauth2 import SpotifyOAuth\n", + "from IPython.display import Image, display" + ] + }, + { + "cell_type": "markdown", + "id": "3a67f0d6-2ad9-40c8-bfe9-8477f4c08ad6", + "metadata": {}, + "source": [ + "### Setup variables\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." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "b1eebfe7-04d5-4316-ba3c-8b4ff1394625", + "metadata": { + "execution": { + "iopub.execute_input": "2023-12-22T06:12:50.811476Z", + "iopub.status.busy": "2023-12-22T06:12:50.802594Z", + "iopub.status.idle": "2023-12-22T06:12:50.818458Z", + "shell.execute_reply": "2023-12-22T06:12:50.817833Z", + "shell.execute_reply.started": "2023-12-22T06:12:50.811435Z" + } + }, + "outputs": [], + "source": [ + "client_id = \"YOUR_SPOTIFY_CLIENT_ID\"\n", + "client_secret = \"YOUR_SPOTIFY_CLIENT_SECRET\"\n", + "redirect_uri = \"YOUR_REDIRECT_URI\"" + ] + }, + { + "cell_type": "markdown", + "id": "e940ff23-2447-402a-b13b-cbbf0f757a62", + "metadata": {}, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "efaf431d-6998-478e-8c8d-94470986094b", + "metadata": {}, + "source": [ + "### Get User's Playlists" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b2da0be1-b65f-499d-bf3f-7d506bc9f3c6", + "metadata": { + "execution": { + "iopub.execute_input": "2023-12-22T06:12:50.825119Z", + "iopub.status.busy": "2023-12-22T06:12:50.819927Z", + "iopub.status.idle": "2023-12-22T06:12:50.919533Z", + "shell.execute_reply": "2023-12-22T06:12:50.918845Z", + "shell.execute_reply.started": "2023-12-22T06:12:50.825072Z" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "def authenticate_spotify(client_id, client_secret, redirect_uri):\n", + " sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope=\"user-read-recently-played\"))\n", + " \n", + " if sp.auth_manager.get_cached_token() is not None:\n", + " print(\"Successfully authenticated\")\n", + " return sp\n", + " else:\n", + " print(\"Authentication failed\")\n", + " auth_url = sp.auth_manager.get_authorize_url()\n", + " print(f\"Please visit this URL to authorize the application: {auth_url}\")\n", + " authorization_code = input(\"Enter the authorization code from the URL: \")\n", + " token_info = sp.auth_manager.get_access_token(authorization_code)\n", + " return sp\n", + " \n", + "def get_users_playlists(sp):\n", + " user_id = sp.current_user()['id']\n", + " all_playlists = sp.current_user_playlists()\n", + " image_width = 300\n", + " \n", + " print(\"Owned Playlists:\")\n", + " for playlist in all_playlists['items']:\n", + " if playlist['owner']['id'] == user_id:\n", + " print(f\"Playlist Name: {playlist['name']}, Playlist ID: {playlist['id']}\")\n", + " if len(playlist['images']) > 0:\n", + " image_url = playlist['images'][0]['url'] \n", + " display(Image(url=image_url, width=image_width))\n", + " else:\n", + " print(\"No image available for this playlist\")\n", + " print(\"\\n\")\n", + " \n", + " print(\"Followed Playlists:\")\n", + " for playlist in all_playlists['items']:\n", + " if playlist['owner']['id'] != user_id:\n", + " print(f\"Playlist Name: {playlist['name']}, Playlist ID: {playlist['id']}\")\n", + " if len(playlist['images']) > 0:\n", + " image_url = playlist['images'][0]['url'] \n", + " display(Image(url=image_url, width=image_width))\n", + " else:\n", + " print(\"No image available for this playlist\")\n", + " print(\"\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "d2a55088-a462-4414-8bb8-2df2a0c3092d", + "metadata": {}, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "e1045bb2-a90c-40d9-859d-94f2333899d1", + "metadata": {}, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "1e52fba0-881d-43e1-8498-8d72f1600dc4", + "metadata": { + "execution": { + "iopub.execute_input": "2023-12-22T06:12:50.926848Z", + "iopub.status.busy": "2023-12-22T06:12:50.924427Z", + "iopub.status.idle": "2023-12-22T06:12:51.300424Z", + "shell.execute_reply": "2023-12-22T06:12:51.299795Z", + "shell.execute_reply.started": "2023-12-22T06:12:50.926811Z" + }, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Successfully authenticated\n", + "Owned Playlists:\n", + "Playlist Name: whole_lotta_mumblin, Playlist ID: 2Gur8W4KyPe8jWmpPvYd68\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Playlist Name: constipation_medication, Playlist ID: 3lGzgz2Aq94TBw3yv371hg\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Playlist Name: step_down_3d, Playlist ID: 5eyRvNYMFvV0FG2ZmRbTsr\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Playlist Name: kmax_trax, Playlist ID: 7Cbe7xlL1ZOE70hm8f5E8o\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Playlist Name: wurli_guy, Playlist ID: 1RJUWpVWqBW48OQeJesI70\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Playlist Name: dunia_biru, Playlist ID: 5YM4YEAYngHHSDi31gNEF5\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Playlist Name: park_when_safe, Playlist ID: 0E4qlNBb7ssTJxigTQ9fKj\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Playlist Name: yii_siang_enjoyer, Playlist ID: 4wHSo5mSm88dgWYuJHBSoN\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Playlist Name: paranoid_android, Playlist ID: 3JPKHu4mlQygOi71YTIqpd\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Followed Playlists:\n", + "Playlist Name: Squaded it up V2, Playlist ID: 5Xm75KyLk7NIUhyh15JZOK\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n" + ] + } + ], + "source": [ + "spotify_client = authenticate_spotify(client_id, client_secret, redirect_uri)\n", + "if spotify_client:\n", + " get_users_playlists(spotify_client)" + ] + } + ], + "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" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}