goose-ws / bash-scripts

A collection of bash scripts I've hacked together over time
MIT License
10 stars 3 forks source link

[Bug] Sonarr updated TBA - Already refreshed series never processed #11

Closed vsilvar closed 3 months ago

vsilvar commented 3 months ago

With the recent changes in #10 , if the series has already been automatically refreshed by sonarr, the episode will never be renamed. This will be almost garanteed to happen if the script runs less frequently than sonarr's refresh interval (12h by default?), but even if that's not the case, it will still occur.

This happens because, as sonarr already refreshed the data, the title returned from the API will no longer be TBA:

episodeName="$(curl -skL "${containerIp}:${sonarrPort}${sonarrUrlBase}${apiEpisode}?seriesId=${seriesId[0]}&seasonNumber=${fileSeasonNum}" -H "X-api-key: ${sonarrApiKey}" -H "Content-Type: application/json" -H "Accept: application/json")"
episodeName="$(jq -M -r ".[] | select (.episodeNumber==${fileEpisodeNum}) | .title" <<<"${episodeName}")"
if ! [[ "${episodeName}" =~ ^TB[AD]$ ]]; then
    printOutput "2" "Clean episode title [${episodeName}] does not match TBA/TBD -- Skipping"
    continue
fi

I briefly looked at sonarr's API, but couldn't find any endpoint that would return the actual title detected from the file. As such, I think this logic needs to be reworked a little bit.

My suggestion:

*1: I see currently the RenameSeries command is being used, and a whole series is renamed at once, it might make sense to use RenameFiles instead which should make it possible to rename a single file/episode at a time.

vsilvar commented 3 months ago

A quick fix for the above issue could be: (without implementing either checking the /rename endpoint nor implementing the individual episode rename)

diff --git a/sonarr-update-tba.bash b/sonarr-update-tba.bash
index 397a302..08e7264 100755
--- a/sonarr-update-tba.bash
+++ b/sonarr-update-tba.bash
@@ -695,15 +695,7 @@ for containerName in "${containerIp[@]}"; do
             else
                 badExit "25" "Impossible condition"
             fi
-            
-            ### Leaving off here for today.
-            episodeName="$(curl -skL "${containerIp}:${sonarrPort}${sonarrUrlBase}${apiEpisode}?seriesId=${seriesId[0]}&seasonNumber=${fileSeasonNum}" -H "X-api-key: ${sonarrApiKey}" -H "Content-Type: application/json" -H "Accept: application/json")"
-            episodeName="$(jq -M -r ".[] | select (.episodeNumber==${fileEpisodeNum}) | .title" <<<"${episodeName}")"
-            if ! [[ "${episodeName}" =~ ^TB[AD]$ ]]; then
-                printOutput "2" "Clean episode title [${episodeName}] does not match TBA/TBD -- Skipping"
-                continue
-            fi
-            
+
             # Check to see if we should ignore the found file
             for ignoreId in "${ignoreEpisodes[@]}"; do
                 if [[ "${#containerIp[@]}" -eq "1" ]]; then
@@ -734,6 +726,16 @@ for containerName in "${containerIp[@]}"; do
                     break
                 fi
             done
+
+            if [[ "${skipRefresh}" -eq "0" ]]; then
+                ### Check to see if the episode title is TBA/TBD and needs refreshing
+                episodeName="$(curl -skL "${containerIp}:${sonarrPort}${sonarrUrlBase}${apiEpisode}?seriesId=${seriesId[0]}&seasonNumber=${fileSeasonNum}" -H "X-api-key: ${sonarrApiKey}" -H "Content-Type: application/json" -H "Accept: application/json")"
+                episodeName="$(jq -M -r ".[] | select (.episodeNumber==${fileEpisodeNum}) | .title" <<<"${episodeName}")"
+                if ! [[ "${episodeName}" =~ ^TB[AD]$ ]]; then
+                    printOutput "3" "Clean episode title [${episodeName}] has already been refreshed"
+                    skipRefresh="1"
+                fi
+            fi

             if [[ "${skipRefresh}" -eq "0" ]]; then
                 printOutput "2" "Issuing refresh command for: ${seriesTitle}"
@@ -764,11 +766,23 @@ for containerName in "${containerIp[@]}"; do
                     printOutput "3" "Sleeping 15 seconds to attempt to ensure system has time to process command"
                     sleep 15
                 fi
+            fi
+
+            skipRename="0"
+            for checkId in "${renamedSeries[@]}"; do
+                if [[ "${checkId}" == "${seriesId[0]}" ]]; then
+                    printOutput "3" "Series ID [${seriesId[0]}] has already been renamed"
+                    skipRename="1"
+                    break
+                fi
+            done

+            if [[ "${skipRename}" -eq "0" ]]; then
                 # Rename the series
                 printOutput "2" "Issuing rename command for: ${seriesTitle}"
                 commandOutput="$(curl -skL -X POST "${containerIp}:${sonarrPort}${sonarrUrlBase}${apiCommand}" -H "X-api-key: ${sonarrApiKey}" -H "Content-Type: application/json" -H "Accept: application/json" -d "{\"name\": \"RenameSeries\", \"seriesIds\": [${seriesId[0]}]}" 2>&1)"
                 commandId="$(jq -M -r ".id" <<< "${commandOutput}")"
+                renamedSeries+=("${seriesId[0]}")

                 # Give rename a second to process
                 sleep 1
goose-ws commented 3 months ago

Thanks for the detailed report and patch.

*1: I see currently the RenameSeries command is being used, and a whole series is renamed at once, it might make sense to use RenameFiles instead which should make it possible to rename a single file/episode at a time.

At the time I initially wrote this script up, the API was on v2 and I didn't know how to use Firefox's developer tools to see the API calls the web front-end is making. Additionally, there wasn't (that I remember) anything in their API documentation about renaming a single file, only renaming a series.

I found additional benefit from renaming an entire series, as something in my library tended to import multiple TBA's at a time, so it was beneficial to be able to knock them all out with one rename.

I agree that more precision is better than less precision, and I'll refine the API call to only rename what is needed rather than what is not.

My kids have a lot of activities this weekend that'll keep me away from my desktop (on mobile right now), but early next week I'll dive more into this and get it done.

goose-ws commented 3 months ago

Thanks again for the report @vsilvar. This should be fixed now.