Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
fix: don't update locked fields + unlock fields after update
Browse files Browse the repository at this point in the history
  • Loading branch information
zdimension committed Nov 9, 2023
1 parent b463749 commit bc41508
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 33 deletions.
3 changes: 3 additions & 0 deletions Contents/Code/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,22 @@
name='art',
themerr_data_key='art_url',
remove_pref='bool_remove_unused_art',
plex_field='art',
),
posters=dict(
method=lambda item: item.uploadPoster,
type='posters',
name='poster',
themerr_data_key='poster_url',
remove_pref='bool_remove_unused_posters',
plex_field='thumb',
),
themes=dict(
method=lambda item: item.uploadTheme,
type='themes',
name='theme',
themerr_data_key='youtube_theme_url',
remove_pref='bool_remove_unused_theme_songs',
plex_field='theme',
),
)
105 changes: 72 additions & 33 deletions Contents/Code/plex_api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,30 @@ def setup_plexapi():
return plex


def is_field_locked(item, field_name):
# type: (any, str) -> bool
"""
Check if the specified field is locked -- i.e. if the user has manually set a value for it and does not want
it to be updated by an automated process.
Parameters
----------
item : any
The Plex item to check.
field_name : str
The name of the field to check.
Returns
-------
bool
True if the field is locked, False otherwise.
"""
for field in item.fields:
if field.name == field_name:
return field.locked

Check warning on line 110 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L109-L110

Added lines #L109 - L110 were not covered by tests
return False


def update_plex_item(rating_key):
# type: (int) -> bool
"""
Expand Down Expand Up @@ -170,46 +194,52 @@ def update_plex_item(rating_key):
else:
add_media(item=item, media_type='art', media_url_id=data['backdrop_path'], media_url=url)
# update summary
try:
summary = data['overview']
except KeyError:
pass
if is_field_locked(item, "summary"):
Log.Info('Not overwriting locked summary for collection: {}'.format(item.title))

Check warning on line 198 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L197-L198

Added lines #L197 - L198 were not covered by tests
else:
if item.summary != summary:
Log.Info('Updating summary for collection: {}'.format(item.title))
try:
item.editSummary(summary=summary, locked=False)
except Exception as e:
Log.Error('{}: Error updating summary: {}'.format(item.ratingKey, e))

# get youtube_url
try:
yt_video_url = data['youtube_theme_url']
except KeyError:
Log.Info('{}: No theme song found for {} ({})'.format(item.ratingKey, item.title, item.year))
try:
summary = data['overview']
except KeyError:
pass

Check warning on line 203 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L200-L203

Added lines #L200 - L203 were not covered by tests
else:
if item.summary != summary:
Log.Info('Updating summary for collection: {}'.format(item.title))
try:
item.editSummary(summary=summary, locked=False)
except Exception as e:
Log.Error('{}: Error updating summary: {}'.format(item.ratingKey, e))

Check warning on line 210 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L205-L210

Added lines #L205 - L210 were not covered by tests

if is_field_locked(item, "theme"):
Log.Info('Not overwriting locked theme for {}: {}'.format(item.type, item.title))

Check warning on line 213 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L213

Added line #L213 was not covered by tests
else:
settings_hash = general_helper.get_themerr_settings_hash()
themerr_data = general_helper.get_themerr_json_data(item=item)

# get youtube_url
try:
skip = themerr_data['settings_hash'] == settings_hash \
and themerr_data[media_type_dict['themes']['themerr_data_key']] == yt_video_url
yt_video_url = data['youtube_theme_url']
except KeyError:
skip = False

if skip:
Log.Info('Skipping {} for type: {}, title: {}, rating_key: {}'.format(
media_type_dict['themes']['name'], item.type, item.title, item.ratingKey
))
Log.Info('{}: No theme song found for {} ({})'.format(item.ratingKey, item.title, item.year))

Check warning on line 219 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L219

Added line #L219 was not covered by tests
else:
settings_hash = general_helper.get_themerr_settings_hash()
themerr_data = general_helper.get_themerr_json_data(item=item)

try:
theme_url = process_youtube(url=yt_video_url)
except Exception as e:
Log.Exception('{}: Error processing youtube url: {}'.format(item.ratingKey, e))
skip = themerr_data['settings_hash'] == settings_hash \
and themerr_data[media_type_dict['themes']['themerr_data_key']] == yt_video_url
except KeyError:
skip = False

Check warning on line 228 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L227-L228

Added lines #L227 - L228 were not covered by tests

if skip:
Log.Info('Skipping {} for type: {}, title: {}, rating_key: {}'.format(
media_type_dict['themes']['name'], item.type, item.title, item.ratingKey
))
else:
if theme_url:
add_media(item=item, media_type='themes',
media_url_id=yt_video_url, media_url=theme_url)
try:
theme_url = process_youtube(url=yt_video_url)
except Exception as e:
Log.Exception('{}: Error processing youtube url: {}'.format(item.ratingKey, e))

Check warning on line 238 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L235-L238

Added lines #L235 - L238 were not covered by tests
else:
if theme_url:
add_media(item=item, media_type='themes',

Check warning on line 241 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L240-L241

Added lines #L240 - L241 were not covered by tests
media_url_id=yt_video_url, media_url=theme_url)


def add_media(item, media_type, media_url_id, media_file=None, media_url=None):
Expand Down Expand Up @@ -248,6 +278,12 @@ def add_media(item, media_type, media_url_id, media_file=None, media_url=None):
settings_hash = general_helper.get_themerr_settings_hash()
themerr_data = general_helper.get_themerr_json_data(item=item)

if is_field_locked(item, media_type_dict[media_type]['plex_field']):
Log.Info('Not overwriting locked "{}" for {}: {}'.format(

Check warning on line 282 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L281-L282

Added lines #L281 - L282 were not covered by tests
media_type_dict[media_type]['name'], item.type, item.title
))
return False

Check warning on line 285 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L285

Added line #L285 was not covered by tests

if media_file or media_url:
global plex
if not plex:
Expand Down Expand Up @@ -293,6 +329,9 @@ def add_media(item, media_type, media_url_id, media_file=None, media_url=None):
new_themerr_data[media_type_dict[media_type]['themerr_data_key']] = media_url_id

general_helper.update_themerr_data_file(item=item, new_themerr_data=new_themerr_data)

# unlock the field since it contains an automatically added value
getattr(item, "_edit")(**{'{}.locked'.format(media_type_dict[media_type]['plex_field']): 0})

Check warning on line 334 in Contents/Code/plex_api_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/plex_api_helper.py#L334

Added line #L334 was not covered by tests
else:
Log.Debug('Could not upload {} for type: {}, title: {}, rating_key: {}'.format(
media_type_dict[media_type]['name'], item.type, item.title, item.ratingKey
Expand Down

0 comments on commit bc41508

Please sign in to comment.