dupecheck.py

Thu, 30 Nov 2017 00:20:52 +0100

author
mdd
date
Thu, 30 Nov 2017 00:20:52 +0100
changeset 24
a1bc75496992
parent 22
c18abd9198c0
child 32
df89a8fba2a2
permissions
-rwxr-xr-x

div0 bug if we have frame errors at the video stream beginning

3
569fa9a431b9 added filename duplicate checker
mdd
parents:
diff changeset
1 #!/usr/bin/env python
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
2 """
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
3 Toolkit / executable to scan for duplicate filenames in movie database
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
4
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
5 2017 by mdd
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
6 """
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
7
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
8 #pylint: disable=line-too-long
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
9 #pylint: disable=invalid-name
3
569fa9a431b9 added filename duplicate checker
mdd
parents:
diff changeset
10
4
a7e9e7974c22 prepare for speedup
mdd
parents: 3
diff changeset
11 from __future__ import print_function
3
569fa9a431b9 added filename duplicate checker
mdd
parents:
diff changeset
12 import os, sys
569fa9a431b9 added filename duplicate checker
mdd
parents:
diff changeset
13
22
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
14 def similarity(a, b):
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
15 if DIFFLIB:
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
16 return difflib.SequenceMatcher(a=a, b=b).ratio()
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
17 else:
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
18 return Levenshtein.ratio(a, b)
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
19
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
20 class dupechecker(object):
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
21 """
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
22 Simple class to scan multiple directories recursive,
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
23 build a list of movie filenames.
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
24 analyze the list for duplicates and dump them
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
25 """
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
26 def __init__(self):
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
27 self.basedir = ""
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
28 self.filelist = []
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
29 self.duplicates = {}
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
30 self.ratio = 0.85
3
569fa9a431b9 added filename duplicate checker
mdd
parents:
diff changeset
31
22
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
32
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
33 def reset(self):
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
34 self.filelist = []
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
35 self.duplicates = {}
3
569fa9a431b9 added filename duplicate checker
mdd
parents:
diff changeset
36
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
37 def scandir(self, basedir):
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
38 """
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
39 Scan a base directory for movie files and add them to
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
40 the list for analyze
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
41 """
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
42 self.basedir = basedir
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
43 print("Scanning directory: %s" % basedir)
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
44 for root, subdirs, files in os.walk(basedir):
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
45 for filename in files:
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
46 ext = os.path.splitext(filename)[1].lower()
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
47 if ext == ".ts":
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
48 #file_path = os.path.join(root, filename)
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
49 title = filename.split(" - ")
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
50 if len(title) == 1:
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
51 title = title[0]
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
52 else:
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
53 title = " - ".join(title[2:])
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
54 title = title[:-3].lower()
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
55 self.filelist.append([title, filename, root])
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
56 elif ext in ['.mkv', '.avi', '.mpg', '.mpeg', '.mp4']:
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
57 title = filename[:-4].lower()
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
58 self.filelist.append([title, filename, root])
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
59
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
60 def analyze(self):
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
61 """
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
62 Analyze the scanlist for duplicates
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
63 """
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
64 print("%i files to analyze, running duplicate testing loop..." % (
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
65 len(self.filelist)))
3
569fa9a431b9 added filename duplicate checker
mdd
parents:
diff changeset
66
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
67 listlen = len(self.filelist)
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
68 for idx in range(listlen):
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
69 if not self.filelist[idx]:
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
70 continue
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
71 print("\r%d %s\033[K" % (
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
72 idx, self.filelist[idx][0]), end='')
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
73 sys.stdout.flush()
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
74 for idx2 in range(idx + 1, listlen):
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
75 if self.filelist[idx2]:
22
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
76 if similarity(self.filelist[idx][0], self.filelist[idx2][0]) > self.ratio:
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
77 #print "possible duplicate %d %s" % (idx2, item2[0])
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
78 key = os.path.join(self.filelist[idx][2], self.filelist[idx][1])
22
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
79 if not key in self.duplicates:
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
80 self.duplicates[key] = []
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
81 self.duplicates[key].append(
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
82 os.path.join(
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
83 self.filelist[idx2][2],
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
84 self.filelist[idx2][1]
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
85 ))
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
86 # unset the found duplicate, so that this will not be scanned again
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
87 self.filelist[idx2] = None
22
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
88 print("\n\n")
3
569fa9a431b9 added filename duplicate checker
mdd
parents:
diff changeset
89
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
90 def output(self):
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
91 """
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
92 Dump found duplicates to console
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
93 """
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
94 idx = 1
22
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
95 for base in self.duplicates:
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
96 print("Duplicate file set #%i" % idx)
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
97 print(base)
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
98 for dup in self.duplicates[base]:
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
99 print(dup)
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
100 print()
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
101 idx += 1
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
102
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
103
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
104 if __name__ == "__main__":
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
105 # parse command line options
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
106 import argparse
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
107
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
108 parser = argparse.ArgumentParser(\
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
109 description='Movie database filename duplicate checker')
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
110 parser.add_argument('--ratio', type=float, default=0.85, \
22
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
111 help='filename duplicate threshold 0.1 < ratio 1.0 (default 0.85)')
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
112 parser.add_argument('--difflib', action='store_true', default=False, \
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
113 help='force the use of difflib instead Levenshtein')
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
114 parser.add_argument('basedir', metavar='basedir', nargs='+', \
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
115 help='one or more base directories')
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
116
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
117 args = parser.parse_args()
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
118 dupe = dupechecker()
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
119 dupe.ratio = args.ratio
22
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
120 if args.difflib:
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
121 DIFFLIB = True
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
122 import difflib
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
123 else:
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
124 try:
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
125 import Levenshtein
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
126 DIFFLIB = False
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
127 except ImportError:
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
128 import difflib
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
129 DIFFLIB = True
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
130 print("Consider 'pip install python-Levenshtein' for faster analyze")
c18abd9198c0 implemented Levenshtein algorithm for incredible speedup
mdd
parents: 21
diff changeset
131
21
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
132
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
133 for srcstr in args.basedir:
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
134 dupe.scandir(srcstr)
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
135 dupe.analyze()
1c0beeca2f9c cleanup dupechecker
mdd
parents: 15
diff changeset
136 dupe.output()

mercurial