Skip to content

Commit fd3e200

Browse files
author
Konrad Rieck
committed
minor fixes to improve matching
1 parent 4465fd6 commit fd3e200

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

perplex.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,32 @@
1212

1313
import progressbar as pb
1414

15-
# Default path to metadata database
16-
dbpath = "Plug-in Support/Databases/com.plexapp.plugins.library.db"
1715

16+
def find_db(plex_dir, name):
17+
""" Search for database file in directory """
18+
19+
for root, dirs, files in os.walk(plex_dir):
20+
for file in files:
21+
if file == name:
22+
return os.path.join(root, file)
23+
24+
return None
1825

1926
def build_db(plex_dir, movies={}):
2027
""" Build movie database from sqlite database """
2128

22-
print "Analyzing Plex database: ",
23-
dbfile = os.path.join(plex_dir, *dbpath.split("/"))
29+
print "Analyzing Plex database:",
30+
dbfile = find_db(plex_dir, "com.plexapp.plugins.library.db")
2431
db = sqlite3.connect(dbfile)
2532

2633
# Select only movies with year
2734
query = """
28-
SELECT id, title, year FROM metadata_items
29-
WHERE metadata_type = 1 AND year """
35+
SELECT id, title, originally_available_at FROM metadata_items
36+
WHERE metadata_type = 1 AND originally_available_at """
3037

3138
for row in db.execute(query):
32-
movies[row[0]] = (row[1], row[2], [])
39+
year = row[2].split('-')[0]
40+
movies[row[0]] = (row[1], year, [])
3341

3442
# Get files for each movie
3543
query = """
@@ -43,12 +51,12 @@ def build_db(plex_dir, movies={}):
4351
files += 1
4452

4553
db.close()
46-
print "Found %d movies and %d files" % (len(movies), files)
54+
print "%d movies and %d files" % (len(movies), files)
4755

4856
return movies
4957

5058

51-
def build_map(movies, mapping=[]):
59+
def build_map(movies, dest, mapping=[]):
5260
""" Build mapping to new names """
5361

5462
for title, year, files in movies.values():
@@ -59,9 +67,11 @@ def build_map(movies, mapping=[]):
5967
template += " - part%d" % (i + 1) if len(files) > 1 else ""
6068
template += ext
6169

62-
new_name = os.path.join(*template.split("/"))
70+
dest = os.path.normpath(dest)
71+
new_name = os.path.join(dest, *template.split("/"))
6372
mapping.append((old_name, new_name))
6473

74+
mapping = filter(lambda (x,y): x.lower() != y.lower(), mapping)
6575
return mapping
6676

6777

@@ -102,18 +112,21 @@ def copy_rename(mapping, dest):
102112

103113
if args.plex:
104114
movies = build_db(args.plex)
105-
mapping = build_map(movies)
106115
elif args.load:
107116
print "Loading metadata from " + args.load
108-
movies, mapping = json.load(gzip.open(args.load))
117+
movies = json.load(gzip.open(args.load))
109118
else:
110119
print "Error: Provide a Plex database or stored database."
111120
sys.exit(-1)
112121

113122
if args.save:
114123
print "Saving metadata to " + args.save
115-
json.dump((movies, mapping), gzip.open(args.save, 'w'))
124+
json.dump(movies, gzip.open(args.save, 'w'))
125+
116126

117127
if args.dest:
118-
print "Copying renamed files to " + args.dest
119-
copy_rename(mapping, args.dest)
128+
print "Building file mapping for " + args.dest
129+
mapping = build_map(movies, args.dest)
130+
131+
#print "Copying renamed files to " + args.dest
132+
#copy_rename(mapping, args.dest)

0 commit comments

Comments
 (0)