diff --git a/root/scripts/PlexNotify.bash b/root/scripts/PlexNotify.bash index b489f79..f5493b5 100644 --- a/root/scripts/PlexNotify.bash +++ b/root/scripts/PlexNotify.bash @@ -1,12 +1,11 @@ #!/usr/bin/env bash -lidarrArtistId=$lidarr_artist_id - +lidarrRootFolderPath="$(dirname "$lidarr_artist_path")" # auto-clean up log file to reduce space usage -if [ -f "/config/logs/Plex_MusicVideos.txt" ]; then - find /config/logs -type f -name "Plex_MusicVideos.txt" -size +1024k -delete +if [ -f "/config/logs/PlexNotify.txt" ]; then + find /config/logs -type f -name "PlexNotify.txt" -size +1024k -delete fi -exec &>> "/config/logs/Plex_MusicVideos.txt" -chmod 777 "/config/logs/Plex_MusicVideos.txt" +exec &>> "/config/logs/PlexNotify.txt" +chmod 777 "/config/logs/PlexNotify.txt" log () { m_time=`date "+%F %T"` @@ -18,102 +17,43 @@ if [ "$lidarr_eventtype" == "Test" ]; then exit 0 fi -lidarrUrlBase="$(cat /config/config.xml | xq | jq -r .Config.UrlBase)" -if [ "$lidarrUrlBase" = "null" ]; then - lidarrUrlBase="" -else - lidarrUrlBase="/${lidarrUrlBase}" -fi -lidarrApiKey="$(cat /config/config.xml | xq | jq -r .Config.ApiKey)" -lidarrUrl="http://127.0.0.1:8686${lidarrUrlBase}" -lidarrRootFolderPath="$(dirname "$lidarr_artist_path")" -lidarrArtistData="$(curl -s "$lidarrUrl/api/v1/artist/$lidarrArtistId?apikey=${lidarrApiKey}")" -lidarrArtistPath="$(echo "${lidarrArtistData}" | jq -r " .path")" -tidalArtistUrl=$(echo "${lidarrArtistData}" | jq -r ".links | .[] | select(.name==\"tidal\") | .url") -tidalArtistId="$(echo "$tidalArtistUrl" | grep -o '[[:digit:]]*' | sort -u)" - -if [ "$dlClientSource" = "tidal" ] || [ "$dlClientSource" = "both" ]; then - skipTidal=false - if [ ! -f /config/xdg/.tidal-dl.token.json ]; then - log "TIDAL :: ERROR :: tidal-dl client not configured, please run the main script \"/config/extended/scripts/download.sh\" to configure the client" - log "Exiting..." - exit 0 - fi -else - skipTidal=true -fi - -if [ "$skipTidal" = "false" ]; then - if [ -z "$tidalArtistUrl" ]; then - log "TIDAL :: ERROR :: musicbrainz id: $lidarrArtistForeignArtistId is missing Tidal link, see: \"/config/logs/tidal-arist-id-not-found.txt\" for more detail..." - touch "/config/logs/tidal-arist-id-not-found.txt" - if cat "/config/logs/tidal-arist-id-not-found.txt" | grep "https://musicbrainz.org/artist/$lidarrArtistForeignArtistId/relationships" | read; then - sleep 0.01 - else - echo "Update Musicbrainz Relationship Page: https://musicbrainz.org/artist/$lidarrArtistForeignArtistId/relationships for \"${lidarrArtistName}\" with Tidal Artist Link" >> "/config/logs/tidal-arist-id-not-found.txt" - chmod 666 "/config/logs/tidal-arist-id-not-found.txt" - chown abc:abc "/config/logs/tidal-arist-id-not-found.txt" - fi - skipTidal=true +until false +do + taskCount=$(ps aux | grep Plex_MusicVideos.bash | grep -v grep | wc -l) + if [ "$taskCount" -ge "1" ]; then + sleep 1 + else + break fi -fi +done -if [ "$skipTidal" = "false" ]; then - if [ ! -d /config/extended/cache/tidal ]; then - mkdir -p /config/extended/cache/tidal +plexLibraries="$(curl -s "$plexUrl/library/sections?X-Plex-Token=$plexToken" | xq .)" +if echo "$plexLibraries" | grep "$lidarrRootFolderPath" | read; then + if echo "$plexLibraries" | jq -r ".MediaContainer.Directory[] | select(.Location.\"@path\"==\"$lidarrRootFolderPath\") | .\"@key\"" &>/dev/null; then + plexlibrarykey="$(echo "$plexLibraries" | jq -r ".MediaContainer.Directory[] | select(.Location.\"@path\"==\"$lidarrRootFolderPath\") | .\"@key\"" | head -n 1)" + else + plexlibrarykey="$(echo "$plexLibraries" | jq -r ".MediaContainer.Directory | select(.Location.\"@path\"==\"$lidarrRootFolderPath\") | .\"@key\"" | head -n 1)" fi - - if [ ! -f /config/extended/cache/tidal/$tidalArtistId-videos.json ]; then - log "Caching Artist Videos List" - curl -s "https://api.tidal.com/v1/artists/${tidalArtistId}/videos?limit=10000&countryCode=$tidalCountryCode&filter=ALL" -H 'x-tidal-token: CzET4vdadNUFQ5JU' > /config/extended/cache/tidal/$tidalArtistId-videos.json + if [ -z "$plexlibrarykey" ]; then + log "ERROR: No Plex Library key found for \"$lidarrRootFolderPath\"" + exit 1 fi - -fi - -if [ "$skipTidal" = "false" ]; then - tidalArtistVideoCount=$(cat /config/extended/cache/tidal/$tidalArtistId-videos.json | jq -r '.items | sort_by(.duration) | .[].id' | wc -l) - if [ $tidalArtistVideoCount -ge 1 ]; then - for i in $(cat /config/extended/cache/tidal/$tidalArtistId-videos.json | jq -r '.items | sort_by(.duration) | .[].id'); do - tidalVideoTitle=$(cat /config/extended/cache/tidal/$tidalArtistId-videos.json | jq -r ".items[] | select(.id==$i) | .title") - if find "$lidarrArtistPath" -type f -iname "* - $tidalVideoTitle.*" | read; then - echo "$i :: $tidalVideoTitle :: Match" - matchedFile="$(find "$lidarrArtistPath" -type f -iname "* - $tidalVideoTitle.*")" - echo "$matchedFile" - fileDirectory="$(dirname "$matchedFile")" - fileName="$(basename "$matchedFile")" - fileNameNoExt="${fileName%.*}" - - if [ ! -f "$fileDirectory/$fileNameNoExt.mkv" ]; then - if [ ! -d /downloads/lidarr-extended/music-videos ]; then - mkdir -p /downloads/lidarr-extended/music-videos - chmod 777 /downloads/lidarr-extended/music-videos - chown abc:abc /downloads/lidarr-extended/music-videos - else - rm -rf /downloads/lidarr-extended/music-videos/* - fi - - tidal-dl -r P1080 - tidal-dl -o /downloads/lidarr-extended/music-videos -l "https://tidal.com/browse/video/$i" - - find "/downloads/lidarr-extended/music-videos" -type f -iname "*.mp4" -print0 | while IFS= read -r -d '' video; do - ffmpeg \ - -i "${video}" \ - -vcodec copy \ - -acodec copy \ - "$fileDirectory/$fileNameNoExt.mkv" - done - log "Downloaded Video to: $fileDirectory/$fileNameNoExt.mkv" - chmod 666 "$fileDirectory/$fileNameNoExt.mkv" - chown abc:abc "$fileDirectory/$fileNameNoExt.mkv" - rm -rf /downloads/lidarr-extended/music-videos/* - fi - - - fi - done +else + if echo "$plexLibraries" | grep -i "Unauthorized" | read; then + log "ERROR :: Cannot connect to Plex" + log "ERROR :: plexUrl or plexToken is invalid" + log "ERROR :: plexUrl is currently set to \"$plexUrl\"" + log "ERROR :: plexToken is currently set to \"$plexToken\"" + exit 1 else - log "ERROR :: No videos found for the artist on Tidal" + log "ERROR: No Plex Library found containing path \"$lidarrRootFolderPath\"" + log "ERROR: Add \"$lidarrRootFolderPath\" as a folder to a Plex Music Library" + exit 1 fi fi +plexFolderEncoded="$(jq -R -r @uri <<<"$lidarr_artist_path")" +curl -s "$plexUrl/library/sections/$plexlibrarykey/refresh?path=$plexFolderEncoded&X-Plex-Token=$plexToken" +log "Plex Scan notification sent! ($lidarr_artist_path)" + exit 0