Skip to content

Commit

Permalink
Update quality profile code so that it's optional (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nylonee authored Oct 28, 2023
1 parent ee5c08b commit e0d4c48
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 48 deletions.
40 changes: 13 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,24 @@ The easiest way to try this code is using docker:
```bash
docker run \
-e SONARR_API_KEY=YOUR_API_KEY \
-e SONARR_QUALITY_PROFILE_ID=6 \
-e RADARR_API_KEY=YOUR_API_KEY \
-e RADARR_QUALITY_PROFILE_ID=6 \
-e PLEX_WATCHLIST_URL_1=YOUR_PLEX_WATCHLIST_URL \
nylonee/watchlistarr
```

| Key | Example Value | Optional | Description |
|---------------------------|----------------------------------|----------|-----------------------------------------------------------------|
| REFRESH_INTERVAL_SECONDS | 60 | Yes | Number of seconds to wait in between checking the watchlist |
| SONARR_API_KEY | 7a392fb4817a46e59f2e84e7d5f021bc | No | API key for Sonarr, found in your Sonarr UI -> General settings |
| SONARR_BASE_URL | http://localhost:8989 | Yes | Base URL for Sonarr, including the 'http' and port |
| SONARR_QUALITY_PROFILE_ID | 6 | No | Quality profile ID for Sonarr |
| SONARR_ROOT_FOLDER | /data/ | Yes | Root folder for Sonarr |
| RADARR_API_KEY | 7a392fb4817a46e59f2e84e7d5f021bc | No | API key for Radarr, found in your Radarr UI -> General settings |
| RADARR_BASE_URL | http://127.0.0.1:7878 | Yes | Base URL for Radarr, including the 'http' and port |
| RADARR_QUALITY_PROFILE_ID | 6 | No | Quality profile ID for Radarr |
| RADARR_ROOT_FOLDER | /data/ | Yes | Root folder for Radarr |
| PLEX_WATCHLIST_URL_1 | https://rss.plex.tv/UUID | No | First Plex Watchlist URL |
| PLEX_WATCHLIST_URL_2 | https://rss.plex.tv/UUID | Yes | Second Plex Watchlist URL (if applicable) |

### Getting your Quality Profile IDs

Eventually I will make this easier to get, but for now:

1. Go to your Sonarr/Radarr UI
2. Go to Settings -> Profiles
3. Open the profile you want to find the ID for
4. Right-click your browser and click "Inspect"
5. On the screen that shows up, navigate to the "Network" tab
6. On the Sonarr/Radarr UI, click "Save" on the profile card
7. A new entry will show up on the network tab, with a number. This number is your Quality Profile ID
| Key | Example Value | Optional | Description |
|--------------------------|----------------------------------|----------|--------------------------------------------------------------------------|
| REFRESH_INTERVAL_SECONDS | 60 | Yes | Number of seconds to wait in between checking the watchlist |
| SONARR_API_KEY | 7a392fb4817a46e59f2e84e7d5f021bc | No | API key for Sonarr, found in your Sonarr UI -> General settings |
| SONARR_BASE_URL | http://localhost:8989 | Yes | Base URL for Sonarr, including the 'http' and port |
| SONARR_QUALITY_PROFILE | 1080p | Yes | Quality profile for Sonarr, found in your Sonarr UI -> Profiles settings |
| SONARR_ROOT_FOLDER | /data/ | Yes | Root folder for Sonarr |
| RADARR_API_KEY | 7a392fb4817a46e59f2e84e7d5f021bc | No | API key for Radarr, found in your Radarr UI -> General settings |
| RADARR_BASE_URL | http://127.0.0.1:7878 | Yes | Base URL for Radarr, including the 'http' and port |
| RADARR_QUALITY_PROFILE | 1080p | Yes | Quality profile for Radarr, found in your Radarr UI -> Profiles settings |
| RADARR_ROOT_FOLDER | /data/ | Yes | Root folder for Radarr |
| PLEX_WATCHLIST_URL_1 | https://rss.plex.tv/UUID | No | First Plex Watchlist URL |
| PLEX_WATCHLIST_URL_2 | https://rss.plex.tv/UUID | Yes | Second Plex Watchlist URL (if applicable) |

### Getting your Plex Watchlist URLs

Expand Down
8 changes: 4 additions & 4 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ if [ -n "$SONARR_BASE_URL" ]; then
CMD="$CMD -Dsonarr.baseUrl=$SONARR_BASE_URL"
fi

if [ -n "$SONARR_QUALITY_PROFILE_ID" ]; then
CMD="$CMD -Dsonarr.qualityProfile=$SONARR_QUALITY_PROFILE_ID"
if [ -n "$SONARR_QUALITY_PROFILE" ]; then
CMD="$CMD -Dsonarr.qualityProfile=$SONARR_QUALITY_PROFILE"
fi

if [ -n "$SONARR_ROOT_FOLDER" ]; then
Expand All @@ -26,8 +26,8 @@ if [ -n "$RADARR_BASE_URL" ]; then
CMD="$CMD -Dradarr.baseUrl=$RADARR_BASE_URL"
fi

if [ -n "$RADARR_QUALITY_PROFILE_ID" ]; then
CMD="$CMD -Dradarr.qualityProfile=$RADARR_QUALITY_PROFILE_ID"
if [ -n "$RADARR_QUALITY_PROFILE" ]; then
CMD="$CMD -Dradarr.qualityProfile=$RADARR_QUALITY_PROFILE"
fi

if [ -n "$RADARR_ROOT_FOLDER" ]; then
Expand Down
50 changes: 35 additions & 15 deletions src/main/scala/Configuration.scala
Original file line number Diff line number Diff line change
@@ -1,49 +1,69 @@
import cats.effect.IO
import cats.effect.unsafe.implicits.global
import model.QualityProfile
import io.circe.generic.auto._

import scala.concurrent.duration._

class Configuration {

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

val (sonarrBaseUrl, sonarrApiKey) = getAndTestSonarrUrlAndApiKey.unsafeRunSync()
// TODO: Grab the quality profile ID automatically if it's not set
val sonarrQualityProfileId: Option[Int] = getConfigOption("sonarr.qualityProfile").flatMap(_.toIntOption)
val (sonarrBaseUrl, sonarrApiKey, sonarrQualityProfileId) = getAndTestSonarrUrlAndApiKey.unsafeRunSync()
val sonarrRootFolder: String = getConfigOption("sonarr.rootFolder").getOrElse("/data/")

val (radarrBaseUrl, radarrApiKey) = getAndTestRadarrUrlAndApiKey.unsafeRunSync()
val radarrQualityProfileId: Option[Int] = getConfigOption("radarr.qualityProfile").flatMap(_.toIntOption)
val (radarrBaseUrl, radarrApiKey, radarrQualityProfileId) = getAndTestRadarrUrlAndApiKey.unsafeRunSync()
val radarrRootFolder: String = getConfigOption("radarr.rootFolder").getOrElse("/data/")

val plexWatchlistUrls: List[String] = getPlexWatchlistUrls

private def getAndTestSonarrUrlAndApiKey: IO[(String, String)] = {
private def getAndTestSonarrUrlAndApiKey: IO[(String, String, Int)] = {
val url = getConfigOption("sonarr.baseUrl").getOrElse("http://localhost:8989")
val apiKey = getConfig("sonarr.apikey")

ArrUtils.getToArr(url, apiKey, "health").map {
case Right(_) =>
println("Successfully tested the connection to Sonarr!")
(url, apiKey)
ArrUtils.getToArr(url, apiKey, "qualityprofile").map {
case Right(res) =>
println("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")
}
}

private def getAndTestRadarrUrlAndApiKey: IO[(String, String)] = {
private def getAndTestRadarrUrlAndApiKey: IO[(String, String, Int)] = {
val url = getConfigOption("radarr.baseUrl").getOrElse("http://localhost:7878")
val apiKey = getConfig("radarr.apikey")

ArrUtils.getToArr(url, apiKey, "health").map {
case Right(_) =>
println("Successfully tested the connection to Radarr!")
(url, apiKey)
ArrUtils.getToArr(url, apiKey, "qualityprofile").map {
case Right(res) =>
println("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")
}
}

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")
throw new IllegalArgumentException("Unable to fetch quality profiles from Sonarr or Radarr")
case (List(one), _) =>
println(s"Only one quality profile defined: ${one.name}")
one.id
case (_, None) =>
println("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")
)
}

private def getPlexWatchlistUrls: List[String] =
Set(
getConfigOption("plex.watchlist1"),
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/WatchlistSync.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ object WatchlistSync {

private def addToRadarr(config: Configuration)(item: Item): IO[Unit] = {

val movie = RadarrPost(item.title, findTmdbId(item.guids).getOrElse(0L), config.radarrQualityProfileId.getOrElse(0), config.radarrRootFolder)
val movie = RadarrPost(item.title, findTmdbId(item.guids).getOrElse(0L), config.radarrQualityProfileId, config.radarrRootFolder)

ArrUtils.postToArr(config.radarrBaseUrl, config.radarrApiKey, "movie")(movie.asJson).map {
case Right(_) =>
Expand All @@ -116,7 +116,7 @@ object WatchlistSync {

private def addToSonarr(config: Configuration)(item: Item): IO[Unit] = {

val show = SonarrPost(item.title, findTvdbId(item.guids).getOrElse(0L), config.sonarrQualityProfileId.getOrElse(0), config.sonarrRootFolder)
val show = SonarrPost(item.title, findTvdbId(item.guids).getOrElse(0L), config.sonarrQualityProfileId, config.sonarrRootFolder)

ArrUtils.postToArr(config.sonarrBaseUrl, config.sonarrApiKey, "series")(show.asJson).map {
case Right(_) =>
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/model/QualityProfile.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package model

case class QualityProfile(name: String, id: Int)

0 comments on commit e0d4c48

Please sign in to comment.