Skip to content

Commit 69b2643

Browse files
authored
Merge RSS and token sync, fix docker environments with spaces in env variables (#91)
1 parent 9c9c423 commit 69b2643

7 files changed

+43
-421
lines changed

docker/entrypoint.sh

+23-24
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,86 @@
11
#!/bin/bash
2+
CMD=("/app/bin/watchlistarr")
23

3-
CMD="/app/bin/watchlistarr"
4-
5-
JAVA_OPTS="-Xmx100m"
4+
JAVA_OPTS=(-Xmx100m)
65

76
if [ -n "$SONARR_API_KEY" ]; then
8-
CMD="$CMD -Dsonarr.apikey=$SONARR_API_KEY"
7+
CMD+=("-Dsonarr.apikey=$SONARR_API_KEY")
98
fi
109

1110
if [ -n "$SONARR_BASE_URL" ]; then
12-
CMD="$CMD -Dsonarr.baseUrl=$SONARR_BASE_URL"
11+
CMD+=("-Dsonarr.baseUrl=$SONARR_BASE_URL")
1312
fi
1413

1514
if [ -n "$SONARR_QUALITY_PROFILE" ]; then
16-
CMD="$CMD -Dsonarr.qualityProfile=$SONARR_QUALITY_PROFILE"
15+
CMD+=("-Dsonarr.qualityProfile=$SONARR_QUALITY_PROFILE")
1716
fi
1817

1918
if [ -n "$SONARR_ROOT_FOLDER" ]; then
20-
CMD="$CMD -Dsonarr.rootFolder=$SONARR_ROOT_FOLDER"
19+
CMD+=("-Dsonarr.rootFolder=$SONARR_ROOT_FOLDER")
2120
fi
2221

2322
if [ -n "$RADARR_API_KEY" ]; then
24-
CMD="$CMD -Dradarr.apikey=$RADARR_API_KEY"
23+
CMD+=("-Dradarr.apikey=$RADARR_API_KEY")
2524
fi
2625

2726
if [ -n "$RADARR_BASE_URL" ]; then
28-
CMD="$CMD -Dradarr.baseUrl=$RADARR_BASE_URL"
27+
CMD+=("-Dradarr.baseUrl=$RADARR_BASE_URL")
2928
fi
3029

3130
if [ -n "$RADARR_QUALITY_PROFILE" ]; then
32-
CMD="$CMD -Dradarr.qualityProfile=$RADARR_QUALITY_PROFILE"
31+
CMD+=("-Dradarr.qualityProfile=$RADARR_QUALITY_PROFILE")
3332
fi
3433

3534
if [ -n "$RADARR_ROOT_FOLDER" ]; then
36-
CMD="$CMD -Dradarr.rootFolder=$RADARR_ROOT_FOLDER"
35+
CMD+=("-Dradarr.rootFolder=$RADARR_ROOT_FOLDER")
3736
fi
3837

3938
if [ -n "$PLEX_WATCHLIST_URL_1" ]; then
40-
CMD="$CMD -Dplex.watchlist1=$PLEX_WATCHLIST_URL_1"
39+
CMD+=("-Dplex.watchlist1=$PLEX_WATCHLIST_URL_1")
4140
fi
4241

4342
if [ -n "$PLEX_WATCHLIST_URL_2" ]; then
44-
CMD="$CMD -Dplex.watchlist2=$PLEX_WATCHLIST_URL_2"
43+
CMD+=("-Dplex.watchlist2=$PLEX_WATCHLIST_URL_2")
4544
fi
4645

4746
if [ -n "$REFRESH_INTERVAL_SECONDS" ]; then
48-
CMD="$CMD -Dinterval.seconds=$REFRESH_INTERVAL_SECONDS"
47+
CMD+=("-Dinterval.seconds=$REFRESH_INTERVAL_SECONDS")
4948
fi
5049

5150
if [ -n "$SONARR_BYPASS_IGNORED" ]; then
52-
CMD="$CMD -Dsonarr.bypassIgnored=$SONARR_BYPASS_IGNORED"
51+
CMD+=("-Dsonarr.bypassIgnored=$SONARR_BYPASS_IGNORED")
5352
fi
5453

5554
if [ -n "$RADARR_BYPASS_IGNORED" ]; then
56-
CMD="$CMD -Dradarr.bypassIgnored=$RADARR_BYPASS_IGNORED"
55+
CMD+=("-Dradarr.bypassIgnored=$RADARR_BYPASS_IGNORED")
5756
fi
5857

5958
if [ -n "$SONARR_SEASON_MONITORING" ]; then
60-
CMD="$CMD -Dsonarr.seasonMonitoring=$SONARR_SEASON_MONITORING"
59+
CMD+=("-Dsonarr.seasonMonitoring=$SONARR_SEASON_MONITORING")
6160
fi
6261

6362
if [ -n "$PLEX_TOKEN" ]; then
64-
CMD="$CMD -Dplex.token=$PLEX_TOKEN"
63+
CMD+=("-Dplex.token=$PLEX_TOKEN")
6564
fi
6665

6766
if [ -n "$SKIP_FRIEND_SYNC" ]; then
68-
CMD="$CMD -Dplex.skipfriendsync=$SKIP_FRIEND_SYNC"
67+
CMD+=("-Dplex.skipfriendsync=$SKIP_FRIEND_SYNC")
6968
fi
7069

7170
if [ -n "$ALLOW_MOVIE_DELETING" ]; then
72-
CMD="$CMD -Ddelete.movie=$ALLOW_MOVIE_DELETING"
71+
CMD+=("-Ddelete.movie=$ALLOW_MOVIE_DELETING")
7372
fi
7473

7574
if [ -n "$ALLOW_ENDED_SHOW_DELETING" ]; then
76-
CMD="$CMD -Ddelete.endedShow=$ALLOW_ENDED_SHOW_DELETING"
75+
CMD+=("-Ddelete.endedShow=$ALLOW_ENDED_SHOW_DELETING")
7776
fi
7877

7978
if [ -n "$ALLOW_CONTINUING_SHOW_DELETING" ]; then
80-
CMD="$CMD -Ddelete.continuingShow=$ALLOW_CONTINUING_SHOW_DELETING"
79+
CMD+=("-Ddelete.continuingShow=$ALLOW_CONTINUING_SHOW_DELETING")
8180
fi
8281

8382
if [ -n "$DELETE_INTERVAL_DAYS" ]; then
84-
CMD="$CMD -Ddelete.interval.days=$DELETE_INTERVAL_DAYS"
83+
CMD+=("-Ddelete.interval.days=$DELETE_INTERVAL_DAYS")
8584
fi
8685

87-
exec $CMD $JAVA_OPTS
86+
exec "${CMD[@]}" "${JAVA_OPTS[@]}"

src/main/scala/PlexTokenDeleteSync.scala

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import WatchlistSync.fetchWatchlistFromRss
21
import cats.data.EitherT
32
import cats.effect.IO
43
import cats.implicits._

src/main/scala/PlexTokenSync.scala

+13-8
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@ object PlexTokenSync extends PlexUtils with SonarrUtils with RadarrUtils {
1313

1414
private val logger = LoggerFactory.getLogger(getClass)
1515

16-
def run(config: Configuration, client: HttpClient): IO[Unit] = {
16+
def run(config: Configuration, client: HttpClient, firstRun: Boolean): IO[Unit] = {
1717
val result = for {
18-
selfWatchlist <- getSelfWatchlist(config.plexConfiguration, client)
19-
_ = logger.info(s"Found ${selfWatchlist.size} items on user's watchlist using the plex token")
20-
othersWatchlist <- if (config.plexConfiguration.skipFriendSync)
18+
selfWatchlist <- if (firstRun)
19+
getSelfWatchlist(config.plexConfiguration, client)
20+
else
21+
EitherT.pure[IO, Throwable](Set.empty[Item])
22+
_ = if (firstRun) logger.info(s"Found ${selfWatchlist.size} items on user's watchlist using the plex token")
23+
othersWatchlist <- if (!firstRun || config.plexConfiguration.skipFriendSync)
2124
EitherT.pure[IO, Throwable](Set.empty[Item])
2225
else
2326
getOthersWatchlist(config.plexConfiguration, client)
24-
_ = logger.info(s"Found ${othersWatchlist.size} items on other available watchlists using the plex token")
27+
watchlistDatas <- EitherT[IO, Throwable, List[Set[Item]]](config.plexConfiguration.plexWatchlistUrls.map(fetchWatchlistFromRss(client)).toList.sequence.map(Right(_)))
28+
watchlistData = watchlistDatas.flatten.toSet
29+
_ = if (firstRun) logger.info(s"Found ${othersWatchlist.size} items on other available watchlists using the plex token")
2530
movies <- fetchMovies(client)(config.radarrConfiguration.radarrApiKey, config.radarrConfiguration.radarrBaseUrl, config.radarrConfiguration.radarrBypassIgnored)
2631
series <- fetchSeries(client)(config.sonarrConfiguration.sonarrApiKey, config.sonarrConfiguration.sonarrBaseUrl, config.sonarrConfiguration.sonarrBypassIgnored)
2732
allIds = movies ++ series
28-
_ <- missingIds(client)(config)(allIds, selfWatchlist ++ othersWatchlist)
33+
_ <- missingIds(client)(config)(allIds, selfWatchlist ++ othersWatchlist ++ watchlistData)
2934
} yield ()
3035

3136
result.leftMap {
@@ -50,15 +55,15 @@ object PlexTokenSync extends PlexUtils with SonarrUtils with RadarrUtils {
5055
logger.debug(s"Found show \"${watchlistedItem.title}\" which does not exist yet in Sonarr")
5156
Right(addToSonarr(client)(config.sonarrConfiguration)(watchlistedItem))
5257
} else {
53-
logger.warn(s"Found show \"${watchlistedItem.title}\" which does not exist yet in Sonarr, but we do not have the tvdb ID so will skip adding")
58+
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")
5459
Right(IO.unit)
5560
}
5661
case (false, "movie") =>
5762
if (watchlistedItem.getTmdbId.isDefined) {
5863
logger.debug(s"Found movie \"${watchlistedItem.title}\" which does not exist yet in Radarr")
5964
Right(addToRadarr(client)(config.radarrConfiguration)(watchlistedItem))
6065
} else {
61-
logger.warn(s"Found movie \"${watchlistedItem.title}\" which does not exist yet in Radarr, but we do not have the tmdb ID so will skip adding")
66+
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")
6267
Right(IO.unit)
6368
}
6469

src/main/scala/Server.scala

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import cats.effect._
3-
import cats.implicits.catsSyntaxTuple4Parallel
3+
import cats.implicits.catsSyntaxTuple3Parallel
44
import configuration.{Configuration, ConfigurationUtils, SystemPropertyReader}
55
import http.HttpClient
66
import org.slf4j.LoggerFactory
@@ -24,22 +24,13 @@ object Server extends IOApp {
2424
for {
2525
memoizedConfigIo <- ConfigurationUtils.create(configReader, httpClient).memoize
2626
result <- (
27-
watchlistSync(memoizedConfigIo, httpClient),
2827
pingTokenSync(memoizedConfigIo, httpClient),
2928
plexTokenSync(memoizedConfigIo, httpClient),
3029
plexTokenDeleteSync(memoizedConfigIo, httpClient)
3130
).parTupled.as(ExitCode.Success)
3231
} yield result
3332
}
3433

35-
private def watchlistSync(configIO: IO[Configuration], httpClient: HttpClient): IO[Unit] =
36-
for {
37-
config <- configIO
38-
_ <- WatchlistSync.run(config, httpClient)
39-
_ <- IO.sleep(config.refreshInterval)
40-
_ <- watchlistSync(configIO, httpClient)
41-
} yield ()
42-
4334
private def pingTokenSync(configIO: IO[Configuration], httpClient: HttpClient): IO[Unit] =
4435
for {
4536
config <- configIO
@@ -48,10 +39,12 @@ object Server extends IOApp {
4839
_ <- pingTokenSync(configIO, httpClient)
4940
} yield ()
5041

51-
private def plexTokenSync(configIO: IO[Configuration], httpClient: HttpClient): IO[Unit] =
42+
private def plexTokenSync(configIO: IO[Configuration], httpClient: HttpClient, firstRun: Boolean = true): IO[Unit] =
5243
for {
5344
config <- configIO
54-
_ <- PlexTokenSync.run(config, httpClient)
45+
_ <- PlexTokenSync.run(config, httpClient, firstRun)
46+
_ <- IO.sleep(config.refreshInterval)
47+
_ <- plexTokenSync(configIO, httpClient, firstRun = false)
5548
} yield ()
5649

5750
private def plexTokenDeleteSync(configIO: IO[Configuration], httpClient: HttpClient): IO[Unit] =

src/main/scala/WatchlistSync.scala

-70
This file was deleted.

src/test/scala/PlexTokenSyncSpec.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class PlexTokenSyncSpec extends AnyFlatSpec with Matchers with MockFactory {
2525
defaultRadarrMock(mockHttpClient)
2626
defaultSonarrMock(mockHttpClient)
2727

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

3030
sync shouldBe ()
3131
}
@@ -51,7 +51,7 @@ class PlexTokenSyncSpec extends AnyFlatSpec with Matchers with MockFactory {
5151
radarrBypassIgnored = false
5252
),
5353
PlexConfiguration(
54-
plexWatchlistUrls = Set(Uri.unsafeFromString("https://localhost:9090")),
54+
plexWatchlistUrls = Set(),
5555
plexTokens = plexTokens,
5656
skipFriendSync = false
5757
),

0 commit comments

Comments
 (0)