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

Dry-run of plex deletes using token #47

Merged
merged 5 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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.info(s"Found show \"${item.title}\" which is not on Plex")
Right(IO.unit)
case (false, "movie") =>
logger.info(s"Found movie \"${item.title}\" which is not on Plex")
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 ()
}
4 changes: 2 additions & 2 deletions src/test/scala/model/ItemSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class ItemSpec extends AnyFlatSpec with Matchers {

it should "succeed if one of the guids match" in {
val item1 = Item(punch, List(punch, punch), punch)
val item2 = item1.copy(guids = List(item1.guids.head))
val item3 = item1.copy(guids = List(item1.guids.last))
val item2 = item1.copy(guids = List(punch, item1.guids.head, punch))
val item3 = item1.copy(guids = List(punch, item1.guids.last, punch))

item1.matches(item2) shouldBe true
item2.matches(item1) shouldBe true
Expand Down
Loading