Skip to content

Commit

Permalink
Merge pull request #57 from CollinHeist/develop
Browse files Browse the repository at this point in the history
Permit "custom" season titles in Star Wars cards, other minor fixes and changes
  • Loading branch information
CollinHeist authored Apr 9, 2022
2 parents 73949d6 + 54ced47 commit fd20c9f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
17 changes: 16 additions & 1 deletion fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from modules.GenreMaker import GenreMaker
from modules.PreferenceParser import PreferenceParser
from modules.preferences import set_preference_parser
from modules.SonarrInterface import SonarrInterface
from modules.TitleCard import TitleCard
from modules.TMDbInterface import TMDbInterface

Expand Down Expand Up @@ -35,7 +36,7 @@
title_card_group.add_argument(
'--episode',
type=str,
default='EPISODE x',
default='EPISODE',
metavar='EPISODE_TEXT',
help="Specify this card's episode text")
title_card_group.add_argument(
Expand Down Expand Up @@ -101,6 +102,13 @@
help='Create all genre cards for images in the given directory based on '
'their file names')

# Argument group for fixes relating to Sonarr
sonarr_group = parser.add_argument_group('Sonarr')
sonarr_group.add_argument(
'--sonarr-list-ids',
action='store_true',
help="Whether to list all the ID's for all shows within Sonarr")

# Argument group for fixes relating to TheMovieDatabase
tmdb_group = parser.add_argument_group(
'TheMovieDatabase',
Expand Down Expand Up @@ -177,6 +185,13 @@
output=Path(file.parent / f'{file.stem}-GenreCard{file.suffix}'),
).create()

# Execute Sonarr related options
if args.sonarr_list_ids:
if not pp.use_sonarr:
log.warning("Cannot print Sonarr ID's if Sonarr is disabled")
else:
SonarrInterface(pp.sonarr_url, pp.sonarr_api_key).list_all_series_id()

# Execute TMDB related options
if hasattr(args, 'delete_blacklist'):
if args.delete_blacklist:
Expand Down
43 changes: 30 additions & 13 deletions modules/StarWarsTitleCard.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
from re import match

from num2words import num2words

Expand Down Expand Up @@ -75,14 +76,15 @@ def __init__(self, source: Path, output_file: Path, title: str,

# Modify episode text to remove "Episode"-like text, replace numbers
# with text, strip spaces, and convert to uppercase
self.episode_prefix = 'EPISODE'
self.episode_text = self.image_magick.escape_chars(
self.__modify_episode_text(episode_text)
)


def __modify_episode_text(self, text: str) -> str:
"""
Amend the given episode text (such as "EPISODE 1" or "CHAPTER 1") to fit
Modify the given episode text (such as "EPISODE 1" or "CHAPTER 1") to fit
the theme of this card. This removes preface text like episode, chapter,
or part; and converts numeric episode numbers to their text equivalent.
For example:
Expand All @@ -92,7 +94,7 @@ def __modify_episode_text(self, text: str) -> str:
>>> self.__modify_episode_text('PART 14')
'FOURTEEN'
:param text: The episode text to modify.
:param text: The episode text to modify.
:returns: The modified episode text with preface text removed, numbers
replaced with words, and converted to uppercase. If numbers
Expand All @@ -102,13 +104,21 @@ def __modify_episode_text(self, text: str) -> str:
# Convert to uppercase, remove space padding
modified_text = text.upper().strip()

# Remove preface text
for to_remove in ['CHAPTER', 'EPISODE', 'PART']:
modified_text = modified_text.replace(to_remove, '')
# Remove preface text - if CHAPTER or EPISODE, set object episode prefix
if match(rf'CHAPTER\s*(\d+)', modified_text):
self.episode_prefix = 'CHAPTER'
modified_text = modified_text.replace('CHAPTER', '')
elif match(rf'EPISODE\s*(\d+)', modified_text):
self.episode_prefix = 'EPISODE'
modified_text = modified_text.replace('EPISODE', '')
elif match(rf'PART\s*(\d+)', modified_text):
modified_text = modified_text.replace('PART', '')

try:
# Only digit episode text remains, return as a number (i.e. "two")
return num2words(int(modified_text.strip())).upper()
except ValueError:
# Not just a digit, return as-is
return modified_text.strip()


Expand Down Expand Up @@ -158,7 +168,8 @@ def __add_title_text(self) -> list:

def __add_episode_prefix(self) -> list:
"""
ImageMagick commands to add the "EPISODE" prefix text to an image.
ImageMagick commands to add the episode prefix text to an image. This is
either "EPISODE" or "CHAPTER".
:returns: List of ImageMagick commands.
"""
Expand All @@ -169,7 +180,7 @@ def __add_episode_prefix(self) -> list:
f'-fill "{self.EPISODE_TEXT_COLOR}"',
f'-pointsize 53',
f'-kerning 19',
f'-annotate +325-140 "EPISODE"',
f'-annotate +325-140 "{self.episode_prefix}"',
]


Expand Down Expand Up @@ -227,17 +238,23 @@ def is_custom_font(font: 'Font') -> bool:


@staticmethod
def is_custom_season_titles(*args, **kwargs) -> bool:
def is_custom_season_titles(episode_text_format: str,
*args, **kwargs) -> bool:
"""
Determines if custom season titles.
Determines whether the given attributes constitute custom or generic
season titles.
:param args and kwargs: Generic arguments to permit generalized
function calls for any CardType.
:param episode_text_format: The episode text format in use.
:param args and kwargs: Generic arguments to permit
generalized function calls for any
CardType.
:returns: False, as season titles are not customizable with this card.
:returns: True if custom season titles are indicated, False otherwise.
"""

return False
generic_formats = ('EPISODE {episode_number}', 'PART {episode_number}')

return episode_text_format not in generic_formats


def create(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion modules/TMDbInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def get_source_image(self, series_info: SeriesInfo,

# If None was returned, episode not found - warn, blacklist, and exit
if index == None:
log.info(f'TMDb has no matching episode for "{series_info}" '
log.debug(f'TMDb has no matching episode for "{series_info}" '
f'{episode_info}')
self.__update_blacklist(series_info, episode_info, 'image')
return None
Expand Down
2 changes: 1 addition & 1 deletion modules/TitleCard.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def validate_card_format_string(format_string: str) -> bool:
season=1, episode=1, title='Episode Title',
)
return True
except ValueError as e:
except Exception as e:
# Invalid format string, log
log.error(f'Card format string is invalid - "{e}"')
return False
Expand Down

0 comments on commit fd20c9f

Please sign in to comment.