2023-07-08 18:38:28 +02:00
#!/usr/bin/env bash
2023-07-19 20:08:51 +02:00
scriptVersion = "1.5"
2023-07-12 13:11:58 +02:00
scriptName = "AutoConfig"
#### Import Settings
s o u r c e / c o n f i g / e x t e n d e d . c o n f
2023-07-10 12:55:29 +02:00
l o g ( ) {
m_time = `date "+%F %T"`
2023-07-12 13:11:58 +02:00
e c h o $ m _ t i m e " : : $ s c r i p t N a m e : : $ s c r i p t V e r s i o n : : " $ 1
2023-07-10 12:55:29 +02:00
}
2023-07-08 18:38:28 +02:00
2023-07-19 20:08:51 +02:00
l o g f i l e S e t u p ( ) {
# auto-clean up log file to reduce space usage
i f [ - f " / c o n f i g / l o g s / $ s c r i p t N a m e . t x t " ] ; then
f i n d / c o n f i g / l o g s - t y p e f - n a m e " $ s c r i p t N a m e . t x t " - s i z e + 1 0 2 4 k - d e l e t e
f i
i f [ ! - f " / c o n f i g / l o g s / $ s c r i p t N a m e . t x t " ] ; then
t o u c h " / c o n f i g / l o g s / $ s c r i p t N a m e . t x t "
c h m o d 6 6 6 " / c o n f i g / l o g s / $ s c r i p t N a m e . t x t "
f i
}
# Create Log, start writing...
l o g f i l e S e t u p
e x e c & > > ( t e e - a " / c o n f i g / l o g s / $ s c r i p t N a m e . t x t " )
2023-07-12 13:11:58 +02:00
if [ "$enableAutoConfig" ! = "true" ]; then
2023-07-12 13:12:26 +02:00
l o g " S c r i p t i s n o t e n a b l e d , e n a b l e b y s e t t i n g e n a b l e A u t o C o n f i g t o \ " t r u e \ " b y m o d i f y i n g t h e \ " / c o n f i g / e x t e n d e d . c o n f \ " c o n f i g f i l e . . . "
2023-07-12 13:11:58 +02:00
l o g " S l e e p i n g ( i n f i n i t y ) "
s l e e p i n f i n i t y
f i
2023-07-08 18:38:28 +02:00
g e t A r r A p p I n f o ( ) {
# Get Arr App information
i f [ - z " $ a r r U r l " ] | | [ - z " $ a r r A p i K e y " ] ; then
arrUrlBase = "$(cat /config/config.xml | xq | jq -r .Config.UrlBase)"
if [ "$arrUrlBase" = = "null" ]; then
arrUrlBase = ""
e l s e
arrUrlBase = "/$(echo "$arrUrlBase" | sed "s/\///g")"
f i
arrName = "$(cat /config/config.xml | xq | jq -r .Config.InstanceName)"
arrApiKey = "$(cat /config/config.xml | xq | jq -r .Config.ApiKey)"
arrPort = "$(cat /config/config.xml | xq | jq -r .Config.Port)"
arrUrl = "http://127.0.0.1:${arrPort}${arrUrlBase}"
f i
}
v e r i f y A p i A c c e s s ( ) {
u n t i l f a l s e
d o
arrApiTest = ""
arrApiVersion = ""
2023-07-15 01:21:38 +02:00
if [ "$arrPort" = = "8989" ] || [ "$arrPort" == "7878" ]; then
2023-07-08 18:38:28 +02:00
arrApiVersion = "v3"
2023-07-15 01:21:38 +02:00
elif [ "$arrPort" = = "8686" ] || [ "$arrPort" == "8787" ]; then
2023-07-08 18:38:28 +02:00
arrApiVersion = "v1"
f i
arrApiTest = $(curl -s "$arrUrl/api/$arrApiVersion/system/status?apikey=$arrApiKey" | jq -r .instanceName)
if [ "$arrApiTest" = = "$arrName" ]; then
b r e a k
e l s e
l o g " $ a r r N a m e i s n o t r e a d y , s l e e p i n g u n t i l v a l i d r e s p o n s e . . . "
s l e e p 1
f i
d o n e
}
g e t A r r A p p I n f o
v e r i f y A p i A c c e s s
2023-07-08 18:46:56 +02:00
i f [ - f / c o n f i g / e x t e n d e d / n a m i n g . j s o n ] ; then
l o g " U s i n g c u s t o m S o n a r r N a m i n g ( / c o n f i g / e x t e n d e d / n a m i n g . j s o n ) . . . "
namingJson = $(cat /config/extended/naming.json)
2023-07-08 18:38:28 +02:00
e l s e
l o g " G e t t i n g T r a s h G u i d e R e c o m m e n d e d N a m i n g . . . "
namingJson = $(curl -s "https://raw.githubusercontent.com/TRaSH-/Guides/master/docs/json/sonarr/naming/sonarr-naming.json")
f i
standardNaming = $(echo "$namingJson" | jq -r '.episodes.standard."default:4"')
dailyNaming = $(echo "$namingJson" | jq -r '.episodes.daily."default:4"')
animeNaming = $(echo "$namingJson" | jq -r '.episodes.anime."default:4"')
seriesNaming = $(echo "$namingJson" | jq -r '.series.default')
seasonNaming = $(echo "$namingJson" | jq -r '.season.default')
l o g " U p d a t i n g S o n a r r F i l e N a m i n g . . . "
updateArr = $(curl -s "$arrUrl/api/v3/config/naming" -X PUT -H "Content-Type: application/json" -H "X-Api-Key: $arrApiKey" --data-raw "{
\ " r e n a m e E p i s o d e s \ " : t r u e ,
\ " r e p l a c e I l l e g a l C h a r a c t e r s \ " : t r u e ,
\ " m u l t i E p i s o d e S t y l e \ " : 5 ,
\ " s t a n d a r d E p i s o d e F o r m a t \ " : \ " $ s t a n d a r d N a m i n g \ " ,
\ " d a i l y E p i s o d e F o r m a t \ " : \ " $ d a i l y N a m i n g \ " ,
\ " a n i m e E p i s o d e F o r m a t \ " : \ " $ a n i m e N a m i n g \ " ,
\ " s e r i e s F o l d e r F o r m a t \ " : \ " $ s e r i e s N a m i n g \ " ,
\ " s e a s o n F o l d e r F o r m a t \ " : \ " $ s e a s o n N a m i n g \ " ,
\ " s p e c i a l s F o l d e r F o r m a t \ " : \ " $ s e a s o n N a m i n g \ " ,
\ " i n c l u d e S e r i e s T i t l e \ " : f a l s e ,
\ " i n c l u d e E p i s o d e T i t l e \ " : f a l s e ,
\ " i n c l u d e Q u a l i t y \ " : f a l s e ,
\ " r e p l a c e S p a c e s \ " : t r u e ,
\ " s e p a r a t o r \ " : \ " - \ " ,
\ " n u m b e r S t y l e \ " : \ " S { s e a s o n : 0 0 } E { e p i s o d e : 0 0 } \ " ,
\ " i d \ " : 1
} " )
l o g " C o m p l e t e "
l o g " U p d a t i n g S o n r r M e d i a M a n a g e m e n t . . . "
updateArr = $(curl -s "$arrUrl/api/v3/config/mediamanagement" -X PUT -H "Content-Type: application/json" -H "X-Api-Key: $arrApiKey" --data-raw '{"autoUnmonitorPreviouslyDownloadedEpisodes":false,
" r e c y c l e B i n " : " " ,
" r e c y c l e B i n C l e a n u p D a y s " : 7 ,
" d o w n l o a d P r o p e r s A n d R e p a c k s " : " d o N o t P r e f e r " ,
" c r e a t e E m p t y S e r i e s F o l d e r s " : f a l s e ,
" d e l e t e E m p t y F o l d e r s " : t r u e ,
2023-07-08 19:12:16 +02:00
" f i l e D a t e " : " u t c A i r D a t e " ,
" r e s c a n A f t e r R e f r e s h " : " n e v e r " ,
2023-07-08 18:38:28 +02:00
" s e t P e r m i s s i o n s L i n u x " : f a l s e ,
" c h m o d F o l d e r " : " 7 7 7 " ,
" c h o w n G r o u p " : " " ,
" e p i s o d e T i t l e R e q u i r e d " : " a l w a y s " ,
" s k i p F r e e S p a c e C h e c k W h e n I m p o r t i n g " : f a l s e ,
" m i n i m u m F r e e S p a c e W h e n I m p o r t i n g " : 1 0 0 ,
" c o p y U s i n g H a r d l i n k s " : t r u e ,
" i m p o r t E x t r a F i l e s " : t r u e ,
" e x t r a F i l e E x t e n s i o n s " : " s r t " ,
" e n a b l e M e d i a I n f o " : t r u e , " i d " : 1 } ' )
l o g " C o m p l e t e "
l o g " U p d a t i n g S o n a r r M e d a t a S e t t i n g s . . . "
updateArr = $(curl -s "$arrUrl/api/v3/metadata/1?" -X PUT -H "Content-Type: application/json" -H "X-Api-Key: $arrApiKey" --data-raw '{"enable":true,"name":"Kodi (XBMC) / Emby","fields":[{"name":"seriesMetadata","value":true},{"name":"seriesMetadataEpisodeGuide","value":true},{"name":"seriesMetadataUrl","value":false},{"name":"episodeMetadata","value":true},{"name":"seriesImages","value":true},{"name":"seasonImages","value":true},{"name":"episodeImages","value":true}],"implementationName":"Kodi (XBMC) / Emby","implementation":"XbmcMetadata","configContract":"XbmcMetadataSettings","infoLink":"https://wiki.servarr.com/sonarr/supported#xbmcmetadata","tags":[],"id":1}')
updateArr = $(curl -s "$arrUrl/api/v3/metadata/4?" -X PUT -H "Content-Type: application/json" -H "X-Api-Key: $arrApiKey" --data-raw '{"enable":true,"name":"Plex","fields":[{"name":"seriesPlexMatchFile","value":true}],"implementationName":"Plex","implementation":"PlexMetadata","configContract":"PlexMetadataSettings","infoLink":"https://wiki.servarr.com/sonarr/supported#plexmetadata","tags":[],"id":4}')
l o g " C o n f i g u r i n g S o n a r r C u s t o m S c r i p t s "
i f c u r l - s " $ a r r U r l / a p i / v 3 / n o t i f i c a t i o n " - H " X - A p i - K e y : $ { a r r A p i K e y } " | j q - r . [ ] . n a m e | g r e p " P l e x N o t i f y . b a s h " | r e a d ; then
l o g " P l e x N o t i f y . b a s h a l r e a d y a d d e d t o S o n a r r c u s t o m s c r i p t s "
e l s e
l o g " A d d i n g P l e x N o t i f y . b a s h t o S o n a r r c u s t o m s c r i p t s "
# Send a command to check file path, to prevent error with adding...
updateArr = $(curl -s "$arrUrl/api/v3/filesystem?path=%2Fconfig%2Fextended%2Fscripts%2FPlexNotify.bash&allowFoldersWithoutTrailingSlashes=true&includeFiles=true" -H "X-Api-Key: ${arrApiKey}")
# Add PlexNotify.bash
2023-07-08 18:42:26 +02:00
updateArr = $(curl -s "$arrUrl/api/v3/notification?" -X POST -H "Content-Type: application/json" -H "X-Api-Key: ${arrApiKey}" --data-raw '{"onGrab":false,"onDownload":true,"onUpgrade":true,"onRename":true,"onSeriesDelete":true,"onEpisodeFileDelete":true,"onEpisodeFileDeleteForUpgrade":true,"onHealthIssue":false,"onApplicationUpdate":false,"supportsOnGrab":true,"supportsOnDownload":true,"supportsOnUpgrade":true,"supportsOnRename":true,"supportsOnSeriesDelete":true,"supportsOnEpisodeFileDelete":true,"supportsOnEpisodeFileDeleteForUpgrade":true,"supportsOnHealthIssue":true,"supportsOnApplicationUpdate":true,"includeHealthWarnings":false,"name":"PlexNotify.bash","fields":[{"name":"path","value":"/config/extended/PlexNotify.bash"},{"name":"arguments"}],"implementationName":"Custom Script","implementation":"CustomScript","configContract":"CustomScriptSettings","infoLink":"https://wiki.servarr.com/sonarr/supported#customscript","message":{"message":"Testing will execute the script with the EventType set to Test, ensure your script handles this correctly","type":"warning"},"tags":[]}')
2023-07-08 18:38:28 +02:00
l o g " C o m p l e t e "
f i
i f c u r l - s " $ a r r U r l / a p i / v 3 / n o t i f i c a t i o n " - H " X - A p i - K e y : $ { a r r A p i K e y } " | j q - r . [ ] . n a m e | g r e p " D a i l y S e r i e s E p i s o d e T r i m m e r . b a s h " | r e a d ; then
l o g " D a i l y S e r i e s E p i s o d e T r i m m e r . b a s h a l r e a d y a d d e d t o S o n a r r c u s t o m s c r i p t s "
e l s e
l o g " A d d i n g D a i l y S e r i e s E p i s o d e T r i m m e r . b a s h t o S o n a r r c u s t o m s c r i p t s "
# Send a command to check file path, to prevent error with adding...
updateArr = $(curl -s "$arrUrl/api/v3/filesystem?path=%2Fconfig%2Fextended%2Fscripts%2FDailySeriesEpisodeTrimmer.bash&allowFoldersWithoutTrailingSlashes=true&includeFiles=true" -H "X-Api-Key: ${arrApiKey}")
# Add DailySeriesEpisodeTrimmer.bash
2023-07-08 18:42:26 +02:00
updateArr = $(curl -s "$arrUrl/api/v3/notification?" -X POST -H "Content-Type: application/json" -H "X-Api-Key: ${arrApiKey}" --data-raw '{"onGrab":false,"onDownload":true,"onUpgrade":true,"onRename":true,"onSeriesDelete":true,"onEpisodeFileDelete":false,"onEpisodeFileDeleteForUpgrade":false,"onHealthIssue":false,"onApplicationUpdate":false,"supportsOnGrab":true,"supportsOnDownload":true,"supportsOnUpgrade":true,"supportsOnRename":true,"supportsOnSeriesDelete":true,"supportsOnEpisodeFileDelete":true,"supportsOnEpisodeFileDeleteForUpgrade":true,"supportsOnHealthIssue":true,"supportsOnApplicationUpdate":true,"includeHealthWarnings":false,"name":"DailySeriesEpisodeTrimmer.bash","fields":[{"name":"path","value":"/config/extended/DailySeriesEpisodeTrimmer.bash"},{"name":"arguments"}],"implementationName":"Custom Script","implementation":"CustomScript","configContract":"CustomScriptSettings","infoLink":"https://wiki.servarr.com/sonarr/supported#customscript","message":{"message":"Testing will execute the script with the EventType set to Test, ensure your script handles this correctly","type":"warning"},"tags":[]}')
2023-07-08 18:38:28 +02:00
l o g " C o m p l e t e "
f i
i f c u r l - s " $ a r r U r l / a p i / v 3 / n o t i f i c a t i o n " - H " X - A p i - K e y : $ { a r r A p i K e y } " | j q - r . [ ] . n a m e | g r e p " E x t r a s . b a s h " | r e a d ; then
l o g " E x t r a s . b a s h a l r e a d y a d d e d t o S o n a r r c u s t o m s c r i p t s "
e l s e
l o g " A d d i n g E x t r a s . b a s h t o S o n a r r c u s t o m s c r i p t s "
# Send a command to check file path, to prevent error with adding...
updateArr = $(curl -s "$arrUrl/api/v3/filesystem?path=%2Fconfig%2Fextended%2Fscripts%2FExtras.bash&allowFoldersWithoutTrailingSlashes=true&includeFiles=true" -H "X-Api-Key: ${arrApiKey}")
# Add Extras.bash
2023-07-08 18:42:26 +02:00
updateArr = $(curl -s "$arrUrl/api/v3/notification?" -X POST -H "Content-Type: application/json" -H "X-Api-Key: ${arrApiKey}" --data-raw '{"onGrab":false,"onDownload":true,"onUpgrade":true,"onRename":true,"onSeriesDelete":false,"onEpisodeFileDelete":false,"onEpisodeFileDeleteForUpgrade":false,"onHealthIssue":false,"onApplicationUpdate":false,"supportsOnGrab":true,"supportsOnDownload":true,"supportsOnUpgrade":true,"supportsOnRename":true,"supportsOnSeriesDelete":false,"supportsOnEpisodeFileDelete":true,"supportsOnEpisodeFileDeleteForUpgrade":true,"supportsOnHealthIssue":true,"supportsOnApplicationUpdate":true,"includeHealthWarnings":false,"name":"Extras.bash","fields":[{"name":"path","value":"/config/extended/Extras.bash"},{"name":"arguments"}],"implementationName":"Custom Script","implementation":"CustomScript","configContract":"CustomScriptSettings","infoLink":"https://wiki.servarr.com/sonarr/supported#customscript","message":{"message":"Testing will execute the script with the EventType set to Test, ensure your script handles this correctly","type":"warning"},"tags":[]}')
2023-07-08 18:38:28 +02:00
l o g " C o m p l e t e "
f i
2023-07-15 22:52:14 +02:00
# Add Language: Not Original Custom Format
l o g " A d d i n g L a n g u a g e : N o t O r i g i n a l c u s t o m f o r m a t "
updateArr = $(curl -s "$arrUrl/api/v3/customformat?" -X POST -H "Content-Type: application/json" -H "X-Api-Key: ${arrApiKey}" --data-raw '{"includeCustomFormatWhenRenaming":false,"trash_id":"guide-only","trash_score":"-10000","trash_description":"Language: Original Only","name":"Language: Not Original","specifications":[{"implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":true,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":-2,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal"}],"name":"Not Original Language","id":2}]}')
l o g " C o m p l e t e "
2023-07-08 18:38:28 +02:00
s l e e p i n f i n i t y
e x i t $ ?