Skip to content

Commit

Permalink
Merge pull request #75 from CollinHeist/develop
Browse files Browse the repository at this point in the history
Add option to skip syncing specials from Sonarr, minor bug fixes
  • Loading branch information
CollinHeist authored Apr 15, 2022
2 parents bf51135 + fb82532 commit 332b67c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

# Validate that ImageMagick is configured correctly
found = False
for prefix in ('magick ', ''):
for prefix in ('', 'magick '):
pp.use_magick_prefix = 'magick' in prefix
imi = ImageMagickInterface(pp.imagemagick_container)
font_output = imi.run_get_output(f'convert -list font')
Expand Down
15 changes: 10 additions & 5 deletions modules/PreferenceParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(self, file: Path) -> None:
self.use_sonarr = False
self.sonarr_url = None
self.sonarr_api_key = None
self.sonarr_sync_specials = True
self.use_tmdb = False
self.tmdb_api_key = None
self.tmdb_retry_count = TMDbInterface.BLACKLIST_THRESHOLD
Expand Down Expand Up @@ -186,6 +187,10 @@ def __parse_yaml(self) -> None:
self.sonarr_api_key = self.__yaml['sonarr']['api_key']
self.use_sonarr = True

if self.__is_specified('sonarr', 'sync_specials'):
value = self.__yaml['sonarr']['sync_specials']
self.sonarr_sync_specials = bool(value)

if self.__is_specified('tmdb'):
if not self.__is_specified('tmdb', 'api_key'):
log.critical(f'TMDb preferences must contain "api_key"')
Expand Down Expand Up @@ -273,11 +278,11 @@ def iterate_series_files(self) -> [Show]:
continue

# Get library map for this file; error+skip missing library paths
library_map = file_yaml.get('libraries', {})
if not all('path' in library_map[lib] for lib in library_map):
log.error(f'Libraries in series file "{file_object.resolve()}" '
f'are missing their "path" attributes.')
continue
if (library_map := file_yaml.get('libraries', {})):
if not all('path' in library_map[lib] for lib in library_map):
log.error(f'Libraries in series file "{file_object.resolve()}'
f'"are missing their "path" attributes.')
continue

# Get font map for this file
font_map = file_yaml.get('fonts', {})
Expand Down
11 changes: 11 additions & 0 deletions modules/Show.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __init__(self, name: str, yaml_dict: dict, library_map: dict,
self.library = None
self.archive = True
self.sonarr_sync = True
self.sync_specials = self.preferences.sonarr_sync_specials
self.tmdb_sync = True
self.hide_seasons = False
self.__episode_range = {}
Expand Down Expand Up @@ -178,6 +179,9 @@ def __parse_yaml(self):
if self.__is_specified('sonarr_sync'):
self.sonarr_sync = bool(self.__yaml['sonarr_sync'])

if self.__is_specified('sync_specials'):
self.sync_specials = bool(self.__yaml['sync_specials'])

if self.__is_specified('tmdb_sync'):
self.tmdb_sync = bool(self.__yaml['tmdb_sync'])

Expand Down Expand Up @@ -392,6 +396,13 @@ def query_sonarr(self, sonarr_interface: 'SonarrInterface') -> None:
all_episodes,
))

# Filter episodes that are specials if sync_specials is False
if not self.sync_specials:
new_episodes = list(filter(
lambda e: e.season_number != 0,
new_episodes,
))

# If there are new episodes, add to the datafile, return True
if new_episodes:
self.file_interface.add_many_entries(new_episodes)
Expand Down
21 changes: 1 addition & 20 deletions modules/TMDbInterface.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime, timedelta
from pathlib import Path
from yaml import dump, safe_load
from urllib.request import urlretrieve

from modules.Debug import log
from modules.EpisodeInfo import EpisodeInfo
Expand All @@ -21,7 +20,7 @@ class TMDbInterface(WebInterface):
API_BASE_URL = 'https://api.themoviedb.org/3/'

"""Default for how many failed requests lead to a blacklisted entry"""
BLACKLIST_THRESHOLD = 3
BLACKLIST_THRESHOLD = 5

"""Generic translated episode format strings for each language code"""
GENERIC_TITLE_FORMATS = {
Expand Down Expand Up @@ -634,24 +633,6 @@ def get_series_logo(self, series_info: SeriesInfo) -> str:
return f'https://image.tmdb.org/t/p/original{best["file_path"]}'


def download_image(self, image_url: str, destination: Path) -> None:
"""
Downloads the provided image URL to the destination filepath.
:param image_url: The image url to download.
:param destination: The destination for the requested image.
"""

# Make parent folder structure
destination.parent.mkdir(parents=True, exist_ok=True)

# Download the image and store it in destination
try:
urlretrieve(image_url, destination.resolve())
except Exception as e:
log.error(f'Cannot download, TMDb errored: "{e}"')


@staticmethod
def manually_download_season(api_key: str, title: str, year: int,
season: int, episode_count: int,
Expand Down
25 changes: 24 additions & 1 deletion modules/WebInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,27 @@ def _get(self, url: str, params: dict) -> dict:
self.__cached_results.pop(0)

# Return latest result
return self.__cached_results[-1]
return self.__cached_results[-1]


def download_image(self, image_url: str, destination: 'Path') -> None:
"""
Download the provided image URL to the destination filepath.
:param image_url: The image url to download.
:param destination: The destination for the requested image.
"""

# Make parent folder structure
destination.parent.mkdir(parents=True, exist_ok=True)

# Attempt to download the image, if an error happens log to user
try:
with destination.open('wb') as file_handle:
file_handle.write(get(image_url).content)
log.debug(f'downloaded {image_url} to {destination}')
except Exception as e:
log.error(f'Cannot download image, error: "{e}"')



0 comments on commit 332b67c

Please sign in to comment.