Add upload option to hide_episode_spoilers.py
This commit is contained in:
parent
10287e3885
commit
a04b1a3c32
@ -21,8 +21,13 @@
|
|||||||
# --rating_key {rating_key} --blur 25
|
# --rating_key {rating_key} --blur 25
|
||||||
# To add a prefix to the summary (optional string prefix):
|
# To add a prefix to the summary (optional string prefix):
|
||||||
# --rating_key {rating_key} --summary_prefix "** SPOILERS **"
|
# --rating_key {rating_key} --summary_prefix "** SPOILERS **"
|
||||||
|
# To upload the episode artwork instead of creating a local asset (optional, for when the script cannot access the media folder):
|
||||||
|
# --rating_key {rating_key} --blur 25 --upload
|
||||||
# * Watched (optional):
|
# * Watched (optional):
|
||||||
# --rating_key {rating_key} --remove
|
# To remove the local asset episode artwork:
|
||||||
|
# --rating_key {rating_key} --remove
|
||||||
|
# To remove the uploaded episode artwork
|
||||||
|
# --rating_key {rating_key} --remove --upload
|
||||||
# Note:
|
# Note:
|
||||||
# * "Use local assets" must be enabled for the library in Plex (Manage Library > Edit > Advanced > Use local assets).
|
# * "Use local assets" must be enabled for the library in Plex (Manage Library > Edit > Advanced > Use local assets).
|
||||||
|
|
||||||
@ -40,7 +45,7 @@ PLEX_URL = os.getenv('PLEX_URL', PLEX_URL)
|
|||||||
PLEX_TOKEN = os.getenv('PLEX_TOKEN', PLEX_TOKEN)
|
PLEX_TOKEN = os.getenv('PLEX_TOKEN', PLEX_TOKEN)
|
||||||
|
|
||||||
|
|
||||||
def modify_episode_artwork(plex, rating_key, image=None, blur=None, summary_prefix=None, remove=False):
|
def modify_episode_artwork(plex, rating_key, image=None, blur=None, summary_prefix=None, remove=False, upload=False):
|
||||||
item = plex.fetchItem(rating_key)
|
item = plex.fetchItem(rating_key)
|
||||||
|
|
||||||
if item.type == 'show':
|
if item.type == 'show':
|
||||||
@ -61,21 +66,29 @@ def modify_episode_artwork(plex, rating_key, image=None, blur=None, summary_pref
|
|||||||
episode_filename = os.path.splitext(os.path.basename(episode_filepath))[0]
|
episode_filename = os.path.splitext(os.path.basename(episode_filepath))[0]
|
||||||
|
|
||||||
if remove:
|
if remove:
|
||||||
# Find image files with the same name as the episode
|
if upload:
|
||||||
for filename in os.listdir(episode_folder):
|
# Unlock and select the first poster
|
||||||
if filename.startswith(episode_filename) and filename.endswith(('.jpg', '.png')):
|
episode.unlockPoster().posters()[0].select()
|
||||||
# Delete the episode artwork image file
|
else:
|
||||||
os.remove(os.path.join(episode_folder, filename))
|
# Find image files with the same name as the episode
|
||||||
|
for filename in os.listdir(episode_folder):
|
||||||
|
if filename.startswith(episode_filename) and filename.endswith(('.jpg', '.png')):
|
||||||
|
# Delete the episode artwork image file
|
||||||
|
os.remove(os.path.join(episode_folder, filename))
|
||||||
|
|
||||||
# Unlock the summary so it will get updated on refresh
|
# Unlock the summary so it will get updated on refresh
|
||||||
episode.edit(**{'summary.locked': 0})
|
episode.editSummary(episode.summary, locked=False)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if image:
|
if image:
|
||||||
# File path to episode artwork using the same episode file name
|
if upload:
|
||||||
episode_artwork = os.path.splitext(episode_filepath)[0] + os.path.splitext(image)[1]
|
# Upload the image to the episode artwork
|
||||||
# Copy the image to the episode artwork
|
episode.uploadPoster(filepath=image)
|
||||||
shutil.copy2(image, episode_artwork)
|
else:
|
||||||
|
# File path to episode artwork using the same episode file name
|
||||||
|
episode_artwork = os.path.splitext(episode_filepath)[0] + os.path.splitext(image)[1]
|
||||||
|
# Copy the image to the episode artwork
|
||||||
|
shutil.copy2(image, episode_artwork)
|
||||||
|
|
||||||
elif blur:
|
elif blur:
|
||||||
# File path to episode artwork using the same episode file name
|
# File path to episode artwork using the same episode file name
|
||||||
@ -91,16 +104,17 @@ def modify_episode_artwork(plex, rating_key, image=None, blur=None, summary_pref
|
|||||||
r = requests.get(image_url, stream=True)
|
r = requests.get(image_url, stream=True)
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
r.raw.decode_content = True
|
r.raw.decode_content = True
|
||||||
# Copy the image to the episode artwork
|
if upload:
|
||||||
with open(episode_artwork, 'wb') as f:
|
# Upload the image to the episode artwork
|
||||||
shutil.copyfileobj(r.raw, f)
|
episode.uploadPoster(filepath=r.raw)
|
||||||
|
else:
|
||||||
|
# Copy the image to the episode artwork
|
||||||
|
with open(episode_artwork, 'wb') as f:
|
||||||
|
shutil.copyfileobj(r.raw, f)
|
||||||
|
|
||||||
if summary_prefix and not episode.summary.startswith(summary_prefix):
|
if summary_prefix and not episode.summary.startswith(summary_prefix):
|
||||||
# Use a zero-width space (\u200b) for blank lines
|
# Use a zero-width space (\u200b) for blank lines
|
||||||
episode.edit(**{
|
episode.editSummary(summary_prefix + '\n\u200b\n' + episode.summary)
|
||||||
'summary.value': summary_prefix + '\n\u200b\n' + episode.summary,
|
|
||||||
'summary.locked': 1
|
|
||||||
})
|
|
||||||
|
|
||||||
# Refresh metadata for the episode
|
# Refresh metadata for the episode
|
||||||
episode.refresh()
|
episode.refresh()
|
||||||
@ -113,6 +127,7 @@ if __name__ == "__main__":
|
|||||||
parser.add_argument('--blur', type=int, default=25)
|
parser.add_argument('--blur', type=int, default=25)
|
||||||
parser.add_argument('--summary_prefix', nargs='?', const='** SPOILERS **')
|
parser.add_argument('--summary_prefix', nargs='?', const='** SPOILERS **')
|
||||||
parser.add_argument('--remove', action='store_true')
|
parser.add_argument('--remove', action='store_true')
|
||||||
|
parser.add_argument('--upload', action='store_true')
|
||||||
opts = parser.parse_args()
|
opts = parser.parse_args()
|
||||||
|
|
||||||
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
|
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
|
||||||
|
Loading…
Reference in New Issue
Block a user