12
12
13
13
import progressbar as pb
14
14
15
- # Default path to metadata database
16
- dbpath = "Plug-in Support/Databases/com.plexapp.plugins.library.db"
17
15
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
18
25
19
26
def build_db (plex_dir , movies = {}):
20
27
""" Build movie database from sqlite database """
21
28
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" )
24
31
db = sqlite3 .connect (dbfile )
25
32
26
33
# Select only movies with year
27
34
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 """
30
37
31
38
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 , [])
33
41
34
42
# Get files for each movie
35
43
query = """
@@ -43,12 +51,12 @@ def build_db(plex_dir, movies={}):
43
51
files += 1
44
52
45
53
db .close ()
46
- print "Found %d movies and %d files" % (len (movies ), files )
54
+ print "%d movies and %d files" % (len (movies ), files )
47
55
48
56
return movies
49
57
50
58
51
- def build_map (movies , mapping = []):
59
+ def build_map (movies , dest , mapping = []):
52
60
""" Build mapping to new names """
53
61
54
62
for title , year , files in movies .values ():
@@ -59,9 +67,11 @@ def build_map(movies, mapping=[]):
59
67
template += " - part%d" % (i + 1 ) if len (files ) > 1 else ""
60
68
template += ext
61
69
62
- new_name = os .path .join (* template .split ("/" ))
70
+ dest = os .path .normpath (dest )
71
+ new_name = os .path .join (dest , * template .split ("/" ))
63
72
mapping .append ((old_name , new_name ))
64
73
74
+ mapping = filter (lambda (x ,y ): x .lower () != y .lower (), mapping )
65
75
return mapping
66
76
67
77
@@ -102,18 +112,21 @@ def copy_rename(mapping, dest):
102
112
103
113
if args .plex :
104
114
movies = build_db (args .plex )
105
- mapping = build_map (movies )
106
115
elif args .load :
107
116
print "Loading metadata from " + args .load
108
- movies , mapping = json .load (gzip .open (args .load ))
117
+ movies = json .load (gzip .open (args .load ))
109
118
else :
110
119
print "Error: Provide a Plex database or stored database."
111
120
sys .exit (- 1 )
112
121
113
122
if args .save :
114
123
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
+
116
126
117
127
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