Skip to content

Commit

Permalink
scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nylonee committed Apr 9, 2024
1 parent f09d1c2 commit 5ed0683
Show file tree
Hide file tree
Showing 26 changed files with 1,028 additions and 636 deletions.
37 changes: 26 additions & 11 deletions src/main/scala/PlexTokenDeleteSync.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,39 @@ object PlexTokenDeleteSync extends PlexUtils with SonarrUtils with RadarrUtils {
def run(config: Configuration, client: HttpClient): IO[Unit] = {
val result = for {
selfWatchlist <- getSelfWatchlist(config.plexConfiguration, client)
othersWatchlist <- if (config.plexConfiguration.skipFriendSync) EitherT.pure[IO, Throwable](Set.empty[Item]) else getOthersWatchlist(config.plexConfiguration, client)
watchlistDatas <- EitherT[IO, Throwable, List[Set[Item]]](config.plexConfiguration.plexWatchlistUrls.map(fetchWatchlistFromRss(client)).toList.sequence.map(Right(_)))
othersWatchlist <-
if (config.plexConfiguration.skipFriendSync) EitherT.pure[IO, Throwable](Set.empty[Item])
else getOthersWatchlist(config.plexConfiguration, client)
watchlistDatas <- EitherT[IO, Throwable, List[Set[Item]]](
config.plexConfiguration.plexWatchlistUrls.map(fetchWatchlistFromRss(client)).toList.sequence.map(Right(_))
)
watchlistData = watchlistDatas.flatten.toSet
moviesWithoutExclusions <- fetchMovies(client)(config.radarrConfiguration.radarrApiKey, config.radarrConfiguration.radarrBaseUrl, bypass = true)
seriesWithoutExclusions <- fetchSeries(client)(config.sonarrConfiguration.sonarrApiKey, config.sonarrConfiguration.sonarrBaseUrl, bypass = true)
moviesWithoutExclusions <- fetchMovies(client)(
config.radarrConfiguration.radarrApiKey,
config.radarrConfiguration.radarrBaseUrl,
bypass = true
)
seriesWithoutExclusions <- fetchSeries(client)(
config.sonarrConfiguration.sonarrApiKey,
config.sonarrConfiguration.sonarrBaseUrl,
bypass = true
)
allIdsWithoutExclusions = moviesWithoutExclusions ++ seriesWithoutExclusions
_ <- missingIdsOnPlex(client)(config)(allIdsWithoutExclusions, selfWatchlist ++ othersWatchlist ++ watchlistData)
} yield ()

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

private def missingIdsOnPlex(client: HttpClient)(config: Configuration)(existingItems: Set[Item], watchlist: Set[Item]): EitherT[IO, Throwable, Set[Unit]] = {
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))
Expand All @@ -53,13 +69,13 @@ 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)(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] = {
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)
} else if (show.ended.contains(false) && config.deleteConfiguration.continuingShowDeleting) {
Expand All @@ -68,6 +84,5 @@ object PlexTokenDeleteSync extends PlexUtils with SonarrUtils with RadarrUtils {
logger.info(s"Found show \"${show.title}\" which is not watchlisted on Plex")
EitherT[IO, Throwable, Unit](IO.pure(Right(())))
}
}

}
59 changes: 40 additions & 19 deletions src/main/scala/PlexTokenSync.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,54 @@ object PlexTokenSync extends PlexUtils with SonarrUtils with RadarrUtils {
val runTokenSync = firstRun || !config.plexConfiguration.hasPlexPass

val result = for {
selfWatchlist <- if (runTokenSync)
getSelfWatchlist(config.plexConfiguration, client)
else
EitherT.pure[IO, Throwable](Set.empty[Item])
selfWatchlist <-
if (runTokenSync)
getSelfWatchlist(config.plexConfiguration, client)
else
EitherT.pure[IO, Throwable](Set.empty[Item])
_ = if (runTokenSync)
logger.info(s"Found ${selfWatchlist.size} items on user's watchlist using the plex token")
othersWatchlist <- if (config.plexConfiguration.skipFriendSync || !runTokenSync)
EitherT.pure[IO, Throwable](Set.empty[Item])
else
getOthersWatchlist(config.plexConfiguration, client)
watchlistDatas <- EitherT[IO, Throwable, List[Set[Item]]](config.plexConfiguration.plexWatchlistUrls.map(fetchWatchlistFromRss(client)).toList.sequence.map(Right(_)))
othersWatchlist <-
if (config.plexConfiguration.skipFriendSync || !runTokenSync)
EitherT.pure[IO, Throwable](Set.empty[Item])
else
getOthersWatchlist(config.plexConfiguration, client)
watchlistDatas <- EitherT[IO, Throwable, List[Set[Item]]](
config.plexConfiguration.plexWatchlistUrls.map(fetchWatchlistFromRss(client)).toList.sequence.map(Right(_))
)
watchlistData = watchlistDatas.flatten.toSet
_ = if (runTokenSync) logger.info(s"Found ${othersWatchlist.size} items on other available watchlists using the plex token")
movies <- fetchMovies(client)(config.radarrConfiguration.radarrApiKey, config.radarrConfiguration.radarrBaseUrl, config.radarrConfiguration.radarrBypassIgnored)
series <- fetchSeries(client)(config.sonarrConfiguration.sonarrApiKey, config.sonarrConfiguration.sonarrBaseUrl, config.sonarrConfiguration.sonarrBypassIgnored)
_ = if (runTokenSync)
logger.info(s"Found ${othersWatchlist.size} items on other available watchlists using the plex token")
movies <- fetchMovies(client)(
config.radarrConfiguration.radarrApiKey,
config.radarrConfiguration.radarrBaseUrl,
config.radarrConfiguration.radarrBypassIgnored
)
series <- fetchSeries(client)(
config.sonarrConfiguration.sonarrApiKey,
config.sonarrConfiguration.sonarrBaseUrl,
config.sonarrConfiguration.sonarrBypassIgnored
)
allIds = movies ++ series
_ <- missingIds(client)(config)(allIds, selfWatchlist ++ othersWatchlist ++ watchlistData)
} yield ()

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

private def missingIds(client: HttpClient)(config: Configuration)(existingItems: Set[Item], watchlist: Set[Item]): EitherT[IO, Throwable, Set[Unit]] = {
private def missingIds(
client: HttpClient
)(config: Configuration)(existingItems: Set[Item], watchlist: Set[Item]): EitherT[IO, Throwable, Set[Unit]] = {
for {
watchlistedItem <- watchlist
maybeExistingItem = existingItems.exists(_.matches(watchlistedItem))
category = watchlistedItem.category
category = watchlistedItem.category
task = EitherT.fromEither[IO]((maybeExistingItem, category) match {
case (true, c) =>
logger.debug(s"$c \"${watchlistedItem.title}\" already exists in Sonarr/Radarr")
Expand All @@ -58,15 +75,19 @@ object PlexTokenSync extends PlexUtils with SonarrUtils with RadarrUtils {
logger.debug(s"Found show \"${watchlistedItem.title}\" which does not exist yet in Sonarr")
Right(addToSonarr(client)(config.sonarrConfiguration)(watchlistedItem))
} else {
logger.debug(s"Found show \"${watchlistedItem.title}\" which does not exist yet in Sonarr, but we do not have the tvdb ID so will skip adding")
logger.debug(
s"Found show \"${watchlistedItem.title}\" which does not exist yet in Sonarr, but we do not have the tvdb ID so will skip adding"
)
Right(IO.unit)
}
case (false, "movie") =>
if (watchlistedItem.getTmdbId.isDefined) {
logger.debug(s"Found movie \"${watchlistedItem.title}\" which does not exist yet in Radarr")
Right(addToRadarr(client)(config.radarrConfiguration)(watchlistedItem))
} else {
logger.debug(s"Found movie \"${watchlistedItem.title}\" which does not exist yet in Radarr, but we do not have the tmdb ID so will skip adding")
logger.debug(
s"Found movie \"${watchlistedItem.title}\" which does not exist yet in Radarr, but we do not have the tmdb ID so will skip adding"
)
Right(IO.unit)
}

Expand Down
31 changes: 17 additions & 14 deletions src/main/scala/Server.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import cats.effect._
import cats.implicits.catsSyntaxTuple3Parallel
import configuration.{Configuration, ConfigurationUtils, FileAndSystemPropertyReader, SystemPropertyReader}
Expand All @@ -14,16 +13,16 @@ object Server extends IOApp {

override protected def reportFailure(err: Throwable): IO[Unit] = err match {
case _: ClosedChannelException => IO.pure(logger.debug("Suppressing ClosedChannelException error", err))
case _ => IO.pure(logger.error("Failure caught and handled by IOApp", err))
case _ => IO.pure(logger.error("Failure caught and handled by IOApp", err))
}

def run(args: List[String]): IO[ExitCode] = {
val configReader = FileAndSystemPropertyReader
val httpClient = new HttpClient
val httpClient = new HttpClient

for {
initialConfig <- ConfigurationUtils.create(configReader, httpClient)
configRef <- Ref.of[IO, Configuration](initialConfig)
configRef <- Ref.of[IO, Configuration](initialConfig)
result <- (
pingTokenSync(configRef, httpClient),
plexTokenSync(configRef, httpClient),
Expand All @@ -38,24 +37,28 @@ object Server extends IOApp {
private def pingTokenSync(configRef: Ref[IO, Configuration], httpClient: HttpClient): IO[Unit] =
for {
config <- fetchLatestConfig(configRef)
_ <- PingTokenSync.run(config, httpClient)
_ <- IO.sleep(24.hours)
_ <- pingTokenSync(configRef, httpClient)
_ <- PingTokenSync.run(config, httpClient)
_ <- IO.sleep(24.hours)
_ <- pingTokenSync(configRef, httpClient)
} yield ()

private def plexTokenSync(configRef: Ref[IO, Configuration], httpClient: HttpClient, firstRun: Boolean = true): IO[Unit] =
private def plexTokenSync(
configRef: Ref[IO, Configuration],
httpClient: HttpClient,
firstRun: Boolean = true
): IO[Unit] =
for {
config <- fetchLatestConfig(configRef)
_ <- PlexTokenSync.run(config, httpClient, firstRun)
_ <- IO.sleep(config.refreshInterval)
_ <- plexTokenSync(configRef, httpClient, firstRun = false)
_ <- PlexTokenSync.run(config, httpClient, firstRun)
_ <- IO.sleep(config.refreshInterval)
_ <- plexTokenSync(configRef, httpClient, firstRun = false)
} yield ()

private def plexTokenDeleteSync(configRef: Ref[IO, Configuration], httpClient: HttpClient): IO[Unit] =
for {
config <- fetchLatestConfig(configRef)
_ <- PlexTokenDeleteSync.run(config, httpClient)
_ <- IO.sleep(config.deleteConfiguration.deleteInterval)
_ <- plexTokenDeleteSync(configRef, httpClient)
_ <- PlexTokenDeleteSync.run(config, httpClient)
_ <- IO.sleep(config.deleteConfiguration.deleteInterval)
_ <- plexTokenDeleteSync(configRef, httpClient)
} yield ()
}
64 changes: 32 additions & 32 deletions src/main/scala/configuration/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,43 @@ import org.http4s.Uri
import scala.concurrent.duration.FiniteDuration

case class Configuration(
refreshInterval: FiniteDuration,
sonarrConfiguration: SonarrConfiguration,
radarrConfiguration: RadarrConfiguration,
plexConfiguration: PlexConfiguration,
deleteConfiguration: DeleteConfiguration
)
refreshInterval: FiniteDuration,
sonarrConfiguration: SonarrConfiguration,
radarrConfiguration: RadarrConfiguration,
plexConfiguration: PlexConfiguration,
deleteConfiguration: DeleteConfiguration
)

case class SonarrConfiguration(
sonarrBaseUrl: Uri,
sonarrApiKey: String,
sonarrQualityProfileId: Int,
sonarrRootFolder: String,
sonarrBypassIgnored: Boolean,
sonarrSeasonMonitoring: String,
sonarrLanguageProfileId: Int,
sonarrTagIds: Set[Int]
)
sonarrBaseUrl: Uri,
sonarrApiKey: String,
sonarrQualityProfileId: Int,
sonarrRootFolder: String,
sonarrBypassIgnored: Boolean,
sonarrSeasonMonitoring: String,
sonarrLanguageProfileId: Int,
sonarrTagIds: Set[Int]
)

case class RadarrConfiguration(
radarrBaseUrl: Uri,
radarrApiKey: String,
radarrQualityProfileId: Int,
radarrRootFolder: String,
radarrBypassIgnored: Boolean,
radarrTagIds: Set[Int]
)
radarrBaseUrl: Uri,
radarrApiKey: String,
radarrQualityProfileId: Int,
radarrRootFolder: String,
radarrBypassIgnored: Boolean,
radarrTagIds: Set[Int]
)

case class PlexConfiguration(
plexWatchlistUrls: Set[Uri],
plexTokens: Set[String],
skipFriendSync: Boolean,
hasPlexPass: Boolean
)
plexWatchlistUrls: Set[Uri],
plexTokens: Set[String],
skipFriendSync: Boolean,
hasPlexPass: Boolean
)

case class DeleteConfiguration(
movieDeleting: Boolean,
endedShowDeleting: Boolean,
continuingShowDeleting: Boolean,
deleteInterval: FiniteDuration
)
movieDeleting: Boolean,
endedShowDeleting: Boolean,
continuingShowDeleting: Boolean,
deleteInterval: FiniteDuration
)
3 changes: 1 addition & 2 deletions src/main/scala/configuration/ConfigurationRedactor.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package configuration

object ConfigurationRedactor {
def redactToString(config: Configuration): String = {
def redactToString(config: Configuration): String =
s"""
|Configuration:
| refreshInterval: ${config.refreshInterval.toSeconds} seconds
Expand Down Expand Up @@ -36,5 +36,4 @@ object ConfigurationRedactor {
| deleteInterval: ${config.deleteConfiguration.deleteInterval.toDays} days
|
|""".stripMargin
}
}
Loading

0 comments on commit 5ed0683

Please sign in to comment.