Skip to content

Commit b34fcff

Browse files
committed
[Weekly-Episode-Deletion] Don't count not-yet-aired episodes in number to keep
1 parent 1caf097 commit b34fcff

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/main/kotlin/com/github/schaka/janitorr/cleanup/WeeklyEpisodeCleanupSchedule.kt

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import org.springframework.aot.hint.annotation.RegisterReflectionForBinding
1414
import org.springframework.cache.annotation.CacheEvict
1515
import org.springframework.scheduling.annotation.Scheduled
1616
import org.springframework.stereotype.Service
17+
import sun.jvm.hotspot.HelloWorld.e
18+
import java.time.LocalDate
1719
import java.time.LocalDateTime
1820

1921
/**
@@ -57,7 +59,11 @@ class WeeklyEpisodeCleanupSchedule(
5759

5860
for (show in series) {
5961
val latestSeason = show.seasons.maxBy { season -> season.seasonNumber }
60-
val episodes = sonarrClient.getAllEpisodes(show.id, latestSeason.seasonNumber).toMutableList()
62+
val episodes = sonarrClient.getAllEpisodes(show.id, latestSeason.seasonNumber)
63+
.filter { it.airDate != null }
64+
.filter { LocalDate.parse(it.airDate!!) <= today.toLocalDate() }
65+
.toMutableList()
66+
6167
val episodesHistory = sonarrClient.getHistory(show.id, latestSeason.seasonNumber)
6268
.sortedBy { parseDate(it.date)}
6369
.distinctBy { it.episodeId }
@@ -85,13 +91,12 @@ class WeeklyEpisodeCleanupSchedule(
8591
val leftoverEpisodes = episodes.sortedByDescending { it.episodeNumber }.take(applicationProperties.episodeDeletion.maxEpisodes)
8692
episodes.removeAll(leftoverEpisodes) // remove the most recent episodes from the list, as we want to keep those
8793

88-
for (episode in leftoverEpisodes) {
94+
for (episode in episodes) {
8995
log.trace("Deleting episode ${episode.episodeNumber} of ${show.title} S${latestSeason.seasonNumber} because there are too many episodes")
9096

9197
if (episode.episodeFileId != null && episode.episodeFileId != 0) {
9298
if (!applicationProperties.dryRun) {
9399
sonarrClient.deleteEpisodeFile(episode.episodeFileId)
94-
episodes.remove(episode)
95100
}
96101
}
97102
}

src/main/kotlin/com/github/schaka/janitorr/servarr/sonarr/episodes/EpisodeResponse.kt

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ data class EpisodeResponse(
1010
val folder: String?,
1111
val episodeFile: EpisodeFile?,
1212
val path: String?,
13+
val airDate: String?,
1314
val hasFile: Boolean
1415
)

0 commit comments

Comments
 (0)