-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_compare_v2.py
179 lines (134 loc) · 5.23 KB
/
file_compare_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import sys
import re
from time import time
import logging
from collections import Counter
SUPPORTED_EXTENSIONS = ['mp3', 'm4a', 'wav', 'wma', 'flac']
LOG_FILENAME = 'report.log'
log = logging.getLogger()
def set_logger(filename=None, level=None):
filename = filename or'log.log'
level = level or 'INFO'
format = ("%(asctime)s [%(levelname)-5s] | %(message)s")
# create the log directory if it doesn't exist
# if filename and not os.path.exists(os.path.dirname(filename)):
# os.makedirs(os.path.dirname(filename))
logging.basicConfig(level=getattr(logging, level),
format=format,
datefmt='%Y-%m-%d %H:%M:%S',
filename=filename,
filemode='w')
class Duplicates(object):
'''
Store Duplicates in an array
'''
def __init__(self):
self.artist = ''
self.songs = []
self.sizes = []
self.folders = []
self.paths = []
class Song():
'''
Store properties for a song
'''
def __init__(self):
self.artist = ''
self.name = ''
self.size = 0
self.folder = ''
self.path = ''
self.extension = ''
class SongLibrary(object):
def __init__(self, musicDir):
self.rootMusicDir = musicDir
self.supported_extensions = SUPPORTED_EXTENSIONS
self.library = []
self.nb_songs = 0
self.nb_duplicates = 0
self.duplicates = []
self.dupByArtist = Counter()
def getSongs(self):
self.__getSongs(self.rootMusicDir)
self.nb_songs = len(self.library)
def __getSongs(self, musicDir):
musicDir = os.path.abspath(musicDir)
filesInCurDir = os.listdir(musicDir)
for file in filesInCurDir:
curFile = os.path.join(musicDir, file)
if os.path.isfile(curFile):
curFileExtension = curFile[-3:]
if curFileExtension in self.supported_extensions:
self.nb_songs += 1
# We add to a list all the files that contain
split_path = (os.path.abspath(curFile)).split('\\')
base_path = [x for x in split_path if x not in self.rootMusicDir.split('\\')]
song = Song()
song.size = float(os.stat(curFile).st_size / 1024) # in KB
song.path = base_path
song.extension = curFileExtension
song.artist = base_path[0]
song.name = base_path[-1]
try:
song.folder = base_path[-2]
except:
song.folder = 'ROOT_FOLDER'
self.library.append(song)
else:
self.__getSongs(curFile)
def getDuplicates(self):
self.__getDuplicates(self.library)
self.__listDuplicatesByArtist()
def __getDuplicates(self, songLibrary):
progress = 0
log.info("%s files to process" % self.nb_songs)
while songLibrary:
# Remove processed song
song = songLibrary.pop(0)
song_name = song.name
song_folder = song.folder
artist = song.artist
song_size = song.size
cleaned_song_name = re.sub("^[\d]{2}", '', song_name)
song_path = ('/').join(song.path)
for index, song__toCompare in enumerate([x for x in songLibrary if x.artist == artist]):
song_name_to_compare = song__toCompare.name
song_folder_to_compare = song__toCompare.folder
song_size_to_compare = song__toCompare.size
song_path_to_compare = ('/').join(song__toCompare.path)
if song_name_to_compare.find(cleaned_song_name) != -1:
dup = Duplicates()
dup.artist = artist
dup.songs = [song_name, song_name_to_compare]
dup.sizes = [song_size, song_size_to_compare]
dup.folders = [song_folder, song_folder_to_compare]
dup.paths = [song_path, song_path_to_compare]
self.duplicates.append(dup)
self.nb_duplicates += 1
progress += 1 # songs processed so far. to be used as a counter
print('%s duplicates found' % self.nb_duplicates + '\n')
def __listDuplicatesByArtist(self):
dups_dict = {}
for dup in self.duplicates:
self.dupByArtist[dup.artist] += 1
if dups_dict.get(dup.artist, None):
dups_dict[dup.artist].append(dup)
else:
dups_dict[dup.artist] = [dup]
print self.dupByArtist
for k, v in self.dupByArtist.items():
log.info('\n' + '='*250)
log.info(k.upper())
for dup in dups_dict[k]:
log.info((dup.sizes, dup.paths))
if __name__ == "__main__":
set_logger(filename=LOG_FILENAME, level='INFO')
rootdir = sys.argv[1]
# Benchmark time to process
startTime = time()
# Get list of files
SongLib = SongLibrary(rootdir)
SongLib.getSongs()
SongLib.getDuplicates()
print('Runtime: %2.2f' % (time() - startTime))