From 1db64239b671c48483e44aa53cbe4b88db3ab519 Mon Sep 17 00:00:00 2001 From: Konrad Rieck Date: Sat, 12 Dec 2015 17:52:19 +0100 Subject: [PATCH] cosmetic fixes --- README.md | 43 +++++++++++++++++++++++++++++-------------- perplex.py | 8 ++++++++ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e777d06..f1f5bf1 100644 --- a/README.md +++ b/README.md @@ -5,20 +5,35 @@ A Movie Renamer for Plex Metadata ## Overview -This simple Python script for renaming movie files in a Plex database. -[Plex](http://plex.tv/) is a software suite for organizing videos, -music and photos. It can identify movies based on their file names -and retrieve corresponding metadata. In practice, however, not all -file names are appropriate and thus some movies usually need to be -assigned to the corresponding movie titles manually. Unfortunately, +This is a simple script for renaming movie files based on Plex +metadata. [Plex](http://plex.tv/) is a software suite for organizing +videos, music and photos. It can identify movies based on their file +names and retrieve corresponding metadata. In practice, however, not +all files are named appropriate and thus some movies usually need to +be manually assigned to the corresponding movie title. Unfortunately, Plex is not able to rename these files and thus if the database is -initialized from the scratch these files need to be manually fixed -again. +deleted or re-installed, these files need to be manually fixed again. -The Python script `perplex` solves this problem. Given an -installation of Plex and the corresponding database, it lists all -imported movies and renames them according to the retrieved movie -metadata. To avoid breaking the current installation, the renamed -files are copied to another directory. +The script `perplex.py` solves this problem. Given an installation of +Plex and the corresponding database of metadata, the script extracts +all movies and renames the corresponding files according to the +retrieved metadata. To avoid breaking the current installation, the +renamed files are copied to another directory. -That's it. +## Usage + +First, extract all the metadata from the Plex database and save it. + + $ ./perplex.py --save movies.db --plex /var/plex + Analyzing Plex database: Found 335 movies and 365 files + Saving metadata to movies.db + +Now start the renaming process. The renamed movies are written to +the given directory. + + $ ./perplex.py --load movies.db --dest ./output + Loading metadata from movies.db + Copying renamed files to ./output + 100% |#################################################| Time: 0:00:00 + +Done. diff --git a/perplex.py b/perplex.py index c82bfd6..dac3391 100755 --- a/perplex.py +++ b/perplex.py @@ -19,6 +19,7 @@ def build_db(plex_dir, movies={}): """ Build movie database from sqlite database """ + print "Analyzing Plex database: ", dbfile = os.path.join(plex_dir, *dbpath.split("/")) db = sqlite3.connect(dbfile) @@ -35,11 +36,15 @@ def build_db(plex_dir, movies={}): 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 """ + files = 0 for id in movies: for file in db.execute(query % id): movies[id][2].append(file[0]) + files += 1 db.close() + print "Found %d movies and %d files" % (len(movies), files) + return movies @@ -99,13 +104,16 @@ def copy_rename(mapping, dest): movies = build_db(args.plex) mapping = build_map(movies) elif args.load: + print "Loading metadata from " + args.load movies, mapping = json.load(gzip.open(args.load)) else: print "Error: Provide a Plex database or stored database." sys.exit(-1) if args.save: + print "Saving metadata to " + args.save json.dump((movies, mapping), gzip.open(args.save, 'w')) if args.dest: + print "Copying renamed files to " + args.dest copy_rename(mapping, args.dest)