Skip to content

Commit

Permalink
Dry-run of plex deletes using token
Browse files Browse the repository at this point in the history
  • Loading branch information
nylonee committed Nov 22, 2023
1 parent 8573055 commit 914cf64
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/main/scala/PlexTokenDeleteSync.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import cats.data.EitherT
import cats.effect.IO
import cats.implicits._
import configuration.Configuration
import http.HttpClient
import model.Item
import org.slf4j.LoggerFactory
import plex.PlexUtils
import radarr.RadarrUtils
import sonarr.SonarrUtils

object PlexTokenDeleteSync extends PlexUtils with SonarrUtils with RadarrUtils {

private val logger = LoggerFactory.getLogger(getClass)

def run(config: Configuration, client: HttpClient): IO[Unit] = {
val result = for {
selfWatchlist <- getSelfWatchlist(config, client)
othersWatchlist <- getOthersWatchlist(config, client)
moviesWithoutExclusions <- fetchMovies(client)(config.radarrApiKey, config.radarrBaseUrl, bypass = true)
seriesWithoutExclusions <- fetchSeries(client)(config.sonarrApiKey, config.sonarrBaseUrl, bypass = true)
allIdsWithoutExclusions = moviesWithoutExclusions ++ seriesWithoutExclusions
_ <- missingIdsOnPlex(client)(config)(allIdsWithoutExclusions, selfWatchlist ++ othersWatchlist)
} yield ()

result.leftMap {
err =>
logger.warn(s"An error occurred: $err")
err
}.value.map(_.getOrElse(()))
}

private def missingIdsOnPlex(client: HttpClient)(config: Configuration)(existingItems: Set[Item], watchlist: Set[Item]): EitherT[IO, Throwable, Set[Unit]] = {
for {
item <- existingItems
maybeExistingItem = watchlist.exists(_.matches(item))
category = item.category
task = EitherT.fromEither[IO]((maybeExistingItem, category) match {
case (true, c) =>
logger.debug(s"$c \"${item.title}\" already exists in Plex")
Right(IO.unit)
case (false, "show") =>
logger.debug(s"Found show \"${item.title}\" which does not exist on Plex")
Right(IO.unit)
case (false, "movie") =>
logger.debug(s"Found movie \"${item.title}\" which does not exist yet in Radarr")
Right(IO.unit)
case (false, c) =>
logger.warn(s"Found $c \"${item.title}\", but I don't recognize the category")
Left(new Throwable(s"Unknown category $c"))
})
} yield task.flatMap(EitherT.liftF[IO, Throwable, Unit])
}.toList.sequence.map(_.toSet)

}
13 changes: 11 additions & 2 deletions src/main/scala/Server.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import cats.effect._
import cats.implicits.catsSyntaxTuple3Parallel
import cats.implicits.catsSyntaxTuple4Parallel
import configuration.{Configuration, ConfigurationUtils, SystemPropertyReader}
import http.HttpClient
import org.slf4j.LoggerFactory
Expand All @@ -26,7 +26,8 @@ object Server extends IOApp {
result <- (
watchlistSync(memoizedConfigIo, httpClient),
pingTokenSync(memoizedConfigIo, httpClient),
plexTokenSync(memoizedConfigIo, httpClient)
plexTokenSync(memoizedConfigIo, httpClient),
plexTokenDeleteSync(memoizedConfigIo, httpClient)
).parTupled.as(ExitCode.Success)
} yield result
}
Expand All @@ -52,4 +53,12 @@ object Server extends IOApp {
config <- configIO
_ <- PlexTokenSync.run(config, httpClient)
} yield ()

private def plexTokenDeleteSync(configIO: IO[Configuration], httpClient: HttpClient): IO[Unit] =
for {
config <- configIO
_ <- PlexTokenDeleteSync.run(config, httpClient)
_ <- IO.sleep(7.days)
_ <- plexTokenDeleteSync(configIO, httpClient)
} yield ()
}

0 comments on commit 914cf64

Please sign in to comment.