Skip to content

Commit

Permalink
[Sonarr] Clean up empty shows
Browse files Browse the repository at this point in the history
  • Loading branch information
Schaka committed Feb 21, 2024
1 parent ef91aa8 commit f1f7089
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.schaka.janitorr.FileSystemProperties
import com.github.schaka.janitorr.servarr.LibraryItem
import com.github.schaka.janitorr.servarr.ServarrService
import com.github.schaka.janitorr.servarr.radarr.RadarrService
import com.github.schaka.janitorr.servarr.sonarr.series.SeriesPayload
import jakarta.annotation.PostConstruct
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
Expand Down Expand Up @@ -102,7 +103,10 @@ class SonarrService(
}
}

// TODO: Clean up entire show, if all seasons are unmonitored AND no files are available
// We could be more efficient and do this when grabbing the series in unmonitorSeason anyway, but more clear cut code should be better here
val affectedShows = items.map { it.id }.distinct().map { sonarrClient.getSeries(it) }
deleteEmptyShows(affectedShows)

}

private fun unmonitorSeason(seriesId: Int, seasonNumber: Int) {
Expand All @@ -116,4 +120,30 @@ class SonarrService(
log.info("Unmonitoring {} - season {}", series.title, seasonToEdit.seasonNumber)
}
}

// If all seasons are unmonitored or don't have any files, we delete them
// If the latest season is monitored, we have to assume the intention is to grab future episodes on release and not delete anything
private fun deleteEmptyShows(affectedShows: List<SeriesPayload>) {

for (show in affectedShows) {
val latestSeason = show.seasons.maxBy { it.seasonNumber }
if (latestSeason.monitored) {
continue
}

val allUnmonitored = show.seasons.all { !it.monitored }
if (!allUnmonitored) {
continue
}

val noFiles = show.seasons.all {
val filesForSeason = sonarrClient.getAllEpisodes(show.id, it.seasonNumber)
filesForSeason.none { ep -> ep.hasFile }
}

if (noFiles) {
sonarrClient.deleteSeries(show.id, true)
}
}
}
}

0 comments on commit f1f7089

Please sign in to comment.