2525 delete them. Note this might always be best in case an artist
2626 has multiple versions. To keep multiple versions,
2727 fix the tag information.''' )
28+ parser .add_argument ('--ignore-multiple-artists' , action = 'store_true' ,
29+ dest = 'ignore_multiple_artists' ,
30+ help = 'TODO' )
2831parser .add_argument ('--collection' , action = 'store_true' ,
2932 help = 'TODO' )
3033parser .add_argument ('--artist' , action = 'store_true' ,
4144# Maps a string such as 'The Beatles' to 'the-beatles'.
4245def toNeat (s ):
4346 s = s .lower ().replace ("&" ,"and" )
44- s = re .sub (r"[()\[\],.'\\\ ?\#/\!]" , "" , s )
47+ s = re .sub (r"[()\[\],.'\"\\\ ?\#/\!\$\: ]" , "" , s )
4548 s = re .sub (r"[ \*\_]" , "-" , s )
4649 s = re .sub ("-+" , "-" , s )
4750 search = re .search ("[^0-9a-z\-]" , s )
@@ -50,34 +53,36 @@ def toNeat(s):
5053 sys .exit (- 42 )
5154 return s
5255
53- def artist ():
54- artists = set ()
55- valid = {"yes" :True , "y" :True , "no" :False , "n" :False }
56- for dirname , dirnames , filenames in os .walk ('.' ):
57- # Make sure there aren't a lot of different artists
58- # in case this was called from the wrong directory.
59- for filename in filenames :
60- try :
61- audio = EasyID3 (os .path .join (dirname , filename ))
62- artist = audio ['artist' ][0 ].decode ()
63- artists .add (artist )
64- except :
65- pass
66-
67- if len (artists ) > 2 :
68- while True :
69- print ("Warning: More than 2 artists found." )
70- print ("This will move all songs to the current directory." )
71- print ("Continue? yes/no" )
72- choice = raw_input ().lower ()
73- if choice in valid :
74- if valid [choice ]: break
75- else :
76- print ("Exiting." )
77- sys .exit (- 1 )
56+ def artist (artistDir ):
57+ print ("Organizing '" + artistDir + "'." )
58+ if not args .ignore_multiple_artists :
59+ artists = set ()
60+ for dirname , dirnames , filenames in os .walk (artistDir ):
61+ # Make sure there aren't a lot of different artists
62+ # in case this was called from the wrong directory.
63+ for filename in filenames :
64+ try :
65+ audio = EasyID3 (os .path .join (dirname , filename ))
66+ artist = audio ['artist' ][0 ].decode ()
67+ artists .add (artist )
68+ except :
69+ pass
70+
71+ if len (artists ) > 2 :
72+ while True :
73+ print ("Warning: More than 2 artists found in '" + artistDir + "'." )
74+ print ("This will move all songs to the '" + artistDir + "' directory." )
75+ print ("Continue? yes/no" )
76+ choice = raw_input ().lower ()
77+ valid = {"yes" :True , "y" :True , "no" :False , "n" :False }
78+ if choice in valid :
79+ if valid [choice ]: break
80+ else :
81+ print ("Exiting." )
82+ sys .exit (- 1 )
7883
7984 delete_dirs = []
80- for dirname , dirnames , filenames in os .walk ('.' ):
85+ for dirname , dirnames , filenames in os .walk (artistDir ):
8186 # Move all the files to the root directory.
8287 for filename in filenames :
8388 ext = os .path .splitext (filename )[1 ]
@@ -87,7 +92,7 @@ def artist():
8792
8893 try :
8994 audio = EasyID3 (fullPath )
90- title = audio ['title' ][0 ].decode ( )
95+ title = audio ['title' ][0 ].encode ( 'ascii' , 'ignore' )
9196 print (" title: " + title )
9297 except : title = None
9398
@@ -98,7 +103,7 @@ def artist():
98103 neatTitle = toNeat (title )
99104 print (" neat-title: " + neatTitle )
100105
101- newFullPath = os .path .join ("." , neatTitle + ext )
106+ newFullPath = os .path .join (artistDir , neatTitle + ext )
102107 print (" newFullPath: " + newFullPath )
103108
104109 if newFullPath != fullPath :
@@ -124,9 +129,12 @@ def artist():
124129 delete_dirs .append (subdirname )
125130
126131 for d in delete_dirs :
127- shutil .rmtree (d ,ignore_errors = True )
132+ shutil .rmtree (os . path . join ( artistDir , d ) ,ignore_errors = True )
128133
129- print ("\n Complete!" )
134+ def collection ():
135+ for d in os .listdir ("." ):
136+ artist (d )
130137
131- if args .artist :
132- artist ()
138+ if args .artist : artist ('.' )
139+ else : collection ()
140+ print ("\n Complete!" )
0 commit comments