Skip to content

Commit

Permalink
Merge pull request #66 from haijeploeg/feature/pyarr_intergration
Browse files Browse the repository at this point in the history
Feature/pyarr intergration
  • Loading branch information
haijeploeg authored May 20, 2022
2 parents 3b1d371 + b37ac28 commit 1f01980
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 446 deletions.
8 changes: 2 additions & 6 deletions excludarr/commands/radarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def exclude(
locale = config.locale

# Setup Radarr Actions to control the different tasks
radarr = RadarrActions(
config.radarr_url, config.radarr_api_key, locale, config.radarr_verify_ssl
)
radarr = RadarrActions(config.radarr_url, config.radarr_api_key, locale)

# Get the movies to exclude and exclude the movies that are in the exclude list
movies_to_exclude = radarr.get_movies_to_exclude(
Expand Down Expand Up @@ -177,9 +175,7 @@ def re_add(
locale = config.locale

# Setup Radarr Actions to control the different tasks
radarr = RadarrActions(
config.radarr_url, config.radarr_api_key, locale, config.radarr_verify_ssl
)
radarr = RadarrActions(config.radarr_url, config.radarr_api_key, locale)

# Get the movies that should be re monitored
movies_to_re_add = radarr.get_movies_to_re_add(providers, config.fast_search, disable_progress)
Expand Down
8 changes: 2 additions & 6 deletions excludarr/commands/sonarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ def exclude(
locale = config.locale

# Setup Sonarr Actions to control the different tasks
sonarr = SonarrActions(
config.sonarr_url, config.sonarr_api_key, locale, config.sonarr_verify_ssl
)
sonarr = SonarrActions(config.sonarr_url, config.sonarr_api_key, locale)

series_to_exclude = sonarr.get_series_to_exclude(
providers, config.fast_search, disable_progress, tmdb_api_key=config.tmdb_api_key
Expand Down Expand Up @@ -229,9 +227,7 @@ def re_add(
locale = config.locale

# Setup Sonarr Actions to control the different tasks
sonarr = SonarrActions(
config.sonarr_url, config.sonarr_api_key, locale, config.sonarr_verify_ssl
)
sonarr = SonarrActions(config.sonarr_url, config.sonarr_api_key, locale)

series_to_re_add = sonarr.get_series_to_re_add(
providers, config.fast_search, disable_progress, tmdb_api_key=config.tmdb_api_key
Expand Down
50 changes: 27 additions & 23 deletions excludarr/core/radarr_actions.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from platform import release
from loguru import logger
from rich.progress import Progress
from pyarr import RadarrAPI

import excludarr.modules.pyradarr as pyradarr
import excludarr.utils.filters as filters

from excludarr.modules.justwatch import JustWatch
from excludarr.modules.justwatch.exceptions import JustWatchNotFound, JustWatchTooManyRequests
from excludarr.modules.pyradarr.exceptions import RadarrMovieNotFound


class RadarrActions:
def __init__(self, url, api_key, locale, verify_ssl=False):
def __init__(self, url, api_key, locale):
logger.debug(f"Initializing PyRadarr")
self.radarr_client = pyradarr.Radarr(url, api_key, verify_ssl)
self.radarr_client = RadarrAPI(url, api_key)

logger.debug(f"Initializing JustWatch API with locale: {locale}")
self.justwatch_client = JustWatch(locale)
Expand Down Expand Up @@ -82,7 +80,7 @@ def get_movies_to_exclude(self, providers, fast=True, disable_progress=False):

# Get all movies listed in Radarr
logger.debug("Getting all the movies from Radarr")
radarr_movies = self.radarr_client.movie.get_all_movies()
radarr_movies = self.radarr_client.get_movie()

# Get the providers listed for the configured locale from JustWatch
# and filter it with the given providers. This will ensure only the correct
Expand Down Expand Up @@ -150,7 +148,7 @@ def get_movies_to_re_add(self, providers, fast=True, disable_progress=False):

# Get all movies listed in Radarr and filter it to only include not monitored movies
logger.debug("Getting all the movies from Radarr")
radarr_movies = self.radarr_client.movie.get_all_movies()
radarr_movies = self.radarr_client.get_movie()
radarr_not_monitored_movies = [movie for movie in radarr_movies if not movie["monitored"]]

# Get the providers listed for the configured locale from JustWatch
Expand Down Expand Up @@ -208,45 +206,51 @@ def delete(self, ids, delete_files, add_import_exclusion):
try:
logger.debug("Trying to bulk delete all movies at once")

self.radarr_client.movie.delete_movies(
ids, delete_files=delete_files, add_import_exclusion=add_import_exclusion
self.radarr_client.del_movies(
{
"movieIds": ids,
"deleteFiles": delete_files,
"addImportExclusion": add_import_exclusion,
}
)
except RadarrMovieNotFound:
except Exception:
logger.warning("Bulk delete failed, falling back to deleting each movie individually")
for id in ids:
logger.debug(f"Deleting movie with Radarr ID: {id}")

self.radarr_client.movie.delete_movie(
id, delete_files=delete_files, add_import_exclusion=add_import_exclusion
try:
for id in ids:
logger.debug(f"Deleting movie with Radarr ID: {id}")

self.radarr_client.del_movie(
id, delete_files=delete_files, add_exclusion=add_import_exclusion
)
except Exception as e:
logger.error(e)
logger.error(
f"Something went wrong with deleting the movies from Radarr, check the configuration or try --debug for more information"
)
except Exception as e:
logger.error(e)
logger.error(
f"Something went wrong with deleting the movies from Radarr, check the configuration or try --debug for more information"
)

def disable_monitored(self, movies):
logger.debug("Starting the process of changing the status to not monitored")
for movie in movies:
movie.update({"monitored": False})

logger.debug(f"Change monitored to False for movie with Radarr ID: {movie['id']}")
self.radarr_client.movie.update_movie(movie)
self.radarr_client.upd_movie(movie)

def enable_monitored(self, movies):
logger.debug("Starting the process of changing the status to monitored")
for movie in movies:
movie.update({"monitored": True})

logger.debug(f"Change monitored to True for movie with Radarr ID: {movie['id']}")
self.radarr_client.movie.update_movie(movie)
self.radarr_client.upd_movie(movie)

def delete_files(self, ids):
logger.debug("Starting the process of deleting the files")
for id in ids:
logger.debug(f"Checking if movie with Radarr ID: {id} has files")
moviefiles = self.radarr_client.moviefile.get_moviefiles(id)
moviefiles = self.radarr_client.get_movie_files_by_movie_id(id)

for moviefile in moviefiles:
logger.debug(f"Deleting files for movie with Radarr ID: {id}")
self.radarr_client.moviefile.delete_moviefile(moviefile["id"])
self.radarr_client.del_movie_file(moviefile["id"])
45 changes: 26 additions & 19 deletions excludarr/core/sonarr_actions.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
from loguru import logger
from rich.progress import Progress
from pyarr import SonarrAPI

import excludarr.modules.pysonarr as pysonarr
import excludarr.modules.pytmdb as pytmdb
import excludarr.utils.filters as filters

from excludarr.modules.justwatch import JustWatch
from excludarr.modules.justwatch.exceptions import JustWatchNotFound, JustWatchTooManyRequests


def upd_episode(self, id, data):
path = f"episode/{id}"
return self.request_put(path, self.ver_uri, data=data)


# Patch the upd_episode endpoint
SonarrAPI.upd_episode = upd_episode


class SonarrActions:
def __init__(self, url, api_key, locale, verify_ssl=False):
def __init__(self, url, api_key, locale):
logger.debug(f"Initializing PySonarr")
self.sonarr_client = pysonarr.Sonarr(url, api_key, verify_ssl)
self.sonarr_client = SonarrAPI(url, api_key, ver_uri="/v3")

logger.debug(f"Initializing JustWatch API with locale: {locale}")
self.justwatch_client = JustWatch(locale)
Expand Down Expand Up @@ -157,7 +166,7 @@ def get_series_to_exclude(

# Get all series listed in Sonarr
logger.debug("Getting all the series from Sonarr")
sonarr_series = self.sonarr_client.serie.get_all_series()
sonarr_series = self.sonarr_client.get_series()

# Get the providers listed for the configured locale from JustWatch
# and filter it with the given providers. This will ensure only the correct
Expand All @@ -179,7 +188,7 @@ def get_series_to_exclude(
ended = serie["ended"]

# Get episodes of the serie
episodes = self.sonarr_client.episode.get_episodes_of_serie(sonarr_id)
episodes = self.sonarr_client.get_episodes_by_series_id(sonarr_id)

# Get JustWatch serie data
jw_id, jw_serie_data = self._find_serie(
Expand Down Expand Up @@ -331,7 +340,7 @@ def get_series_to_re_add(self, providers, fast=True, disable_progress=False, tmd

# Get all series listed in Sonarr
logger.debug("Getting all the series from Sonarr")
sonarr_series = self.sonarr_client.serie.get_all_series()
sonarr_series = self.sonarr_client.get_series()

# Get the providers listed for the configured locale from JustWatch
# and filter it with the given providers. This will ensure only the correct
Expand All @@ -352,7 +361,7 @@ def get_series_to_re_add(self, providers, fast=True, disable_progress=False, tmd
ended = serie["ended"]

# Get episodes of the serie
episodes = self.sonarr_client.episode.get_episodes_of_serie(sonarr_id)
episodes = self.sonarr_client.get_episodes_by_series_id(sonarr_id)

# Get JustWatch serie data
jw_id, jw_serie_data = self._find_serie(
Expand Down Expand Up @@ -495,9 +504,7 @@ def delete_serie(self, id, delete_files, add_import_exclusion):

try:
logger.debug(f"Deleting serie with Sonarr ID: {id}")
self.sonarr_client.serie.delete_serie(
id, delete_files=delete_files, add_import_exclusion=add_import_exclusion
)
self.sonarr_client.del_series(id, delete_files=delete_files)
except Exception as e:
logger.error(e)
logger.error(
Expand All @@ -510,7 +517,7 @@ def delete_episode_files(self, id, episode_file_ids):
for episode_file in episode_file_ids:
try:
logger.debug(f"Deleting episode ID: {episode_file} for serie with Sonarr ID: {id}")
self.sonarr_client.episodefile.delete_episodefile(episode_file)
self.sonarr_client.del_episode_file(episode_file)
except Exception as e:
logger.error(e)
logger.error(
Expand All @@ -524,7 +531,7 @@ def disable_monitored_serie(self, id, sonarr_object):

try:
logger.debug(f"Updating serie with Sonarr ID: {id}")
self.sonarr_client.serie.update_serie(sonarr_object)
self.sonarr_client.upd_series(sonarr_object)
except Exception as e:
logger.error(e)
logger.error(
Expand All @@ -538,7 +545,7 @@ def disable_monitored_seasons(self, id, sonarr_object, seasons):

try:
logger.debug(f"Updating serie with Sonarr ID: {id}")
self.sonarr_client.serie.update_serie(updated_sonarr_object)
self.sonarr_client.upd_series(updated_sonarr_object)
except Exception as e:
logger.error(e)
logger.error(
Expand All @@ -553,13 +560,13 @@ def disable_monitored_episodes(self, id, episode_ids):
logger.debug(
f"Get details of episode with ID: {episode_id} for serie with Sonarr ID: {id}"
)
episode_object = self.sonarr_client.episode.get_episode(episode_id)
episode_object = self.sonarr_client.get_episode_by_episode_id(episode_id)
episode_object["monitored"] = False

logger.debug(
f"Updating episode with ID: {episode_id} for serie with Sonarr ID: {id}"
)
self.sonarr_client.episode.update_episode(episode_id, episode_object)
self.sonarr_client.upd_episode(episode_id, episode_object)
except Exception as e:
logger.error(e)
logger.error(
Expand All @@ -573,7 +580,7 @@ def enable_monitored_serie(self, id, sonarr_object):

try:
logger.debug(f"Updating serie with Sonarr ID: {id}")
self.sonarr_client.serie.update_serie(sonarr_object)
self.sonarr_client.upd_series(sonarr_object)
except Exception as e:
logger.error(e)
logger.error(
Expand All @@ -587,7 +594,7 @@ def enable_monitored_seasons(self, id, sonarr_object, seasons):

try:
logger.debug(f"Updating serie with Sonarr ID: {id}")
self.sonarr_client.serie.update_serie(updated_sonarr_object)
self.sonarr_client.upd_series(updated_sonarr_object)
except Exception as e:
logger.error(e)
logger.error(
Expand All @@ -602,13 +609,13 @@ def enable_monitored_episodes(self, id, episode_ids):
logger.debug(
f"Get details of episode with ID: {episode_id} for serie with Sonarr ID: {id}"
)
episode_object = self.sonarr_client.episode.get_episode(episode_id)
episode_object = self.sonarr_client.get_episode_by_episode_id(episode_id)
episode_object["monitored"] = True

logger.debug(
f"Updating episode with ID: {episode_id} for serie with Sonarr ID: {id}"
)
self.sonarr_client.episode.update_episode(episode_id, episode_object)
self.sonarr_client.upd_episode(episode_id, episode_object)
except Exception as e:
logger.error(e)
logger.error(
Expand Down
81 changes: 0 additions & 81 deletions excludarr/modules/pyradarr/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions excludarr/modules/pyradarr/base.py

This file was deleted.

Loading

0 comments on commit 1f01980

Please sign in to comment.