Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ dmypy.json

# PyCharm
.idea/

.env
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ This script prints all tracks from a Spotify playlist. It supports playlists wit
limit (100 songs), making multiple requests.

In the first line, it shows the name of the playlist and the number of songs. Then, after a blank line, it shows one
song per line, with the list of artists sepparated with commas and the name of the song.
song per line, with the list of artists separated with commas and the name of the song.

This is the first script I wrote as I started tinkering with spotipy, and it probably doesn't have much practical use.

Expand Down Expand Up @@ -93,6 +93,8 @@ Alok, Bastille — Run Into Trouble
Jax Jones, Martin Solveig, GRACEY, Europa — Lonely Heart
```

Instead of a playlist id you can pass `saved` to get your Liked Songs list.

### [create_playlist_mix](https://github.com/albertored11/spotipy-scripts/blob/main/scripts/create_playlist_mix.py)

This script creates a Spotify playlist with random tracks from other playlists.
Expand Down Expand Up @@ -328,7 +330,7 @@ listen to them later.
The playlist IDs (source and destination) are read from the first and the second program arguments, respectively:

```bash
python scripts/get_playlist_tracks.py <source_playlist_id> <dest_playlist_id>
python scripts/copy_to_playlist.py <source_playlist_id> <dest_playlist_id>
```

Running the script would append the tracks from the playlist with ID `<source_playlist_id>` to the end of the playlist
Expand All @@ -341,3 +343,21 @@ the script once a week.

My choice is using a [systemd timer](https://wiki.archlinux.org/title/Systemd/Timers) to run the script every friday at
12:00, but a cron job should also do the work.

### [copy_saved_to_playlist](blob/main/scripts/copy_saved_to_playlist.py)

This script takes the tracks from the Liked Songs playlist and appends them to the end of a different, existing
playlist, avoiding duplicates.

The reason I wrote this script is that the Spotify mobile app seems to slow down and malfunction when the Liked
Songs list becomes very large. This script allows me to copy that Liked Songs list over to an archive list and
remove a few thousand songs from the Liked list, making my experience in the app significantly better.

The playlist ID (destination) is read from the first program argument:

```bash
python scripts/copy_saved_to_playlist.py <dest_playlist_id>
```

Running the script would append the tracks from the playlist with ID `<source_playlist_id>` to the end of the playlist
with ID `<dest_playlist_id>`, excepting the ones that already existed in the latter.
64 changes: 64 additions & 0 deletions scripts/copy_saved_to_playlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Script that takes the tracks from your Liked Songs and appends them to the end of a different, existing playlist
# Requires: spotipy
# Usage: python copy_to_playlist.py <dest_playlist_id>
# Set SPOTIPY_CLIENT_ID and SPOTIPY_CLIENT_SECRET environment variables
# Set SPOTIPY_REDIRECT_URI environment variable (e. g. to http://localhost:9090)

import spotipy
import sys
from spotipy.oauth2 import SpotifyOAuth

if len(sys.argv) < 2:
print("Usage: python copy_to_playlist.py <source_playlist_id> <dest_playlist_id>", file=sys.stderr)
exit(1)

# Define needed scopes:
# * playlist-read-collaborative: to get tracks from collab playlists
# * playlist-read-private: to get tracks from private playlists
# * playlist-modify-private: to create the playlist and add tracks to it
# * user-library-read: to read the Liked playlist and get tracks from it
scope = "playlist-read-collaborative playlist-read-private playlist-modify-private user-library-read"

# Set up auth using Authorization Code Flow
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))

dest_playlist_id = sys.argv[1]

offset = 0 # Keep an offset bc requests have a 100 track limit
source_playlist_song_ids = [] # List for the songs from source playlist
dest_playlist_song_ids = [] # List for the songs from destination playlist

# Request tracks from destination playlist
items = sp.playlist_items(dest_playlist_id, limit=100)['items']

# Iterate while getting items from the request
while len(items) > 0:
for item in items:
dest_playlist_song_ids.append(item['track']['id']) # Append the ID of the track

# Request next 100 items
offset += 100
items = sp.playlist_items(dest_playlist_id, limit=100, offset=offset)['items']

offset = 0 # Reset offset

# Request tracks from source Liked playlist
# Note: Liked Playlist has a limit of 50 tracks per request
items = sp.current_user_saved_tracks(limit=50, offset=0, market="US")['items']

# Iterate while getting items from the request
while len(items) > 0:
for item in items:
source_playlist_song_ids.append(item['track']['id']) # Append the ID of the track

# Request next 50 items
offset += 50
items = sp.current_user_saved_tracks(limit=50, offset=offset, market="US")['items']

# Remove tracks that already are in destination playlist to avoid duplicates
source_playlist_song_ids = [x for x in source_playlist_song_ids if x not in dest_playlist_song_ids]

# Add tracks to the destination playlist 100 by 100 due to the limit
while len(source_playlist_song_ids) > 0:
sp.playlist_add_items(dest_playlist_id, source_playlist_song_ids[:100])
source_playlist_song_ids = source_playlist_song_ids[100:]
7 changes: 5 additions & 2 deletions scripts/get_playlist_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
print(playlist['name'] + " — " + str(track_total) + " tracks\n")

offset = 0 # Keep an offset bc requests have a 100 track limit

count = 0
# Iterate while getting items from the request
while len(items) > 0:
for item in items:
track = item['track']
artists = track['artists']

count += 1
# If just one artist, print its name
if len(artists) == 1:
print(artists[0]['name'] + " — ", end="")
Expand All @@ -71,3 +71,6 @@
else:
offset += 100
items = sp.playlist_items(playlist_id, limit=100, offset=offset)['items']


print(count);