Skip to content

Commit

Permalink
Add process_thumbnails setting for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
exscape committed Feb 3, 2025
1 parent 27e109f commit 1ac1085
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ plex\_url, plex\_token, libraries, ignored\_items
hidden\_string and lock\_hidden\_summaries were removed. (Edited fields are now always locked; disabling that setting was rarely a good idea.)

The following settings were added:
hidden\_summary\_string (string to show in Plex for a hidden summary)
hidden\_title\_string (string to show in Plex for a hidden title)
hide\_summaries (true/false)
hide\_titles (true/false)
hide\_thumbnails (true/false)
hidden\_summary\_string (string to show in Plex for a hidden summary)
hidden\_title\_string (string to show in Plex for a hidden title)
process\_thumbnails (true/false, see config\_sample.toml for a detailed explanation)

# Installation

Expand Down
10 changes: 10 additions & 0 deletions config_sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ hide_summaries = true
hide_titles = false
hide_thumbnails = false

# The following setting controls whether thumbnails are processed AT ALL.
# Setting this to false will improve performance for those who don't want to hide thumbnails; with thousands of episodes,
# this can be a >10x speedup, as checking the thumbnails is rather slow.
# This must be true if hide_thumbnails is true above.
#
# PLEASE NOTE:
# When this is false, thumbnails/posters will not even be looked at, which means if they have been hidden
# by the script previously, they will not be unhidden, not even with the --restore-all argument.
process_thumbnails = true

# The text to show in Plex for episodes/movies with their summmary/title hidden.
#
# !! IMPORTANT !!
Expand Down
26 changes: 17 additions & 9 deletions plex-hide-spoilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ def read_config(config_path = None):
if 'ignored_items' not in config or not isinstance(config['ignored_items'], list):
config['ignored_items'] = []

known_settings = ('plex_url', 'plex_token', 'libraries', 'ignored_items', 'hidden_summary_string',
'hidden_title_string', 'hide_summaries', 'hide_thumbnails', 'hide_titles', 'process_thumbnails')
errors = []
for setting in ('plex_url', 'plex_token', 'hidden_summary_string', 'hidden_title_string',
'hide_summaries', 'hide_thumbnails', 'hide_titles', 'libraries'):
for setting in known_settings:
if setting not in config or (isinstance(config[setting], str) and len(config[setting]) == 0):
errors.append(setting)
if errors:
Expand All @@ -173,14 +174,17 @@ def read_config(config_path = None):
sys.exit(8)

for setting in config:
if setting not in ('plex_url', 'plex_token', 'hidden_string', 'libraries', 'ignored_items', 'hidden_summary_string',
'hidden_title_string', 'hide_summaries', 'hide_thumbnails', 'hide_titles'):
if setting not in known_settings:
print(f"Warning: unknown setting \"{setting}\" in config.toml, ignoring")

if config['plex_url'] == "http://192.168.x.x:32400" or config['plex_token'] == "...":
print("You need to edit config.toml and change the Plex server settings to match your server!")
sys.exit(2)

if config['hide_thumbnails'] and not config['process_thumbnails']:
print("hide_thumbnails is enabled, but process_thumbnails is not; either disable hide_thumbnails or enable process_thumbnails!")
sys.exit(16)

# Not intended as a user-facing setting, but fits in config anyway
config['in_progress_string'] = "(Restore in progress...)"

Expand Down Expand Up @@ -313,7 +317,7 @@ def calculate_actions(items, also_hide=None, also_unhide=None):
action_list.append(Action(item, 'restore', 'summary'))
if has_hidden_title(item):
action_list.append(Action(item, 'restore', 'title'))
if has_hidden_thumbnail(item):
if config['process_thumbnails'] and has_hidden_thumbnail(item):
action_list.append(Action(item, 'restore', 'thumb'))
elif not item.isPlayed and not should_ignore_item(item):
# Cases 2+3+4: check each field in turn, and create up to one action per field and item
Expand All @@ -329,7 +333,7 @@ def calculate_actions(items, also_hide=None, also_unhide=None):
# Generic titles like "Episode 3" should not be hidden or restored, nor should entirely missing titles
continue
action_list.append(Action(item, 'hide' if config['hide_titles'] else 'restore', 'title'))
if item.type == 'episode' and has_hidden_thumbnail(item) != config['hide_thumbnails']:
if config['process_thumbnails'] and item.type == 'episode' and has_hidden_thumbnail(item) != config['hide_thumbnails']:
if config['hide_thumbnails'] and not has_thumbnail(item):
continue
action_list.append(Action(item, 'hide' if config['hide_thumbnails'] else 'restore', 'thumb'))
Expand All @@ -343,14 +347,14 @@ def calculate_actions(items, also_hide=None, also_unhide=None):
action_list.append(Action(also_unhide, 'restore', 'summary'))
if has_hidden_title(also_unhide):
action_list.append(Action(also_unhide, 'restore', 'title'))
if has_hidden_thumbnail(also_unhide):
if config['process_thumbnails'] and has_hidden_thumbnail(also_unhide):
action_list.append(Action(also_unhide, 'restore', 'thumb'))
if also_hide:
if config['hide_summaries']:
action_list.append(Action(also_hide, 'hide', 'summary'))
if config['hide_titles'] and also_hide.type == 'episode':
action_list.append(Action(also_hide, 'hide', 'title'))
if config['hide_thumbnails'] and also_hide.type == 'episode':
if config['process_thumbnails'] and config['hide_thumbnails'] and also_hide.type == 'episode':
action_list.append(Action(also_hide, 'hide', 'thumb'))

return sorted(action_list, key=compare_actions)
Expand All @@ -367,7 +371,7 @@ def calculate_actions_restore_all(items):
action_list.append(Action(item, 'restore', 'summary'))
if has_hidden_title(item):
action_list.append(Action(item, 'restore', 'title'))
if has_hidden_thumbnail(item):
if config['process_thumbnails'] and has_hidden_thumbnail(item):
action_list.append(Action(item, 'restore', 'thumb'))

return sorted(action_list, key=compare_actions)
Expand All @@ -384,6 +388,7 @@ def perform_single_action(plex, action):
item = action.item

if action.field == 'thumb':
assert config['process_thumbnails']
# Using editField on "thumb" doesn't work; even when locked, Plex redownloads the thumbnail
# on the next refresh. I assume that's a bug, but even so, we need a solution that works now.
if action.action == 'hide':
Expand Down Expand Up @@ -519,6 +524,9 @@ def main():
listener = PlexListener(plex)

if args.restore_all:
if not config['process_thumbnails']:
print("Warning: process_thumbnails set to false in the config file; if any thumbnails were previously hidden, " +
"they will *not* be restored unless process_thumbnails is set to true while restoring.")
if not args.quiet: print("This can take a while.")
items_by_guid = fetch_items(plex)
actions = calculate_actions_restore_all(items_by_guid.values())
Expand Down

0 comments on commit 1ac1085

Please sign in to comment.