Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # _
- # ___ _ __ ___ _ __ ___ __| |
- # / __| '_ \ / __| '_ ` _ \ / _` |
- # \__ | |_) | (__| | | | | | (_| |
- # |___| .__/ \___|_| |_| |_|\__,_|
- # |_|
- # Created by: spcmd
- # http://spcmd.github.io
- # https://github.com/spcmd
- # This script allows you to export your album list from spotify
- # to json format and then extract infromation from it, then make
- # a simple album list. All with one simple command.
- # The final format of the album list file will be:
- # artist|album_title|album_id
- # artist|album_title|album_id
- # ...and so on
- # Then you can process this file as you wish, for example
- # you can pipe it to dmenu with a script (like spotymenu).
- # Dependecy: jq
- # Should be in your repositories. Or you can get it from here:
- # https://stedolan.github.io/jq/
- # Related Spotify Documentation:
- # https://developer.spotify.com/web-api/get-users-saved-albums/
- # Get token here:
- # https://developer.spotify.com/web-api/console/get-current-user-saved-albums/
- # Never include your token in this script!
- # You pass your token in the command line for this script.
- # --------------------
- # --- Configurable ---
- # --------------------
- # The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50.
- limit="50" # use the max
- # Album list saved to this file
- file_albumlist=".\album_list.txt"
- # -----------------
- # --- Functions ---
- # -----------------
- dep_check() {
- if [[ ! -x ".\jq" ]]; then
- echo "Error: can't find 'jq'. Make sure you have it installed! https://stedolan.github.io/jq/"
- exit 1
- fi
- }
- get_album_list() {
- if [[ -z $token ]]; then
- echo -e "Error: you need to specify a token!\n"
- usage
- exit 1
- fi
- curl --silent -X GET "https://api.spotify.com/v1/me/albums?limit=$limit" -H "Accept: application/json" -H "Authorization: Bearer $token" | jq -r '.items | map([.album.artists[].name, .album.name, .album.external_urls.spotify] | join("\n")) | join("\n")' >> $file_albumlist && echo "Done. You can find the album list here: $file_albumlist"
- }
- get_album_list_with_offset() {
- if [[ -z $offset ]] || [[ -z $token ]]; then
- echo -e "Error: you need to specify an offset number and then a token!\n"
- usage
- exit 1
- fi
- curl --silent -X GET "https://api.spotify.com/v1/me/albums?offset=$offset&limit=$limit" -H "Authorization: Bearer $token" | jq -r '.items | map([.album.artists[].name, .album.name, .album.external_urls.spotify] | join("\n")) | join("\n")' >> $file_albumlist && echo "Done. You can find the album list here: $file_albumlist"
- }
- add_by_uri(){
- if [[ -z $uri ]]; then
- echo -e "Error: you need to specify a spotify_uri!\n"
- usage
- exit 1
- fi
- album_id=$(echo "$uri" | awk -F ':' '{print $3}')
- curl --silent -X GET "https://api.spotify.com/v1/albums/$album_id" | jq -r '. | [.artists[].name, .name, .id] | join("|")' >> $file_albumlist && echo "Done. You can find the album list here: $file_albumlist"
- }
- usage() {
- echo -e "Usage: $(basename "$0") [option] [token]\n"
- echo "Without an option it will export the first 50 albums. This is the maximum that Spotify allows on a single run."
- echo -e "Use 50 as an offset on the second run, then 100 on the third run an so on...\n"
- echo "You can get your personal token here (after logged in to spotify):"
- echo "https://developer.spotify.com/web-api/console/get-current-user-saved-albums/"
- echo -e "Check the 'user-library-read' option.\n"
- echo "Options:"
- echo " -o <offset> <token> Offset number: begin the extraction from here. You have to specify the token after it."
- echo " -a <uri> Add album to the album list by a spotify_uri (e.g.: spotify:album:41bTjcSaiEe4G40RVVHbux)"
- echo " -h This help page."
- }
- # ----------------------
- # --- Case Statement ---
- # ----------------------
- case "$1" in
- -o) dep_check && offset="$2" token="$3" get_album_list_with_offset;;
- -a) dep_check && uri="$2" add_by_uri;;
- -h) usage;;
- *) dep_check && token="$1" get_album_list;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement