From 0239e74b448f9509515a585af148f9fb51ba30ad Mon Sep 17 00:00:00 2001 From: RandomNinjaAtk Date: Sun, 5 Feb 2023 10:09:50 -0500 Subject: [PATCH] 1.0.1 - Use production code for link, pause to wait for import... #21 Script will attempt to scrape the production code and use it for the YouTube url, because it's often the correct code... Script now pauses until all sonarr tasks are complete, to allow time to import the downloaded file.... --- root/scripts/Youtube-Series-Downloader.bash | 28 ++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/root/scripts/Youtube-Series-Downloader.bash b/root/scripts/Youtube-Series-Downloader.bash index cc865d3..efb59b2 100644 --- a/root/scripts/Youtube-Series-Downloader.bash +++ b/root/scripts/Youtube-Series-Downloader.bash @@ -1,5 +1,5 @@ #!/usr/bin/env bash -scriptVersion="1.0.0" +scriptVersion="1.0.1" if [ -z "$arrUrl" ] || [ -z "$arrApiKey" ]; then arrUrlBase="$(cat /config/config.xml | xq | jq -r .Config.UrlBase)" @@ -46,6 +46,24 @@ NotifySonarrForImport () { sonarrProcessIt=$(curl -s "$arrUrl/api/v3/command" --header "X-Api-Key:"${arrApiKey} -H "Content-Type: application/json" --data "{\"name\":\"DownloadedEpisodesScan\", \"path\":\"$1\"}") } +SonarrTaskStatusCheck () { + alerted=no + until false + do + taskCount=$(curl -s "$arrUrl/api/v3/command?apikey=${arrApiKey}" | jq -r '.[] | select(.status=="started") | .name' | grep -v "RescanFolders" | wc -l) + if [ "$taskCount" -ge "1" ]; then + if [ "$alerted" == "no" ]; then + alerted=yes + log "STATUS :: SONARR BUSY :: Pausing/waiting for all active Sonarr tasks to end..." + fi + sleep 2 + else + break + fi + done +} + + CookiesCheck sonarrSeriesList=$(curl -s --header "X-Api-Key:"${arrApiKey} --request GET "$arrUrl/api/v3/series") @@ -74,6 +92,13 @@ for id in $(echo $sonarrSeriesIds); do episodeNumber=$(echo $seriesEpisdodeData | jq -r .episodeNumber) downloadUrl=$(curl -s "https://thetvdb.com/series/$seriesTvdbTitleSlug/episodes/$episodeId" | grep -i youtube.com | grep -i watch | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*") + if [ -z $downloadUrl ]; then + downloadUrl=$(curl -s "https://thetvdb.com/series/$seriesTvdbTitleSlug/episodes/$episodeId" | grep -iwns "production code" -A 2 | sed 's/\ //g' | cut -d "-" -f2 | tail -n1) + if [ ! -z $downloadUrl ]; then + downloadUrl="https://www.youtube.com/watch?v=$downloadUrl" + fi + fi + if [ -z $downloadUrl ]; then log "$loopCount/$sonarrSeriesTotal :: $currentLoopIteration/$seriesEpisodeTvdbIdsCount :: $seriesTitle :: S${episodeSeasonNumber}E${episodeNumber} :: ERROR :: No Download URL found, skipping" continue @@ -107,6 +132,7 @@ for id in $(echo $sonarrSeriesIds); do NotifySonarrForImport "$downloadLocation/$fileName.mkv" log "$loopCount/$sonarrSeriesTotal :: $currentLoopIteration/$seriesEpisodeTvdbIdsCount :: $seriesTitle :: S${episodeSeasonNumber}E${episodeNumber} :: Notified Sonarr to import \"$fileName.mkv\"" fi + SonarrTaskStatusCheck done done exit