33import os .path
44import shutil
55# use urllib to escape special (UTF8) characters in filenames
6- from urllib .parse import quote
6+ from urllib .parse import unquote
77
88# Author: "Schallbert"
99
@@ -30,7 +30,7 @@ def playerFSnumbers(indx, maxCnt):
3030def replace_special_chars_in_path (line_of_playlist ):
3131 """Make sure .m3u file's special characters are converted
3232 Replace most common UTF8 special characters"""
33- return quote (line_of_playlist )
33+ return unquote (line_of_playlist )
3434
3535
3636def quitWithMessage (messageString ):
@@ -66,15 +66,12 @@ def get_target_file_name(index):
6666 This script analyzes .m3u playlists and restructures their contents to a format\n \
6767 readable by the DFmini mp3 module.\n " )
6868
69- listOfPlaylists = None
70-
7169# get current working directory
7270myPath = os .getcwd ()
7371# create directories for playlists and target folders
7472plPath = os .path .join (os .sep , myPath , PLAYLISTFLDRNAME )
7573crdPath = os .path .join (os .sep , myPath , CARDFLDRNAME )
76- tgtFldrPath = - 1
77- tgtFileName = - 1
74+ listOfPlaylists = None
7875
7976print ("Hooked to current working directory: %s" % myPath )
8077
@@ -103,68 +100,65 @@ def get_target_file_name(index):
103100 print (listOfPlaylists )
104101
105102# Check for album(folder) path
106- if not os .path .exists (CARDFLDRNAME ):
103+ if os .path .exists (CARDFLDRNAME ):
104+ # if Playlist folder exists, print playlists to be evaluated.
105+ listOfCardFolders = os .listdir (CARDFLDRNAME )
106+ print ("Folder containing SDcard MP3 folders found. Will modify folder structure based on playlist input." )
107+ print ("Existing folders: " + str (listOfCardFolders ))
108+ else :
107109 print ("\
108- Did not find folder containing SD card MP3 folders.\n \
109- Trying to create folder 'SDcardFolders' for you.\n \
110- You will find the files to transfer to your DFplayerMini's SD card in this folder." )
110+ Did not find folder containing SD card MP3 folders.\n \
111+ Trying to create folder 'SDcardFolders' for you.\n \
112+ You will find the files to transfer to your DFplayerMini's SD card in this folder." )
111113 try :
112114 os .makedirs (CARDFLDRNAME )
113115 except :
114116 quitWithMessage ("--- ERROR --- Couldn't create folder. Is directory read-only?" )
115- else :
116- # if Playlist folder exists, print playlists to be evaluated.
117- listOfCardFolders = os .listdir (CARDFLDRNAME )
118- print ("Folder containing SDcard MP3 folders found. Will modify folder structure based on playlist input." )
119- print ("Existing folders: " + str (listOfCardFolders ))
120117
118+ if not listOfPlaylists :
119+ quitWithMessage ("--- ERROR --- No playlists found in folder. Place .m3u file(s) here, please. Aborting." )
121120# create folders with correct naming to contain mp3 files
122121folderIndex = 0
123- tgt_folder_name = ""
122+ target_folder_name = ""
124123line_lower = ''
125- if listOfPlaylists :
126- # Go through all .m3u files in .m3u folder
127- for item in listOfPlaylists :
128- os .chdir (crdPath )
129- folderIndex += 1
130- # get folderIndexes right according to player's needs
131- tgt_folder_name = playerFSnumbers (folderIndex , MAXFLDRCNT )
132- if not os .path .exists (tgt_folder_name ):
124+ # Go through all .m3u files in .m3u folder
125+ for playlist in listOfPlaylists :
126+ folderIndex += 1
127+ # get folderIndexes right according to player's needs
128+ target_folder_name = playerFSnumbers (folderIndex , MAXFLDRCNT )
129+ target_folder_path = os .sep .join ([crdPath , target_folder_name ])
130+ if os .path .exists (target_folder_name ):
131+ print ("Folder '%s' already exists. Will auto-overwrite contents." % target_folder_name )
132+ else :
133+ try :
134+ os .makedirs (target_folder_path )
135+ except :
136+ quitWithMessage ("--- ERROR --- Couldn't create folder. Is directory read-only?" )
137+ # playlist open and show
138+ # try to find out if playlist has been renamed already, returns -1 if not
139+ if playlist .find (target_folder_name , 0 , 2 ) == - 1 :
140+ try :
141+ # rename playlists to match folder numbers
142+ new_name = [target_folder_name , "_" , playlist ]
143+ os .rename (os .sep .join ([plPath , playlist ], os .sep .join ([plPath , new_name ])))
144+ playlist = new_name
145+ except :
146+ quitWithMessage (" --- ERROR --- Couldn't rename playlist " + str (playlist ) + ". Aborting." )
147+ playlistFile = open (os .sep .join ([plPath , playlist ]), "r" )
148+ file_index = 0
149+ for line in playlistFile .readlines ():
150+ line = find_m3u_filemarker (line .lower ())
151+ if line is not None :
152+ file_index += 1
153+ target_file_name = get_target_file_name (file_index )
154+ # copies file from playlist to tgt folder
133155 try :
134- os .makedirs ( tgt_folder_name )
156+ shutil . copy2 ( line , os .sep . join ([ target_folder_path , target_file_name ]) )
135157 except :
136- quitWithMessage ("--- ERROR --- Couldn't create folder. Is directory read-only?" )
137- else :
138- # folder already exists. Auto-Overwrite.
139- print ("Folder '%s' exists. Will auto-overwrite contents." % tgt_folder_name )
140- # go through all playlists in folder
141- tgtFldrPath = os .path .join (os .sep , crdPath , tgt_folder_name )
142- os .chdir (plPath )
143- # playlist open and show
144- if item .find (str (tgt_folder_name ), 0 ,
145- 4 ) == - 1 : # tries to find out if playlist has been used already, returns -1 if no duplicates
146- try :
147- os .rename (str (item ), tgt_folder_name + " " + str (item ))
148- item = tgt_folder_name + " " + str (item )
149- except :
150- quitWithMessage (" --- ERROR --- Couldn't rename playlist " + str (item ) + ". Aborting." )
151- playlistFile = open (item , "r" )
152- file_index = 0
153- for line in playlistFile .readlines ():
154- line = find_m3u_filemarker (line .lower ())
155- if line is not None :
156- file_index += 1
157- tgt_file_name = get_target_file_name (file_index )
158- # copies & renames file from playlist to tgt folder
159- #try:
160- shutil .copy2 (line , os .path .join (os .sep , tgtFldrPath , tgt_file_name ))
161- #except:
162- # quitWithMessage(
163- # "--- ERROR --- Couldn't copy file\n" + str(line) + " to:\n" + str(
164- # tgtFldrPath) + "\nAborting.")
165- print (
166- "...completed for folder %d of %d: %s with %d files" % (
167- folderIndex , len (listOfPlaylists ), item , file_index ))
168- quitWithMessage ("--- SUCCESS --- All actions completed successfully." )
169- else :
170- quitWithMessage ("--- ERROR --- No playlists found in folder. Place .m3u file(s) here, please. Aborting." )
158+ quitWithMessage (
159+ "--- ERROR --- Couldn't copy file\n " + str (line ) + " to:\n " + str (
160+ target_folder_path ) + "\n Aborting." )
161+ print (
162+ "...completed for folder %d of %d: %s with %d files" % (
163+ folderIndex , len (listOfPlaylists ), playlist , file_index ))
164+ quitWithMessage ("--- SUCCESS --- All actions completed successfully." )
0 commit comments