Advertisement
sk8ordie

spotify-album-export.sh

Oct 15th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.17 KB | None | 0 0
  1. #!/bin/bash
  2. #                               _
  3. #  ___ _ __   ___ _ __ ___   __| |
  4. # / __| '_ \ / __| '_ ` _ \ / _` |
  5. # \__ | |_) | (__| | | | | | (_| |
  6. # |___| .__/ \___|_| |_| |_|\__,_|
  7. #     |_|
  8. # Created by: spcmd
  9. # http://spcmd.github.io
  10. # https://github.com/spcmd
  11.  
  12. # This script allows you to export your album list from spotify
  13. # to json format and then extract infromation from it, then make
  14. # a simple album list. All with one simple command.
  15.  
  16. # The final format of the album list file will be:
  17. #   artist|album_title|album_id
  18. #   artist|album_title|album_id
  19. #   ...and so on
  20.  
  21. # Then you can process this file as you wish, for example
  22. # you can pipe it to dmenu with a script (like spotymenu).
  23.  
  24. # Dependecy: jq
  25. # Should be in your repositories. Or you can get it from here:
  26. # https://stedolan.github.io/jq/
  27.  
  28. # Related Spotify Documentation:
  29. # https://developer.spotify.com/web-api/get-users-saved-albums/
  30.  
  31. # Get token here:
  32. # https://developer.spotify.com/web-api/console/get-current-user-saved-albums/
  33. # Never include your token in this script!
  34. # You pass your token in the command line for this script.
  35.  
  36. # --------------------
  37. # --- Configurable ---
  38. # --------------------
  39.  
  40. # The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50.
  41. limit="50" # use the max
  42.  
  43. # Album list saved to this file
  44. file_albumlist=".\album_list.txt"
  45.  
  46. # -----------------
  47. # --- Functions ---
  48. # -----------------
  49.  
  50. dep_check() {
  51.     if [[ ! -x ".\jq" ]]; then
  52.         echo "Error: can't find 'jq'. Make sure you have it installed! https://stedolan.github.io/jq/"
  53.         exit 1
  54.     fi
  55. }
  56.  
  57. get_album_list() {
  58.  
  59.     if [[ -z $token ]]; then
  60.         echo -e "Error: you need to specify a token!\n"
  61.         usage
  62.         exit 1
  63.     fi
  64.  
  65.     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"
  66. }
  67.  
  68. get_album_list_with_offset() {
  69.     if [[ -z $offset ]] || [[ -z $token ]]; then
  70.         echo -e "Error: you need to specify an offset number and then a token!\n"
  71.         usage
  72.         exit 1
  73.     fi
  74.  
  75.         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"
  76.  
  77. }
  78.  
  79. add_by_uri(){
  80.     if [[ -z $uri ]]; then
  81.         echo -e "Error: you need to specify a spotify_uri!\n"
  82.         usage
  83.         exit 1
  84.     fi
  85.  
  86.     album_id=$(echo "$uri" | awk -F ':' '{print $3}')
  87.     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"
  88. }
  89.  
  90. usage() {
  91.     echo -e "Usage: $(basename "$0") [option] [token]\n"
  92.     echo "Without an option it will export the first 50 albums. This is the maximum that Spotify allows on a single run."
  93.     echo -e "Use 50 as an offset on the second run, then 100 on the third run an so on...\n"
  94.     echo "You can get your personal token here (after logged in to spotify):"
  95.     echo "https://developer.spotify.com/web-api/console/get-current-user-saved-albums/"
  96.     echo -e "Check the 'user-library-read' option.\n"
  97.     echo "Options:"
  98.     echo "              -o <offset> <token>     Offset number: begin the extraction from here. You have to specify the token after it."
  99.     echo "              -a <uri>                Add album to the album list by a spotify_uri (e.g.: spotify:album:41bTjcSaiEe4G40RVVHbux)"
  100.     echo "              -h                      This help page."
  101. }
  102.  
  103. # ----------------------
  104. # --- Case Statement ---
  105. # ----------------------
  106.  
  107. case "$1" in
  108.     -o) dep_check && offset="$2" token="$3" get_album_list_with_offset;;
  109.     -a) dep_check && uri="$2" add_by_uri;;
  110.     -h) usage;;
  111.     *) dep_check && token="$1" get_album_list;;
  112. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement