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 (#208)
Browse files Browse the repository at this point in the history
Co-authored-by: ReenigneArcher <[email protected]>
  • Loading branch information
zdimension and ReenigneArcher authored Nov 18, 2023
1 parent 12c78b8 commit 1a91752
Show file tree
Hide file tree
Showing 2 changed files with 64 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 @@ -106,19 +106,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',
),
)
94 changes: 61 additions & 33 deletions Contents/Code/plex_api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,46 +175,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 item.isLocked(field='summary'):
Log.Debug('Not overwriting locked summary for collection: {}'.format(item.title))
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
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))

if item.isLocked(field='theme'):
Log.Debug('Not overwriting locked theme for {}: {}'.format(item.type, item.title))
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))
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

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))
else:
if theme_url:
add_media(item=item, media_type='themes',
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 @@ -253,6 +259,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 item.isLocked(field=media_type_dict[media_type]['plex_field']):
Log.Info('Not overwriting locked "{}" for {}: {}'.format(
media_type_dict[media_type]['name'], item.type, item.title
))
return False

if media_file or media_url:
global plex
if not plex:
Expand Down Expand Up @@ -298,6 +310,22 @@ 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
edit_field = "{}.locked".format(media_type_dict[media_type]['plex_field'])
edits = {
edit_field: 0,
}
count = 0
while count < 3: # there are random read timeouts
try:
item.edit(**edits)
except requests.ReadTimeout as e:
Log.Error('{}: Error unlocking field: {}'.format(item.ratingKey, e))
time.sleep(5)
count += 1
else:
break
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 1a91752

Please sign in to comment.