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

Merge RSS and token sync #91

Merged
merged 5 commits into from
Feb 13, 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
1 change: 0 additions & 1 deletion src/main/scala/PlexTokenDeleteSync.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import WatchlistSync.fetchWatchlistFromRss
import cats.data.EitherT
import cats.effect.IO
import cats.implicits._
Expand Down
13 changes: 9 additions & 4 deletions src/main/scala/PlexTokenSync.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ object PlexTokenSync extends PlexUtils with SonarrUtils with RadarrUtils {

private val logger = LoggerFactory.getLogger(getClass)

def run(config: Configuration, client: HttpClient): IO[Unit] = {
def run(config: Configuration, client: HttpClient, firstRun: Boolean): IO[Unit] = {
val result = for {
selfWatchlist <- getSelfWatchlist(config.plexConfiguration, client)
selfWatchlist <- if (firstRun)
getSelfWatchlist(config.plexConfiguration, client)
else
EitherT.pure[IO, Throwable](Set.empty[Item])
_ = logger.info(s"Found ${selfWatchlist.size} items on user's watchlist using the plex token")
othersWatchlist <- if (config.plexConfiguration.skipFriendSync)
othersWatchlist <- if (!firstRun && 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
_ = 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)
_ <- missingIds(client)(config)(allIds, selfWatchlist ++ othersWatchlist ++ watchlistData)
} yield ()

result.leftMap {
Expand Down
17 changes: 5 additions & 12 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.catsSyntaxTuple4Parallel
import cats.implicits.catsSyntaxTuple3Parallel
import configuration.{Configuration, ConfigurationUtils, SystemPropertyReader}
import http.HttpClient
import org.slf4j.LoggerFactory
Expand All @@ -24,22 +24,13 @@ object Server extends IOApp {
for {
memoizedConfigIo <- ConfigurationUtils.create(configReader, httpClient).memoize
result <- (
watchlistSync(memoizedConfigIo, httpClient),
pingTokenSync(memoizedConfigIo, httpClient),
plexTokenSync(memoizedConfigIo, httpClient),
plexTokenDeleteSync(memoizedConfigIo, httpClient)
).parTupled.as(ExitCode.Success)
} yield result
}

private def watchlistSync(configIO: IO[Configuration], httpClient: HttpClient): IO[Unit] =
for {
config <- configIO
_ <- WatchlistSync.run(config, httpClient)
_ <- IO.sleep(config.refreshInterval)
_ <- watchlistSync(configIO, httpClient)
} yield ()

private def pingTokenSync(configIO: IO[Configuration], httpClient: HttpClient): IO[Unit] =
for {
config <- configIO
Expand All @@ -48,10 +39,12 @@ object Server extends IOApp {
_ <- pingTokenSync(configIO, httpClient)
} yield ()

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

private def plexTokenDeleteSync(configIO: IO[Configuration], httpClient: HttpClient): IO[Unit] =
Expand Down
70 changes: 0 additions & 70 deletions src/main/scala/WatchlistSync.scala

This file was deleted.

4 changes: 2 additions & 2 deletions src/test/scala/PlexTokenSyncSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PlexTokenSyncSpec extends AnyFlatSpec with Matchers with MockFactory {
defaultRadarrMock(mockHttpClient)
defaultSonarrMock(mockHttpClient)

val sync: Unit = PlexTokenSync.run(config, mockHttpClient).unsafeRunSync()
val sync: Unit = PlexTokenSync.run(config, mockHttpClient, firstRun = true).unsafeRunSync()

sync shouldBe ()
}
Expand All @@ -51,7 +51,7 @@ class PlexTokenSyncSpec extends AnyFlatSpec with Matchers with MockFactory {
radarrBypassIgnored = false
),
PlexConfiguration(
plexWatchlistUrls = Set(Uri.unsafeFromString("https://localhost:9090")),
plexWatchlistUrls = Set(),
plexTokens = plexTokens,
skipFriendSync = false
),
Expand Down
Loading
Loading