Skip to content

Commit

Permalink
feat(Spotify): Remove playlist items
Browse files Browse the repository at this point in the history
  • Loading branch information
altonliew11 committed Dec 24, 2023
1 parent 30570bb commit c1c743f
Showing 1 changed file with 145 additions and 11 deletions.
156 changes: 145 additions & 11 deletions Spotify/Spotify_Remove_Playlist_Items.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"tags": []
},
"source": [
"**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew-749944182/)"
"**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew/)"
]
},
{
Expand Down Expand Up @@ -74,7 +74,9 @@
"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/)"
"**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/)"
]
},
{
Expand All @@ -101,14 +103,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"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": []
},
"source": "import requests\nimport json",
"outputs": []
"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",
Expand All @@ -118,19 +135,136 @@
"tags": []
},
"source": [
"### Setup variables\n- `playlist_id`: The Spotify ID of the playlist to remove items from.\n- `access_token`: The access token of the user.\n- `track_ids`: A list of Spotify IDs of the tracks to remove from the playlist."
"### 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": null,
"id": "7162df50-351b-4625-b440-2a35387e0afb",
"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": "playlist_id = '3Qm86XLflmIXVm1wcwkgDK'\naccess_token = 'BQDqVqV_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3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_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3V_3",
"outputs": []
"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": {
Expand Down Expand Up @@ -161,4 +295,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}

0 comments on commit c1c743f

Please sign in to comment.