-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2484 from jupyter-naas/2470-spotify-remove-playli…
…st-items 2470 spotify remove playlist items
- Loading branch information
Showing
1 changed file
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,298 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d71768d2-6642-4331-822a-27fd3ce970b3", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"<img width=\"8%\" alt=\"Naas.png\" src=\"https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/.github/assets/logos/Naas.png\" style=\"border-radius: 15%\">" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ed563673-363f-4478-99d4-d34fad2e73c7", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# Spotify - Remove Playlist Items" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "7aef91cb-5cea-4b0d-b7da-f7a79a4d6880", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Tags:** #spotify #playlist #remove #items #web-api #reference" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "1989031a-38ca-4c11-bf91-5498251b8339", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b23001e1-962f-4f3d-95ec-0bca442b45e9", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Last update:** 2023-12-15 (Created: 2023-12-15)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f9a980b8-e21d-4046-8d04-65b55b7daa9f", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Description:** This notebook removes one or more items from a user's playlist. It uses the Spotify Web API to access the user's playlist and remove the items." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "aaa09de8-9929-4332-8ee7-261d511554f1", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**References:**\n", | ||
"- [Spotify Web API Reference - Remove Tracks from Playlist](https://developer.spotify.com/documentation/web-api/reference/remove-tracks-playlist)\n", | ||
"- [Spotify Web API Reference - Get a Playlist](https://developer.spotify.com/documentation/web-api/reference/playlists/get-playlist/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "9ef12259-c110-40e7-a680-b0561424f123", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "143ebd85-6900-45c2-87ef-38125207bc33", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "a0a3a29e-5b27-472d-af28-5312e08e40c6", | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2023-12-24T07:45:02.864194Z", | ||
"iopub.status.busy": "2023-12-24T07:45:02.863670Z", | ||
"iopub.status.idle": "2023-12-24T07:45:03.299917Z", | ||
"shell.execute_reply": "2023-12-24T07:45:03.250140Z", | ||
"shell.execute_reply.started": "2023-12-24T07:45:02.864111Z" | ||
}, | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"try:\n", | ||
" import spotipy\n", | ||
"except:\n", | ||
" !pip install spotipy --user\n", | ||
" import spotipy\n", | ||
"from spotipy.oauth2 import SpotifyOAuth\n", | ||
"from pprint import pprint" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "9801f660-e0db-4d90-95ee-b0b0349b5277", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Setup variables\n", | ||
"- **`client_id`**: Client ID of the Spotify app. [Get Client ID](https://developer.spotify.com/documentation/general/guides/authorization-guide/)\n", | ||
"- **`client_secret`**: Client Secret of the Spotify app. [Get Client Secret](https://developer.spotify.com/documentation/general/guides/authorization-guide/)\n", | ||
"- **`redirect_uri`**: This is where users are redirected to for authentication purposes. [Get Redirect URI](https://developer.spotify.com/documentation/general/guides/authorization-guide/)\n", | ||
"- `playlist_id`: The Spotify ID of the playlist to remove items from.\n", | ||
"- `track_ids`: A list of Spotify IDs of the tracks to remove from the playlist." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "51276aaa-3612-4387-9b73-62437828f305", | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2023-12-24T07:45:03.306172Z", | ||
"iopub.status.busy": "2023-12-24T07:45:03.304383Z", | ||
"iopub.status.idle": "2023-12-24T07:45:03.311512Z", | ||
"shell.execute_reply": "2023-12-24T07:45:03.310896Z", | ||
"shell.execute_reply.started": "2023-12-24T07:45:03.306132Z" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"client_id = \"YOUR_SPOTIFY_CLIENT_ID\"\n", | ||
"client_secret = \"YOUR_SPOTIFY_CLIENT_SECRET\"\n", | ||
"redirect_uri = \"YOUR_REDIRECT_URI\"\n", | ||
"playlist_id = 'YOUR_PLAYLIST_ID'\n", | ||
"track_ids = ['TRACK_ID_1', 'TRACK_ID_2', 'TRACK3_ID_3']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "047fcde4-542e-454d-afc2-78a4a133c6d7", | ||
"metadata": {}, | ||
"source": [ | ||
"## Model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f8fbcec8-fa22-4bd9-b9e1-794c967d5c56", | ||
"metadata": {}, | ||
"source": [ | ||
"### Remove playlist item" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "c27994ec-91cf-4584-bb93-bc27b2097678", | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2023-12-24T07:45:03.316004Z", | ||
"iopub.status.busy": "2023-12-24T07:45:03.315538Z", | ||
"iopub.status.idle": "2023-12-24T07:45:03.410050Z", | ||
"shell.execute_reply": "2023-12-24T07:45:03.409395Z", | ||
"shell.execute_reply.started": "2023-12-24T07:45:03.315974Z" | ||
} | ||
}, | ||
"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=\"playlist-modify-public playlist-modify-private\"))\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 remove_playlist_item(sp, playlist_id, track_ids):\n", | ||
" track_uris_to_remove = [f'spotify:track:{track_id}' for track_id in track_ids]\n", | ||
" sp.playlist_remove_all_occurrences_of_items(playlist_id, track_uris_to_remove)\n", | ||
" print(\"done\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8525faf2-aa7c-4365-8160-15cb65b162fd", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fe32a890-2584-44aa-8183-b576551f64b0", | ||
"metadata": {}, | ||
"source": [ | ||
"### Display result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "2aadfbb4-527f-4c27-813d-8340cd33cfdd", | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2023-12-24T07:45:03.411767Z", | ||
"iopub.status.busy": "2023-12-24T07:45:03.411359Z", | ||
"iopub.status.idle": "2023-12-24T07:45:03.778257Z", | ||
"shell.execute_reply": "2023-12-24T07:45:03.777587Z", | ||
"shell.execute_reply.started": "2023-12-24T07:45:03.411735Z" | ||
}, | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Successfully authenticated\n", | ||
"done\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"spotify_client = authenticate_spotify(client_id, client_secret, redirect_uri)\n", | ||
"\n", | ||
"if spotify_client:\n", | ||
" remove_playlist_item(spotify_client, playlist_id, track_ids)" | ||
] | ||
} | ||
], | ||
"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 | ||
} |