@@ -107,54 +107,56 @@ def cleanup_jpg_files(working_dir):
107
107
os .remove (os .path .join (root , filename ))
108
108
109
109
110
- def makedir_if_absent (base_path , path ):
110
+ def makedir_if_absent (path ):
111
111
if not os .path .exists (path ):
112
- try :
113
- os .makedirs (path )
114
- except (NotADirectoryError , FileNotFoundError ):
115
- new_path = base_path
116
- relative_path = os .path .relpath (path , base_path )
117
- relative_path_dirlist = relative_path .split (os .path .sep )
118
- for directory in relative_path_dirlist :
119
- new_path = os .path .join (new_path , re .sub (r'[^\w_. -]' , '_' , directory ))
120
- if not os .path .exists (new_path ):
121
- os .makedirs (new_path )
122
-
123
- return path
112
+ os .makedirs (path )
124
113
125
114
126
115
def reorganize_files (path , quarantine_dir ):
127
116
for root , dirs , files in os .walk (path ):
128
117
if not root .endswith (quarantine_dir ):
129
118
for name in files :
130
- full_path = os .path .join (root , name )
131
- organize_song_by_tags (path , full_path )
119
+ if os .path .splitext (name )[1 ] == '.mp3' :
120
+ full_path = os .path .join (root , name )
121
+ organize_song_by_tags (path , full_path , quarantine_dir )
132
122
133
123
134
- def organize_song_by_tags (base_music_path , filepath ):
124
+ def organize_song_by_tags (base_music_path , filepath , quarantine_dir ):
135
125
file = music_tag .load_file (filepath )
136
126
artist_name = str (file ['artist' ])
137
127
album_name = str (file ['album' ])
128
+ artist_name_sanitized = sanitize_name_for_directory (artist_name )
129
+ album_name_sanitized = sanitize_name_for_directory (album_name )
130
+ makedir_if_absent (os .path .join (base_music_path , artist_name_sanitized ))
131
+ makedir_if_absent (os .path .join (base_music_path , artist_name_sanitized , album_name_sanitized ))
132
+
133
+ if not check_song_location (filepath , artist_name_sanitized , artist_name_sanitized ):
134
+ try :
135
+ os .rename (filepath , os .path .join (base_music_path , artist_name_sanitized , album_name_sanitized ,
136
+ os .path .basename (filepath )))
137
+ except FileExistsError :
138
+ try :
139
+ makedir_if_absent (os .path .join (quarantine_dir , 'DUPLICATE_FILES' ))
140
+ os .rename (filepath , os .path .join (quarantine_dir , 'DUPLICATE_FILES' , os .path .basename (filepath )))
141
+ except FileExistsError :
142
+ return
138
143
139
- base_name = os .path .basename (filepath )
140
- artist_dir = makedir_if_absent (base_music_path , os .path .join (base_music_path , artist_name ))
141
- album_dir = makedir_if_absent (base_music_path , os .path .join (base_music_path , artist_name , album_name ))
142
- directory_list = album_dir .split (os .sep )
143
- if not check_song_location (directory_list , artist_name , album_name ):
144
- os .rename (filepath , os .path .join (album_dir , base_name ))
145
144
145
+ def check_song_location (filepath , artist_name_sanitized , album_name_sanitized ):
146
+ directory_list = filepath .split (os .sep )
146
147
147
- def check_song_location (directory_list , artist_name , album_name ):
148
148
if len (directory_list ) < 2 :
149
149
return False
150
150
151
- if directory_list [- 1 ] != album_name :
151
+ if directory_list [- 1 ] != album_name_sanitized :
152
152
return False
153
153
154
- if directory_list [- 2 ] != artist_name :
154
+ if directory_list [- 2 ] != artist_name_sanitized :
155
155
return False
156
156
157
157
return True
158
158
159
159
160
160
def sanitize_name_for_directory (name ):
161
+ #TODO this check is too aggressive, need to rework
162
+ return re .sub (r'[^\w_. -]' , '_' , name )
0 commit comments