Skip to content

Instantly share code, notes, and snippets.

@scrathe
Last active March 23, 2020 03:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save scrathe/167cf1f7f307140c305c8c532b57a7fb to your computer and use it in GitHub Desktop.
#!/bin/bash
# what is this? query your Radarr library and make changes via the API.
# in this example we query all Radarr entries, identify movies that are named (movie title), THE (year) and update the .path and .folderName to (THE movie title) (year)
# after updating Radarr, rename the folders on the filesystem; https://gist.github.com/scrathe/0bedf625916df1a0d070c053c284017d
RADARR_API_KEY="1234567890"
RADARR_HOST="10.0.0.20"
RADARR_PORT="7878"
# path relative to the docker
relativePath="/movies/Movies/"
# https://stedolan.github.io/jq
prereq=$(which jq)
if [[ $? != 0 ]]; then
echo "ERROR, jq missing"
exit 1
fi
# query Radarr for all movies. this will take a long time if you have 100s/1000s of movies.
movies=`curl -s -H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Api-Key: $RADARR_API_KEY" \
-X GET http://$RADARR_HOST:$RADARR_PORT/api/movie`
# jq select some
# ids=$(echo $movies | jq '.[] | select(.monitored) | select(.downloaded) | .id')
# jq select all
ids=$(echo $movies | jq --compact-output '.[] | .id')
for id in $ids; do
movie=`curl -s -H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Api-Key: $RADARR_API_KEY" \
-X GET http://$RADARR_HOST:$RADARR_PORT/api/movie/$id`
movieTitle=$(echo $movie | jq --compact-output '.title')
movieDownloaded=$(echo $movie | jq --compact-output '.downloaded')
movieFile=$(echo $movie | jq --compact-output '.movieFile.relativePath')
moviePath=$(echo $movie | jq --compact-output '.path')
movieFolder=$(echo $movie | jq --compact-output '.folderName')
# match some
# if [ "$movieDownloaded" == "true" ]; then
# match all
if [[ ! -z "$movieDownloaded" ]]; then
# match movies with improper naming
# (movie title), THE (year)
regexThe="$relativePath(.*)\,\s(The|the)\s(\(.*\))"
if [[ $moviePath =~ $regexThe ]]; then
movieTitle=${BASH_REMATCH[1]}
movieThe=${BASH_REMATCH[2]}
movieYear=${BASH_REMATCH[3]}
newFoldername="${relativePath}$movieThe $movieTitle $movieYear"
echo "$id -> $movieTitle -> $moviePath -> $newFoldername"
# update .path and .folderName
# if there's a better way to do this, plz let me know
json=$(echo $movie | jq --compact-output --arg newFoldername "$newFoldername" '.path = $newFoldername')
json=$(echo $json | jq --compact-output --arg newFoldername "$newFoldername" '.folderName = $newFoldername')
# update Radarr
curl -s -H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Api-Key: $RADARR_API_KEY" \
-X PUT --data-binary "$json" "http://$RADARR_HOST:$RADARR_PORT/api/movie" >> /dev/null
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment