Skip to content

Instantly share code, notes, and snippets.

@simonebaracchi
Created March 3, 2024 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonebaracchi/6193bc02ffdb409ea71d7c2286e5d5f3 to your computer and use it in GitHub Desktop.
Save simonebaracchi/6193bc02ffdb409ea71d7c2286e5d5f3 to your computer and use it in GitHub Desktop.
Export Spotify "Recently played" to a new playlist
#!/bin/bash
# Usage:
# - open https://developer.spotify.com/documentation/web-api/reference/get-recently-played
# - open inspector and get bearer token 1
# - open https://developer.spotify.com/documentation/web-api/reference/add-tracks-to-playlist
# - open inspector and get bearer token 2
# - create a new spotify playlist and get the ID
# currently spotify APIs are limited to the last 50 played songs
if [ $# -ne 3 ]; then
echo "Usage: $0 <token1> <token2> <playlist>"
exit 1
fi
TOKEN1=$1
TOKEN2=$2
PLAYLIST=$3
AMOUNT=20
URL="https://api.spotify.com/v1/me/player/recently-played?limit=${AMOUNT}"
while true; do
echo Loading last songs ...
echo curl \"$URL\" -H \"$TOKEN1\"
curl "$URL" -H "$TOKEN1" > /tmp/spotify_recently_played
echo Extracting URIs ...
cat /tmp/spotify_recently_played |jq '{uris: [.items[].track.uri]}' > /tmp/spotify_tracks_for_playlist
echo Updating playlist ...
curl "https://api.spotify.com/v1/playlists/$PLAYLIST/tracks" -X POST -H "$TOKEN2" --data @/tmp/spotify_tracks_for_playlist
echo Done!
URL=`cat /tmp/spotify_recently_played | jq '.next' | tr -d '"'`
data_iso=`cat /tmp/spotify_recently_played | jq '.items[-1].played_at' | tr -d '"'`
echo "Press enter to load $AMOUNT more songs prior to ${data_iso}, or Ctrl-C to stop ..."
read
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment