-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp3archive.py
350 lines (306 loc) · 11.7 KB
/
mp3archive.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/python3
#coding:utf-8
# tested in win
__version__ = 20201210
import os
import re
import shutil
import argparse
import configparser
import sqlite3
import sys
from prettytable import from_db_cursor
# customized module
from config import logfile,inventory,topdir,archdir,evadir,musicure,coverdir,db,albumlist
from mtag import readtag
import myfs
from mylog import mylogger,get_funcname
ml = mylogger(logfile)
class database():
def create(self):
conn = sqlite3.connect(db)
cursor = conn.cursor()
cursor.execute('create table music (\
fullname varchar(200) primary key,\
artist varchar(100),\
year varchar(5),\
album varchar(100) )'
)
cursor.close()
conn.close()
def insert(self,alist):
ml = mylogger(logfile,get_funcname())
conn = sqlite3.connect(db)
cursor = conn.cursor()
ml.dbg(alist)
fullname = alist[0]
artist = alist[1]
year = alist[2]
album = alist[3]
try:
cursor.execute("insert into music values (?,?,?,?)",(fullname,artist,year,album))
except sqlite3.Integrityerr as e:
ml.err(e)
ml.err('duplicate entry')
cursor.close()
conn.commit()
conn.close()
def query(self,q='',keyword=''):
ml = mylogger(logfile,get_funcname())
if q in ['fullname','artist','year','album']:
cmd = 'select * from music where '+q+' like "%'+keyword+'%" order by artist'
elif keyword =='' and q =='':
cmd = 'select * from music'
else:
ml.err('Missing keyword')
sys.exit()
conn = sqlite3.connect(db)
cursor = conn.cursor()
ml.dbg(cmd)
cursor.execute(cmd)
num = len(cursor.fetchall())
ml.dbg(num)
if num == 0:
ml.dbg('No Entry find')
return False
cursor.execute(cmd) # need to improve
v = from_db_cursor(cursor)
v.align['album']='l'
cursor.close()
conn.close()
return v
def build_albumlist(albumlist,coverdir):
'''Build CD inventory by scanning CD dir'''
with open(albumlist,'w',encoding='utf-8') as f:
for fd in os.listdir(coverdir):
if os.path.isdir(os.path.join(coverdir,fd)):
for cover in os.listdir(os.path.join(coverdir,fd)):
f.write(cover[:-4]+'\n')
def build_inventory(inventory,archdir):
'''Build Artist Inventory of archive folder'''
with open(inventory,'w',encoding='utf-8') as f:
for p in os.listdir(archdir):
pdir = os.path.join(archdir,p)
for adir in os.listdir(pdir):
if adir != '_VA':
f.write(os.path.join(pdir,adir)+'\n')
# def find_art(artist,inventory,match=True):
# '''Find artist path in inventory'''
# p_art = []
# if artist[-1] == '.': # windows folder name cannot end with .
# artist = artist[:-2]
# with open(inventory,'r',encoding='utf-8') as f:
# if match:
# for i in f.readlines():
# if artist.upper() == i.strip().split('\\')[-1].upper():
# p_art = i.strip()
# # for n in os.listdir(p_art): print(n)
# return p_art
# else:
# for i in f.readlines():
# if artist.upper() in i.strip().split('\\')[-1].upper():
# p_art.append(i.strip())
# return p_art
# return 'No artist'
def find_art(artist,inventory,match=True):
'''Find artist path in inventory'''
p_art = []
if artist[-1] == '.': # windows folder name cannot end with .
artist = artist[:-2]
with open(inventory,'r',encoding='utf-8') as f:
for i in f.readlines():
if match:
if artist.upper() == i.strip().split('\\')[-1].upper():
return i.strip()
else:
if artist.upper() in i.strip().split('\\')[-1].upper():
p_art.append(i.strip())
return p_art if p_art != [] else 'No artist'
def rename_mp3(folder=topdir):
'''Rename MP3 based on ID3 tag'''
ml = mylogger(logfile,get_funcname())
for mp3 in os.listdir(folder):
p_mp3 = os.path.join(folder,mp3)
if p_mp3[-3:].lower() == 'mp3':
# l.info(mp3[:-3])
d = readtag(p_mp3)
singer = str(d[0])
title = str(d[1])
songname = singer+' - '+title+'.mp3'
if mp3 != songname:
ml.info(f'Change {mp3} ---> {songname}')
src = p_mp3
dst = os.path.join(folder,songname)
ml.dbg(src)
ml.dbg(dst)
os.rename(src,dst)
def archive_cd(evadir,archdir):
'''Archive album core function'''
ml = mylogger(logfile)
for dirname in os.listdir(evadir):
al_src = os.path.join(evadir, dirname)
if os.path.isdir(al_src):
ml.dbg(al_src)
m = re.split('\s\-\s\d{4}\s\-\s',str(dirname))
if len(m) == 2: #find album
pic_src = os.path.join(evadir,dirname,dirname+'.jpg')
ml.dbg('Cover from '+pic_src)
pic_dst = os.path.join(evadir,dirname+'.jpg')
ml.dbg('Cover to '+pic_dst)
if os.path.exists(pic_dst) == False:
shutil.copyfile(pic_src,pic_dst)
ml.info('Searching Artist: '+m[0])
p_art = find_art(m[0],inventory)
ml.dbg(p_art)
if os.path.isdir(p_art):
# l.info(p_art)
ml.info('Archive '+dirname)
al_dst = p_art
ml.dbg(al_dst)
elif os.path.isdir(os.path.join(evadir, m[0])):
ml.warn('Already prearchive -> move album '+dirname)
al_dst = os.path.join(evadir,m[0])
else:
ml.warn('Prearchive '+m[0]+' -> move album '+dirname)
os.mkdir(os.path.join(evadir, m[0]))
al_dst = os.path.join(evadir, m[0])
result = myfs.d_move(al_src,al_dst)
ml.info(result)
if os.path.isdir(os.path.join(al_dst,m[0])):
ml.info("Archive complete")
def move_mp3(topdir,musicure):
'''Move MP3 from Topfolder to Musicure'''
ml = mylogger(logfile,get_funcname())
for mp3 in os.listdir(topdir):
if mp3[-3:].lower() == 'mp3':
ml.info('Move --> '+mp3)
src = os.path.join(topdir,mp3)
dst = os.path.join(musicure,mp3)
ml.dbg(f'{src} --> {dst}')
shutil.move(src,dst)
def move_cover(evadir,coverdir):
'''Move Cover to CoverFolder'''
ml = mylogger(logfile,get_funcname())
for jpg in os.listdir(evadir):
if jpg[-3:].lower() == 'jpg':
ml.info('Move --> '+jpg)
src = os.path.join(evadir,jpg)
dst = os.path.join(coverdir,jpg)
ml.info(f'{src} --> {dst}')
shutil.move(src,dst)
def arch_cover(coverdir):
'''move Cover to a-z, manuel check others'''
ml = mylogger(logfile,get_funcname())
for c in os.listdir(coverdir):
src = os.path.join(coverdir,c)
if os.path.isdir(src) == False:
dd = os.path.join(coverdir,c[0])
if os.path.isdir(dd) == True:
dst = os.path.join(dd,c)
result = myfs.f_move(src,dst)
ml.info(result)
def evaluate_art(evadir,musicure):
'''Evaluate Artist in temp evadir'''
ml = mylogger(logfile,get_funcname())
for art in os.listdir(evadir):
if os.path.isdir(os.path.join(evadir,art)):
ml.info('='*20)
ml.warn(art)
n = 0
for dirpath, dirnames, files in os.walk(musicure):
for name in files:
#bug name contain -
if str(name).split('-')[0].strip() == art:
ml.info(name)
n += 1
if n == 0:
ml.warn('Zero !!! Track ---> Move to misc')
else:
albumnumber = len(os.listdir(os.path.join(evadir,art)))
rate = str(n/albumnumber)[:4]
if float(rate) < 0.5:
ml.warn(f'Rate {rate} from {str(albumnumber)} Album --> One more CD to evaluate ')
else:
ml.info(f'Rate {rate} from {str(albumnumber)} Album --> Total {str(n)} Tracks')
def create_folder(workfolder,albumdir):
'''Create Album folder and switch to album folder'''
# albumdir = aDict['fullname']
ml = mylogger(__name__,logfile)
ml.info('Create folder: '+albumdir)
os.chdir(workfolder)
albumfulldir = os.path.join(workfolder,albumdir)
if not os.path.exists(albumfulldir):
os.makedirs(albumfulldir)
else:
ml.warn('---- Album folder already exists !')
os.chdir(albumfulldir)
return albumfulldir
def find_album(album,match=True):
'''Find Album in list'''
with open(albumlist,'r',encoding='utf-8') as f:
if match == True:
for i in f.readlines():
if album == i.strip():
return True
else:
for i in f.readlines():
if album.upper() in i.strip().upper():
return i.strip()
return False
def compare_mp3folder():
question = r'L:\Music\_Jazz\cc.txt'
target = r'N:\MusiCure\t.txt'
with open(target,'r') as t:
tdict = { x.strip().split('\\')[-1]:x.strip() for x in t.readlines()}
with open(question,'r') as q:
for x in q.readlines():
s = x.strip().split('\\')[-1]
if s in tdict.keys():
print(x.strip())
# print(tdict[s])
# print(f_move(x.strip(),adict[a]))
def main():
ml = mylogger(logfile,get_funcname())
parser = argparse.ArgumentParser(description = 'Archive music tool')
group = parser.add_mutually_exclusive_group()
group.add_argument('-a',action="store_true", help='Archive CD')
group.add_argument('-e',action="store_true", help='Evaluate artist')
group.add_argument('-r',action="store_true", help='Rename MP3')
group.add_argument('-m',action="store_true", help='Move MP3')
group.add_argument('-f',action="store_true", help='Find Artist')
group.add_argument('-i',action="store_true", help='Update inventory and archive cover')
args = parser.parse_args()
if args.a :
ml.info('Archive CD')
build_inventory(inventory,archdir)
archive_cd(evadir,archdir)
move_cover(evadir,coverdir)
build_albumlist(albumlist,coverdir)
elif args.i:
arch_cover(coverdir)
build_inventory(inventory,archdir)
build_albumlist(albumlist,coverdir)
ml.info('Update Inventory Finish')
elif args.e:
ml.info('Evaluate artist')
evaluate_art(evadir,musicure)
elif args.r:
ml.info('Rename MP3')
rename_mp3()
elif args.m:
ml.info('Move MP3')
move_mp3(topdir,musicure)
elif args.f:
artist = input("Find Artist: ")
path = find_art(artist,inventory,match=False)
for p in path:
ml.info(p)
else:
parser.print_help()
if __name__ == "__main__":
# if os.path.exists(logfile):
# os.remove(logfile)
try:
main()
except KeyboardInterrupt:
print('ctrl + c')