Skip to content

Commit

Permalink
change printlns to logging framework (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
nylonee authored Oct 28, 2023
1 parent 258024e commit a7b4fd4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Watchlistarr

<img src="watchlistarr.png" alt="Alternate Text" width="240"/>

Sync plex watchlists in realtime with Sonarr and Radarr. **Requires Plex Pass**

## Getting Started
Expand Down
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ val fs2Version = "3.7.0"
val circeVersion = "0.14.5"
val circeGenericExtrasVersion = "0.14.3"
val scalatestVersion = "3.2.15"
val slf4jVersion = "2.0.9"

libraryDependencies ++= Seq(
"org.scala-lang" % "scala-library" % scalaVersion.value % "provided",
"ch.qos.logback" % "logback-classic" % logbackVersion,
"org.slf4j" % "slf4j-api" % slf4jVersion,
"org.http4s" %% "http4s-ember-client" % http4sVersion,
"org.http4s" %% "http4s-ember-server" % http4sVersion,
"org.http4s" %% "http4s-circe" % http4sVersion,
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
35 changes: 23 additions & 12 deletions src/main/scala/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import cats.effect.IO
import cats.effect.unsafe.implicits.global
import model.QualityProfile
import io.circe.generic.auto._
import org.slf4j.LoggerFactory

import scala.concurrent.duration._

class Configuration {

private val logger = LoggerFactory.getLogger(getClass)

val refreshInterval: FiniteDuration = getConfigOption("interval.seconds").flatMap(_.toIntOption).getOrElse(60).seconds

val (sonarrBaseUrl, sonarrApiKey, sonarrQualityProfileId) = getAndTestSonarrUrlAndApiKey.unsafeRunSync()
Expand All @@ -23,12 +26,14 @@ class Configuration {

ArrUtils.getToArr(url, apiKey, "qualityprofile").map {
case Right(res) =>
println("Successfully connected to Sonarr")
logger.info("Successfully connected to Sonarr")
val allQualityProfiles = res.as[List[QualityProfile]].getOrElse(List.empty)
val chosenQualityProfile = getConfigOption("sonarr.qualityProfile")
(url, apiKey, getQualityProfileId(allQualityProfiles, chosenQualityProfile))
case Left(err) =>
throw new IllegalArgumentException(s"Unable to connect to Sonarr at $url, with error $err")
val message = s"Unable to connect to Sonarr at $url, with error $err"
logger.error(message)
throw new IllegalArgumentException(message)
}
}

Expand All @@ -38,30 +43,34 @@ class Configuration {

ArrUtils.getToArr(url, apiKey, "qualityprofile").map {
case Right(res) =>
println("Successfully connected to Radarr")
logger.info("Successfully connected to Radarr")
val allQualityProfiles = res.as[List[QualityProfile]].getOrElse(List.empty)
val chosenQualityProfile = getConfigOption("radarr.qualityProfile")
(url, apiKey, getQualityProfileId(allQualityProfiles, chosenQualityProfile))
case Left(err) =>
throw new IllegalArgumentException(s"Unable to connect to Radarr at $url, with error $err")
val message = s"Unable to connect to Radarr at $url, with error $err"
logger.error(message)
throw new IllegalArgumentException(message)
}
}

private def getQualityProfileId(allProfiles: List[QualityProfile], maybeEnvVariable: Option[String]): Int =
(allProfiles, maybeEnvVariable) match {
case (Nil, _) =>
println("Could not find any quality profiles defined, check your Sonarr/Radarr settings")
logger.error("Could not find any quality profiles defined, check your Sonarr/Radarr settings")
throw new IllegalArgumentException("Unable to fetch quality profiles from Sonarr or Radarr")
case (List(one), _) =>
println(s"Only one quality profile defined: ${one.name}")
logger.debug(s"Only one quality profile defined: ${one.name}")
one.id
case (_, None) =>
println("Multiple quality profiles found, selecting the first one in the list.")
logger.debug("Multiple quality profiles found, selecting the first one in the list.")
allProfiles.head.id
case (_, Some(profileName)) =>
allProfiles.find(_.name.toLowerCase == profileName.toLowerCase).map(_.id).getOrElse(
throw new IllegalArgumentException(s"Unable to find quality profile $profileName. Possible values are $allProfiles")
)
allProfiles.find(_.name.toLowerCase == profileName.toLowerCase).map(_.id).getOrElse {
val message = s"Unable to find quality profile $profileName. Possible values are $allProfiles"
logger.error(message)
throw new IllegalArgumentException(message)
}
}

private def getPlexWatchlistUrls: List[String] =
Expand All @@ -71,12 +80,14 @@ class Configuration {
).toList.collect {
case Some(url) => url
} match {
case Nil => throw new IllegalArgumentException("Missing plex watchlist URL")
case Nil =>
logger.error("Missing plex watchlist URL")
throw new IllegalArgumentException("Missing plex watchlist URL")
case ok => ok
}

private def getConfig(key: String): String = getConfigOption(key).getOrElse {
println(s"Unable to find configuration for $key, have you set the environment variable?")
logger.error(s"Unable to find configuration for $key, have you set the environment variable?")
throw new IllegalArgumentException(s"Missing argument for $key")
}

Expand Down
27 changes: 15 additions & 12 deletions src/main/scala/WatchlistSync.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import io.circe.generic.auto._
import org.http4s.circe.CirceEntityDecoder._
import io.circe.syntax._
import model._
import org.slf4j.LoggerFactory

object WatchlistSync {

private val logger = LoggerFactory.getLogger(getClass)
def run(config: Configuration): IO[Unit] = {

for {
Expand Down Expand Up @@ -38,23 +41,23 @@ object WatchlistSync {
ArrUtils.getToArr(baseUrl, apiKey, "movie").map {
case Right(res) =>
res.as[List[RadarrMovie]].getOrElse {
println("Unable to fetch movies from Radarr - decoding failure. Returning empty list instead")
logger.warn("Unable to fetch movies from Radarr - decoding failure. Returning empty list instead")
List.empty
}
case Left(err) =>
println(s"Received error while trying to fetch movies from Radarr: $err")
logger.warn(s"Received error while trying to fetch movies from Radarr: $err")
throw err
}

private def fetchSeries(apiKey: String, baseUrl: String): IO[List[SonarrSeries]] =
ArrUtils.getToArr(baseUrl, apiKey, "series").map {
case Right(res) =>
res.as[List[SonarrSeries]].getOrElse {
println("Unable to fetch series from Sonarr - decoding failure. Returning empty list instead")
logger.warn("Unable to fetch series from Sonarr - decoding failure. Returning empty list instead")
List.empty
}
case Left(err) =>
println(s"Received error while trying to fetch movies from Radarr: $err")
logger.warn(s"Received error while trying to fetch movies from Radarr: $err")
throw err
}

Expand All @@ -72,16 +75,16 @@ object WatchlistSync {

(watchlistIds.exists(allIds.contains), watchlistedItem.category) match {
case (true, c) =>
println(s"$c \"${watchlistedItem.title}\" already exists in Sonarr/Radarr")
logger.debug(s"$c \"${watchlistedItem.title}\" already exists in Sonarr/Radarr")
IO.unit
case (false, "show") =>
println(s"Found show \"${watchlistedItem.title}\" which does not exist yet in Sonarr")
logger.debug(s"Found show \"${watchlistedItem.title}\" which does not exist yet in Sonarr")
addToSonarr(config)(watchlistedItem)
case (false, "movie") =>
println(s"Found movie \"${watchlistedItem.title}\" which does not exist yet in Radarr")
logger.debug(s"Found movie \"${watchlistedItem.title}\" which does not exist yet in Radarr")
addToRadarr(config)(watchlistedItem)
case (false, c) =>
println(s"Found $c \"${watchlistedItem.title}\", but I don't recognize the category")
logger.warn(s"Found $c \"${watchlistedItem.title}\", but I don't recognize the category")
IO.unit
}
}.toList.sequence.map(_.toSet)
Expand All @@ -101,9 +104,9 @@ object WatchlistSync {

ArrUtils.postToArr(config.radarrBaseUrl, config.radarrApiKey, "movie")(movie.asJson).map {
case Right(_) =>
println(s"Successfully added movie")
logger.info(s"Successfully added movie ${item.title} to Radarr")
case Left(err) =>
println(s"Failed to add movie: $err")
logger.error(s"Failed to add movie ${item.title}: $err")
}
}

Expand All @@ -120,9 +123,9 @@ object WatchlistSync {

ArrUtils.postToArr(config.sonarrBaseUrl, config.sonarrApiKey, "series")(show.asJson).map {
case Right(_) =>
println(s"Successfully added show")
logger.info(s"Successfully added show ${item.title} to Sonarr")
case Left(err) =>
println(s"Failed to add show: $err")
logger.info(s"Failed to add show ${item.title}: $err")
}
}

Expand Down

0 comments on commit a7b4fd4

Please sign in to comment.