Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration for choosing to delete files #154

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/resources/config-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,6 @@

## Number of days to wait before deleting content from the arrs (Deleting must be enabled). DEFAULT: 7
# interval.days: 7

## Choose whether you want to delete the files as well, or only the entry in Sonarr/Radarr. DEFAULT: true
# deleteFiles: true
6 changes: 3 additions & 3 deletions src/main/scala/PlexTokenDeleteSync.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ object PlexTokenDeleteSync extends PlexUtils with SonarrUtils with RadarrUtils {

private def deleteMovie(client: HttpClient, config: Configuration)(movie: Item): EitherT[IO, Throwable, Unit] =
if (config.deleteConfiguration.movieDeleting) {
deleteFromRadarr(client, config.radarrConfiguration)(movie)
deleteFromRadarr(client, config.radarrConfiguration, config.deleteConfiguration.deleteFiles)(movie)
} else {
logger.info(s"Found movie \"${movie.title}\" which is not watchlisted on Plex")
EitherT.pure[IO, Throwable](())
}

private def deleteSeries(client: HttpClient, config: Configuration)(show: Item): EitherT[IO, Throwable, Unit] =
if (show.ended.contains(true) && config.deleteConfiguration.endedShowDeleting) {
deleteFromSonarr(client, config.sonarrConfiguration)(show)
deleteFromSonarr(client, config.sonarrConfiguration, config.deleteConfiguration.deleteFiles)(show)
} else if (show.ended.contains(false) && config.deleteConfiguration.continuingShowDeleting) {
deleteFromSonarr(client, config.sonarrConfiguration)(show)
deleteFromSonarr(client, config.sonarrConfiguration, config.deleteConfiguration.deleteFiles)(show)
} else {
logger.info(s"Found show \"${show.title}\" which is not watchlisted on Plex")
EitherT[IO, Throwable, Unit](IO.pure(Right(())))
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/configuration/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ case class DeleteConfiguration(
movieDeleting: Boolean,
endedShowDeleting: Boolean,
continuingShowDeleting: Boolean,
deleteInterval: FiniteDuration
deleteInterval: FiniteDuration,
deleteFiles: Boolean
)
1 change: 1 addition & 0 deletions src/main/scala/configuration/ConfigurationRedactor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object ConfigurationRedactor {
| endedShowDeleting: ${config.deleteConfiguration.endedShowDeleting}
| continuingShowDeleting: ${config.deleteConfiguration.continuingShowDeleting}
| deleteInterval: ${config.deleteConfiguration.deleteInterval.toDays} days
| deleteFiles: ${config.deleteConfiguration.deleteFiles}
|
|""".stripMargin
}
4 changes: 3 additions & 1 deletion src/main/scala/configuration/ConfigurationUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ object ConfigurationUtils {
.flatMap(_.toBooleanOption)
.getOrElse(false)
deleteInterval = configReader.getConfigOption(Keys.deleteIntervalDays).flatMap(_.toIntOption).getOrElse(7).days
deleteFiles = configReader.getConfigOption(Keys.deleteFiles).flatMap(_.toBooleanOption).getOrElse(true)
hasPlexPass = plexWatchlistUrls.nonEmpty
} yield Configuration(
if (hasPlexPass) refreshInterval else 19.minutes,
Expand Down Expand Up @@ -77,7 +78,8 @@ object ConfigurationUtils {
deleteMovies,
deleteEndedShows,
deleteContinuingShows,
deleteInterval
deleteInterval,
deleteFiles
)
)

Expand Down
1 change: 1 addition & 0 deletions src/main/scala/configuration/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ private[configuration] object Keys {
val deleteMovies = "delete.movie"
val deleteEndedShow = "delete.endedShow"
val deleteContinuingShow = "delete.continuingShow"
val deleteFiles = "delete.deleteFiles"
}
8 changes: 4 additions & 4 deletions src/main/scala/radarr/RadarrUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ trait RadarrUtils extends RadarrConversions {
}
}

protected def deleteFromRadarr(client: HttpClient, config: RadarrConfiguration)(
protected def deleteFromRadarr(client: HttpClient, config: RadarrConfiguration, deleteFiles: Boolean)(
item: Item
): EitherT[IO, Throwable, Unit] = {
val movieId = item.getRadarrId.getOrElse {
logger.warn(s"Unable to extract Radarr ID from movie to delete: $item")
0L
}

deleteToArr(client)(config.radarrBaseUrl, config.radarrApiKey, movieId)
deleteToArr(client)(config.radarrBaseUrl, config.radarrApiKey, movieId, deleteFiles)
.map { r =>
logger.info(s"Deleted ${item.title} from Radarr")
r
Expand All @@ -85,9 +85,9 @@ trait RadarrUtils extends RadarrConversions {
decoded <- EitherT.fromOption[IO](maybeDecoded.toOption, new Throwable("Unable to decode response from Radarr"))
} yield decoded

private def deleteToArr(client: HttpClient)(baseUrl: Uri, apiKey: String, id: Long): EitherT[IO, Throwable, Unit] = {
private def deleteToArr(client: HttpClient)(baseUrl: Uri, apiKey: String, id: Long, deleteFiles: Boolean): EitherT[IO, Throwable, Unit] = {
val urlWithQueryParams = (baseUrl / "api" / "v3" / "movie" / id)
.withQueryParam("deleteFiles", true)
.withQueryParam("deleteFiles", deleteFiles)
.withQueryParam("addImportExclusion", false)

EitherT(client.httpRequest(Method.DELETE, urlWithQueryParams, Some(apiKey)))
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/sonarr/SonarrUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ trait SonarrUtils extends SonarrConversions {
}
}

protected def deleteFromSonarr(client: HttpClient, config: SonarrConfiguration)(
protected def deleteFromSonarr(client: HttpClient, config: SonarrConfiguration, deleteFiles: Boolean)(
item: Item
): EitherT[IO, Throwable, Unit] = {
val showId = item.getSonarrId.getOrElse {
logger.warn(s"Unable to extract Sonarr ID from show to delete: $item")
0L
}

deleteToArr(client)(config.sonarrBaseUrl, config.sonarrApiKey, showId)
deleteToArr(client)(config.sonarrBaseUrl, config.sonarrApiKey, showId, deleteFiles)
.map { r =>
logger.info(s"Deleted ${item.title} from Sonarr")
r
}
}

private def deleteToArr(client: HttpClient)(baseUrl: Uri, apiKey: String, id: Long): EitherT[IO, Throwable, Unit] = {
private def deleteToArr(client: HttpClient)(baseUrl: Uri, apiKey: String, id: Long, deleteFiles: Boolean): EitherT[IO, Throwable, Unit] = {
val urlWithQueryParams = (baseUrl / "api" / "v3" / "series" / id)
.withQueryParam("deleteFiles", true)
.withQueryParam("deleteFiles", deleteFiles)
.withQueryParam("addImportListExclusion", false)

EitherT(client.httpRequest(Method.DELETE, urlWithQueryParams, Some(apiKey)))
Expand Down
3 changes: 2 additions & 1 deletion src/test/scala/PlexTokenSyncSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class PlexTokenSyncSpec extends AnyFlatSpec with Matchers with MockFactory {
movieDeleting = false,
endedShowDeleting = false,
continuingShowDeleting = false,
deleteInterval = 7.days
deleteInterval = 7.days,
deleteFiles = true
)
)

Expand Down
1 change: 1 addition & 0 deletions src/test/scala/configuration/ConfigurationUtilsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class ConfigurationUtilsSpec extends AnyFlatSpec with Matchers with MockFactory
(mockConfigReader.getConfigOption _).expects(Keys.deleteIntervalDays).returning(unset).anyNumberOfTimes()
(mockConfigReader.getConfigOption _).expects(Keys.sonarrTags).returning(tags).anyNumberOfTimes()
(mockConfigReader.getConfigOption _).expects(Keys.radarrTags).returning(tags).anyNumberOfTimes()
(mockConfigReader.getConfigOption _).expects(Keys.deleteFiles).returning(unset).anyNumberOfTimes()
nylonee marked this conversation as resolved.
Show resolved Hide resolved
mockConfigReader
}

Expand Down
Loading