-
Notifications
You must be signed in to change notification settings - Fork 0
/
abcde.py
320 lines (252 loc) · 9.73 KB
/
abcde.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/python3
import glob
import os
import shutil
import datetime
# 1. copy albums from newCDs to extHD mp3, ogg, flac folders.
# 2. examine mp3, ogg, flac folders for missing albums.
# 3. examine extHD albums for duplicate tracks (varying track titles)
# and let user choose which to keep.
# 4. (maybe) edit mp3 filenames to be Windows-compatible.
# (removing special characters and/or converting to ASCII)
EXTS = ('flac', 'mp3', 'ogg')
EXT_HD_PATH = '/media/plimley/MUSIC/'
HOME_PATH = '/home/plimley/Music/'
# NEW_CDS_PATH = '/home/plimley/Music/new CDs/' # this one is not by ext
NEW_CDS_PATH = os.path.join(EXT_HD_PATH, 'new CDs')
SANSA_PATH = '/media/plimley/sansa-sd'
def main1():
"""Copy albums from newCDs to external HD mp3, ogg, flac folders."""
source_album_list = get_album_list(NEW_CDS_PATH)
for ext in EXTS:
target_dir = os.path.join(EXT_HD_PATH, ext, 'CDs')
target_album_list = get_album_list(target_dir)
target_albumnames = [a.name for a in target_album_list]
for album in source_album_list:
if album.name not in target_albumnames:
print('Copying {} into {}'.format(album.name, ext))
album.copy(target_dir, ext=ext)
def check_sansa():
"""Find albums that are in extHD ogg or mp3, but not on sansa.
Both external HD and Sansa should be plugged in."""
sansa_list = get_album_list(SANSA_PATH)
sansa_names = [a.name for a in sansa_list]
checked_list = []
for ext in EXTS[1:]:
album_list = get_album_list(os.path.join(EXT_HD_PATH, ext, 'CDs'))
for album in album_list:
if (album.name not in sansa_names and
album.name not in checked_list):
print('Missing {}'.format(album.name))
checked_list.append(album.name)
def main():
# tests
albumdir = '/home/plimley/Music/new CDs/Sara Bareilles - Little Voice'
AlbumInstance(albumdir)
get_album_list('/home/plimley/Music/new CDs')
def list_duplicates_on_ext():
"""
Go through
"""
ext = EXTS[1]
albumdir = os.path.join(EXT_HD_PATH, ext, 'CDs')
album_list = get_album_list(albumdir, verbosity=True)
print('Begin duplicate tracks')
n = 0
for album in album_list:
album.rm_duplicate_tracks(dry_run=True)
n += 1
if n % 10 == 0:
input() # wait for Enter
class FileInstance(object):
"""
Represents one file in one location on disk.
"""
def __init__(self, filepath):
"""
"""
self.fullpath = filepath
self.path, self.filename = os.path.split(filepath)
self.name, ext = os.path.splitext(self.filename)
self.ext = ext[1:] # drop the .
self.stat = os.stat(filepath)
self.is_music = (self.ext in EXTS)
if self.is_music:
try:
num_str, name_str = self.name.split(sep=' - ', maxsplit=1)
except ValueError:
# failed to split
num_str = '0'
name_str = self.name
try:
self.track_num = int(num_str)
except ValueError:
self.track_num = 0
self.track_name = name_str
self.bytes = self.stat.st_size
self.mtime = datetime.datetime.fromtimestamp(self.stat.st_mtime)
class AlbumInstance(object):
"""
Represents one music album in one location on disk.
"""
def __init__(self, filepath):
"""
Initialize the album given the path of the directory.
"""
self.location = filepath
self.get_names()
# filelist does not include the path
self.filelist = [FileInstance(os.path.join(filepath, s))
for s in os.listdir(filepath)
if os.path.isfile(os.path.join(filepath, s))]
self.musiclist = [f for f in self.filelist if f.is_music]
if any([f.track_num == 0 for f in self.musiclist]):
print('File string error in ' + self.name)
self.list_by_ext = {}
self.has_ext = {}
for ftype in EXTS:
self.list_by_ext[ftype] = [
f for f in self.musiclist if f.ext == ftype]
self.has_ext[ftype] = bool(self.list_by_ext[ftype])
self.misc_files = [f for f in self.filelist if not f.is_music]
if any(self.has_ext.values()):
self.multidisc = self.musiclist[0].track_num > 99
self.track_nums = set([f.track_num for f in self.musiclist])
self.n_tracks = len(self.track_nums)
def get_names(self):
# full name
head, tail = os.path.split(self.location)
if tail == '':
_, tail = os.path.split(head)
self.name = tail
# artist and album names
try:
artist_name, album_name = self.name.split(sep=' - ', maxsplit=1)
except ValueError:
artist_name, album_name = ('', self.name)
self.artist = artist_name
self.album = album_name
def copy(self, target_dir, ext=None, overwrite=False, rm_original=False):
"""
Copy the album dir into target_dir.
if ext is not None, only copy files of this ext.
"""
if ext not in EXTS:
ext = None
if not os.path.isdir(target_dir):
os.mkdir(target_dir)
dest_dir = os.path.join(target_dir, self.name)
if os.path.isdir(dest_dir) and overwrite:
print('Warning: {} already exists, proceeding anyway'.format(
dest_dir))
elif os.path.isdir(dest_dir):
return None
else:
os.mkdir(dest_dir)
if ext is None:
filelist = self.filelist
else:
filelist = self.list_by_ext[ext] + self.misc_files
for f in filelist:
shutil.copy2(f.fullpath,
os.path.join(dest_dir, f.filename))
if rm_original:
while filelist:
self.del_file(filelist.pop())
def split_exts(self, target_dir, subdir=None,
overwrite=False, rm_original=False):
"""
For album located in src_dir/album_dir:
copy src_dir/album_dir/*.mp3 to target_dir/mp3/subdir/album_dir/*.mp3
and similarly for each extension type.
"""
if not all(self.has_ext.values()):
raise AbcdeException('File types not found')
for ftype in EXTS:
if subdir is None:
this_dest_dir = os.path.join(target_dir, ftype)
else:
this_dest_dir = os.path.join(target_dir, ftype, subdir)
self.copy(this_dest_dir, ext=ftype,
overwrite=overwrite, rm_original=rm_original)
def del_file(self, file_obj):
"""
Remove file file_obj, also removing references in the album lists.
file_obj is a FileInstance object.
"""
if file_obj not in self.filelist:
raise AbcdeException('Cannot delete file: file object not found')
self.filelist.remove(file_obj)
for ftype in EXTS:
if file_obj in self.list_by_ext[ftype]:
self.list_by_ext[ftype].remove(file_obj)
os.remove(file_obj.fullpath)
def rm_duplicate_tracks(self, dry_run=True):
"""
Examine album files for duplicate tracks with varied names.
"""
for ftype in EXTS:
if (self.has_ext[ftype] and
len(self.list_by_ext[ftype]) > len(self.track_nums)):
print('Found duplicate ' + ftype + ' files in ' + self.name)
dup_list = []
for i in range(max(self.track_nums)):
t = i + 1 # track number from 1 to N, not 0 to N-1
this = [f for f in self.list_by_ext[ftype]
if f.track_num == t]
if len(this) > 1:
dup_list.append(this)
print(' Track ' + str(t) + ':')
for trk in this:
print(' ' + trk.name)
if not dry_run:
# resolve duplicates here or so
pass
def __str__(self):
return self.location
def get_album_list(dirname, verbosity=False):
"""
Return a list of albums from the contents of directory, dirname.
"""
album_dir_list = [os.path.join(dirname, d)
for d in os.listdir(dirname)
if os.path.isdir(os.path.join(dirname, d))]
album_dir_list.sort()
album_list = []
for a in album_dir_list:
try:
if os.path.isdir(a):
this_album = AlbumInstance(a)
album_list.append(this_album)
except PermissionError:
# lost+found
pass
print('.', end='')
return album_list
def split_albums(source_dir, target_dir,
overwrite=False, rm_original=False, verbosity=1):
"""
Split all albums in source_dir according to extensions, to
target_dir/mp3/album-name/ (for example)
"""
albumlist = get_album_list(source_dir)
for album in albumlist:
try:
album.split_exts(
target_dir, overwrite=overwrite, rm_original=rm_original)
if verbosity > 0:
print('Completed ' + album.name)
except AbcdeException:
if verbosity > 0:
print(' Error on ' + album.name)
def copy_folders(source_dir, target_dir,
overwrite=False, rm_original=False, verbosity=1):
"""
Copy source_dir/mp3/* to target_dir/mp3/*, and the same for ogg and flac
"""
# TODO
pass
class AbcdeException(Exception):
pass
if __name__ == '__main__':
main()