From 13e11ab8665aa2e3303f91a6270a3ae2a9b2947b Mon Sep 17 00:00:00 2001 From: Konrad Rieck Date: Fri, 11 Dec 2015 17:37:23 +0100 Subject: [PATCH] initial import --- .gitignore | 3 ++ perplex.py | 107 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 111 insertions(+) create mode 100644 .gitignore create mode 100644 perplex.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3647e1a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +plex +*.gz diff --git a/perplex.py b/perplex.py new file mode 100644 index 0000000..4e4c300 --- /dev/null +++ b/perplex.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python +# Perplex - Plex-base Movie Renamer +# Copyright (c) 2015 Konrad Rieck (konrad@mlsec.org) + +import argparse +import sqlite3 +import os +import shutil +import json +import gzip + +import progressbar as pb + +# Default path to metadata database +dbpath = "Plug-in Support/Databases/com.plexapp.plugins.library.db" + +def build_db(plex_dir, movies = {}): + """ Build movie database from sqlite database """ + + dbfile = os.path.join(plex_dir, *dbpath.split("/")) + db = sqlite3.connect(dbfile) + + # Select only movies with year + query = """ + SELECT id, title, year FROM metadata_items + WHERE metadata_type = 1 AND year """ + + for row in db.execute(query): + movies[row[0]] = (row[1], row[2], []) + + # Get files for each movie + query = """ + SELECT mp.file FROM media_items AS mi, media_parts AS mp + WHERE mi.metadata_item_id = %s AND mi.id = mp.media_item_id """ + + for id in movies: + for file in db.execute(query % id): + movies[id][2].append(file[0]) + + db.close() + return movies + + +def build_map(movies, mapping = []): + """ Build mapping to new names """ + + for title, year, files in movies.values(): + for i, old_name in enumerate(files): + _, ext = os.path.splitext(old_name) + + template = "%s (%s)/%s (%s)" % (title, year, title, year) + template += " - %.2d" % i if len(files) > 1 else "" + template += ext + + new_name = os.path.join(*template.split("/")) + mapping.append((old_name, new_name)) + + return mapping + + +def copy_rename(mapping, dest): + + pbar = pb.ProgressBar() + for old_name, new_name in pbar(mapping): + dp = os.path.join(dest, os.path.dirname(new_name)) + fp = os.path.join(dp, os.path.basename(new_name)) + + try: + if not os.path.exists(dp): + os.makedirs(dp) + + if not os.path.exists(fp): + shutil.copy(old_name, fp) + + except Exception, e: + print str(e) + + + +if __name__ == "__main__": + # Parse command-line arguments + parser = argparse.ArgumentParser(description='Plex-based Movie Renamer.') + parser.add_argument('--plex', metavar='', type=str, + help='set directory of Plex database.') + parser.add_argument('--dest', metavar='', type=str, + help='copy and rename files to directory') + parser.add_argument('--save', metavar='', type=str, + help='save mapping of movie titles') + parser.add_argument('--load', metavar='', type=str, + help='load mapping of movie titles') + + args = parser.parse_args() + + if args.plex: + movies = build_db(args.plex) + mapping = build_map(movies) + elif args.load: + mapping = json.load(gzip.open(args.load)) + else: + print "Error: Provide a Plex database or database of movie titles." + sys.exit(-1) + + if args.save: + json.dump(mapping, gzip.open(args.save, 'w')) + + if args.dest: + copy_rename(mapping, args.dest) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..47e16e1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +progressbar==2.3