From 58c8501529a4421c1a2ef9d74f39dc8077006231 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sun, 2 Jan 2022 11:27:11 -0800 Subject: [PATCH 1/3] Add merge_multiepisodes.py --- utility/merge_multiepisodes.py | 90 ++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 utility/merge_multiepisodes.py diff --git a/utility/merge_multiepisodes.py b/utility/merge_multiepisodes.py new file mode 100644 index 0000000..8ab1f08 --- /dev/null +++ b/utility/merge_multiepisodes.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +''' +Description: Automatically merge multi-episode files in Plex into a single entry. +Author: /u/SwiftPanda16 +Requires: plexapi +Notes: + * All episodes **MUST** be organized correctly according to Plex's "Multiple Episodes in a Single File". + https://support.plex.tv/articles/naming-and-organizing-your-tv-show-files/#toc-4 + + * Episode titles, summaries, and tags will be appended to the first episode of the group. + + * The episode number will display as the first episode in the group. + e.g. Episode 1-2 --> Episode 1 (It is not possible to display "Episode 1 & 2") + e.g. Episode 3-4 --> Episode 3 (It is not possible to reduce this to "Episode 2") + e.g. Episode 5-8 --> Episode 5 (It is possible to merge an arbitrary number of episodes) + +Usage: + python merge_multiepisodes.py --library "TV Shows" --show "SpongeBob SquarePants" +''' + +import argparse +import os +from collections import defaultdict +from plexapi.server import PlexServer + + +# ## EDIT SETTINGS ## + +PLEX_URL = '' +PLEX_TOKEN = '' + +# Environmental Variables +PLEX_URL = os.getenv('PLEX_URL', PLEX_URL) +PLEX_TOKEN = os.getenv('PLEX_TOKEN', PLEX_TOKEN) + + +def group_episodes(plex, library, show): + groups = defaultdict(list) + + show = plex.library.section(library).get(show) + for episode in show.episodes(): + groups[episode.locations[0]].append(episode) + + for first, *episodes in groups.values(): + if not episodes: + continue + + title = first.title + ' / ' + titleSort = first.titleSort + ' / ' + summary = first.summary + '\n\n' + writers = [] + directors = [] + + for episode in episodes: + title += episode.title + ' / ' + titleSort += episode.titleSort + ' / ' + summary += episode.summary + '\n\n' + writers.extend([writer.tag for writer in episode.writers]) + directors.extend([director.tag for director in episode.directors]) + + merge(first, episodes) + first.edit(**{ + 'title.value': title[:-3], + 'title.locked': 1, + 'titleSort.value': titleSort[:-3], + 'titleSort.locked': 1, + 'summary.value': summary[:-2], + 'summary.locked': 1, + 'originallyAvailableAt.locked': 1, + 'contentRating.locked': 1 + }) + first.addWriter(writers, locked=True) + first.addDirector(directors, locked=True) + + +def merge(first, episodes): + key = '%s/merge?ids=%s' % (first.key, ','.join([str(r.ratingKey) for r in episodes])) + first._server.query(key, method=first._server._session.put) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--library', required=True) + parser.add_argument('--show', required=True) + opts = parser.parse_args() + + plex = PlexServer(PLEX_URL, PLEX_TOKEN) + group_episodes(plex, **vars(opts)) From 07b0c942abf4c581e02c03799ab50cde7f45fc64 Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Thu, 20 Jan 2022 21:07:40 -0800 Subject: [PATCH 2/3] Add episode renumbering --- utility/merge_multiepisodes.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/utility/merge_multiepisodes.py b/utility/merge_multiepisodes.py index 8ab1f08..b3a85f5 100644 --- a/utility/merge_multiepisodes.py +++ b/utility/merge_multiepisodes.py @@ -11,11 +11,6 @@ Notes: * Episode titles, summaries, and tags will be appended to the first episode of the group. - * The episode number will display as the first episode in the group. - e.g. Episode 1-2 --> Episode 1 (It is not possible to display "Episode 1 & 2") - e.g. Episode 3-4 --> Episode 3 (It is not possible to reduce this to "Episode 2") - e.g. Episode 5-8 --> Episode 5 (It is possible to merge an arbitrary number of episodes) - Usage: python merge_multiepisodes.py --library "TV Shows" --show "SpongeBob SquarePants" ''' @@ -43,7 +38,7 @@ def group_episodes(plex, library, show): for episode in show.episodes(): groups[episode.locations[0]].append(episode) - for first, *episodes in groups.values(): + for index, (first, *episodes) in enumerate(groups.values()): if not episodes: continue @@ -69,7 +64,9 @@ def group_episodes(plex, library, show): 'summary.value': summary[:-2], 'summary.locked': 1, 'originallyAvailableAt.locked': 1, - 'contentRating.locked': 1 + 'contentRating.locked': 1, + 'index.value': index + 1, + 'index.locked': 1 }) first.addWriter(writers, locked=True) first.addDirector(directors, locked=True) From b2f420b6fba822d7209fb2d328f904051211e668 Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Thu, 20 Jan 2022 21:45:22 -0800 Subject: [PATCH 3/3] Add note about reverting changes and splitting episodes --- utility/merge_multiepisodes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utility/merge_multiepisodes.py b/utility/merge_multiepisodes.py index b3a85f5..72e458e 100644 --- a/utility/merge_multiepisodes.py +++ b/utility/merge_multiepisodes.py @@ -11,6 +11,8 @@ Notes: * Episode titles, summaries, and tags will be appended to the first episode of the group. + * To revert the changes and split the episodes again, the show must be removed and re-added to Plex (aka Plex Dance). + Usage: python merge_multiepisodes.py --library "TV Shows" --show "SpongeBob SquarePants" '''