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] 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))