Created
          May 15, 2024 22:44 
        
      - 
      
- 
        Save OhTerryTorres/51acfa80f4becd3a5d009bcac93dc0ff to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/bash | |
| # Based on the Windows version by mrsilver76 | |
| # https://github.com/mrsilver76/itunes_playlist_exporter | |
| # ASSUMPTIONS | |
| # 1. You have a Plex server. | |
| # 2. You have exported your playlists to a folder of .m3u files. | |
| # 3. You can update configuration values below. | |
| # LOCAL_PLAYLIST_LOCATION - the location to store the generated playlists. If | |
| # you plan on allowing other users/machines/software (such as Plex) to access | |
| # these playlists then you need to pick a location which is either on a network | |
| # drive or shared. | |
| LOCAL_PLAYLIST_LOCATION="/Users/terry/Music/Playlists/" | |
| # TOKEN - the API token required for this script to be able to access Plex. | |
| # Do not share your token with anyone. For details on how to find this, see | |
| # https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/ | |
| TOKEN="" | |
| # SERVER - the URL path to the server and port number. This can start http | |
| # or https depending on whether or not you're forcing secure connections. You | |
| # can use 127.0.0.1 to mean "the same machine". | |
| SERVER="http://127.0.0.1:32400" | |
| # LIBRARY_ID - the number of the library that the playlists will be loaded into. | |
| # Make sure this library exists, is set up for music and contains all the songs | |
| # that you have in the playlists. To find your library, go into the Plex web client, | |
| # hover the mouse over the library you want and look at the URL. It will end with | |
| # "source=xx" where xx is the library ID. | |
| LIBRARY_ID=1 | |
| # Delete playlists with the same name. Only works if you have xmlstarlet installed. | |
| # https://formulae.brew.sh/formula/xmlstarlet | |
| # https://xmlstarlet.readthedocs.io/en/latest/installation.html | |
| REPLACE_EXISTING=true | |
| echo "Start playlist upload!" | |
| for file in "$LOCAL_PLAYLIST_LOCATION"*; do \ | |
| if [ -f "$file" ]; then | |
| echo "--------" | |
| PLAYLIST_TITLE=${file/#$"${LOCAL_PLAYLIST_LOCATION}"} | |
| PLAYLIST_TITLE=${PLAYLIST_TITLE/%$".m3u"} | |
| ENCODED_FILENAME="${file// /%20}" #Update with other character replacements | |
| if $REPLACE_EXISTING; then | |
| echo "--------" | |
| echo "Checking for existing playlist with the same title "${PLAYLIST_TITLE}"." | |
| PLAYLISTS_URL="${SERVER}/playlists/?X-Plex-Token=${TOKEN}" | |
| PLAYLIST_XML="$(curl $PLAYLISTS_URL)" | |
| echo $PLAYLIST_XML > playlists.xml | |
| EXISTING_TITLE="$(xmlstarlet sel -t -m "//Playlist[@title='${PLAYLIST_TITLE}']" -v "@title" -n playlists.xml)" | |
| if [ "$EXISTING_TITLE" = "$PLAYLIST_TITLE" ]; then | |
| echo "--------" | |
| echo "Existing version of ${PLAYLIST_TITLE} found. Finding key." | |
| PLAYLIST_KEY="$(xmlstarlet sel -t -m "//Playlist[@title='${PLAYLIST_TITLE}']" -v "@ratingKey" -n playlists.xml)" | |
| echo "PLAYLIST_KEY is ${PLAYLIST_KEY}. Deleting existing version of ${PLAYLIST_TITLE}." | |
| DELETE_URL="${SERVER}/playlists/${PLAYLIST_KEY}?X-Plex-Token=${TOKEN}" | |
| echo "Deleting from ${DELETE_URL}." | |
| curl -X DELETE $DELETE_URL | |
| else | |
| echo "No existing version of ${PLAYLIST_TITLE} found." | |
| fi | |
| rm -f playlists.xml | |
| fi | |
| UPLOAD_URL="${SERVER}/playlists/upload?X-Plex-Token=${TOKEN}§ionID=${LIBRARY_ID}&path=${ENCODED_FILENAME}" | |
| echo "Uploading to ${UPLOAD_URL}" | |
| curl -X POST "{$UPLOAD_URL}" | |
| fi | |
| done | |
| echo "--------" | |
| echo "Complete!" | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment