11import os
2- from sys import exit
2+ import sys
33import os .path
44import shutil
55
@@ -49,59 +49,67 @@ def quitWithMessage(messageString):
4949print ("\
5050 Starting Conversion script for structuring files for DFplayerMini.\n \
5151 This script analyzes .m3u playlists and restructures their contents to a format\n \
52- readable by the DFmini mp3 module.\n \n " )
52+ readable by the DFmini mp3 module.\n " )
5353
5454# folder names
55- plFldrName = "PlayListsM3U"
56- crdFldrName = "SDcardFolders"
55+ PLAYLISTFLDRNAME = "PlayListsM3U"
56+ CARDFLDRNAME = "SDcardFolders"
5757# player's maximum accepted file cnt
58- maxFldrCnt = 99
59- maxFileCnt = 255
58+ MAXFLDRCNT = 99
59+ MAXFILECNT = 255
60+ M3UFILETYPE = ".m3u"
61+ M3UFILEMARKER = "file:///"
62+ MP3FILEMARKER = ".mp3"
6063listOfPlaylists = None
6164
6265# get current working directory
6366myPath = os .getcwd ()
6467# create directories for playlists and target folders
65- plPath = os .path .join (os .sep , myPath , plFldrName )
66- crdPath = os .path .join (os .sep , myPath , crdFldrName )
68+ plPath = os .path .join (os .sep , myPath , PLAYLISTFLDRNAME )
69+ crdPath = os .path .join (os .sep , myPath , CARDFLDRNAME )
6770tgtFldrPath = - 1
6871tgtFileName = - 1
6972
7073print ("Hooked to current working directory: %s" % myPath )
7174
7275# Check for playlist path
73- if not os .path .exists (plFldrName ):
76+ if not os .path .exists (PLAYLISTFLDRNAME ):
7477 print ("\
7578 Did not find folder containing m3u playlists.\n \
7679 Trying to create folder 'PlayListsM3U' for you." )
7780 try :
78- os .makedirs (plFldrName )
81+ os .makedirs (PLAYLISTFLDRNAME )
7982 except :
8083 quitWithMessage ("Couldn't create folder. Is directory read-only?" )
81- quitWithMessage ("\
84+ quitWithMessage ("\
8285 Playlist folder successfully created: %s\n \
83- Please move your playlists to this place before restarting the script." % plFldrName )
86+ Please move your playlists to this place before restarting the script." % PLAYLISTFLDRNAME )
8487else :
8588 # if Playlist folder exists, print playlists to be evaluated.
86- listOfPlaylists = os .listdir (plFldrName )
87- listOfPlaylists .sort ()
88- print ("Folder containing playlists found. Will try creating DFplayer folders and files for:" )
89- print (listOfPlaylists )
90- # input("Press Enter to continue...")
89+ listOfPlaylists = os .listdir (PLAYLISTFLDRNAME )
90+ for line in listOfPlaylists :
91+ if (line .lower ()).find (M3UFILETYPE ) > - 1 :
92+ listOfPlaylists .sort ()
93+ print ("Folder containing playlists found. Will try creating DFplayer folders and files for:" )
94+ print (listOfPlaylists )
95+ else :
96+ msg = "--- ERROR --- PlayListM3U folder contains other file types than .m3u Playlists: %s\n \
97+ make sure you only place .m3u files here! Aborting." % line
98+ quitWithMessage (msg )
9199
92100# Check for album(folder) path
93- if not os .path .exists (crdFldrName ):
101+ if not os .path .exists (CARDFLDRNAME ):
94102 print ("\
95103 Did not find folder containing SD card MP3 folders.\n \
96104 Trying to create folder 'SDcardFolders' for you.\n \
97105 You will find the files to transfer to your DFplayerMini's SD card in this folder." )
98106 try :
99- os .makedirs (crdFldrName )
107+ os .makedirs (CARDFLDRNAME )
100108 except :
101- quitWithMessage ("Couldn't create folder. Is directory read-only?" )
109+ quitWithMessage ("--- ERROR --- Couldn't create folder. Is directory read-only?" )
102110else :
103111 # if Playlist folder exists, print playlists to be evaluated.
104- listOfCardFolders = os .listdir (crdFldrName )
112+ listOfCardFolders = os .listdir (CARDFLDRNAME )
105113 print ("Folder containing SDcard MP3 folders found. Will modify folder structure based on playlist input." )
106114 print ("Existing folders: " + str (listOfCardFolders ))
107115
@@ -110,18 +118,17 @@ def quitWithMessage(messageString):
110118fName = ""
111119lineLower = ''
112120if listOfPlaylists :
113- print ("Playlists found: " + str (listOfPlaylists ))
114121 # Go through all .m3u files in .m3u folder
115122 for item in listOfPlaylists :
116123 os .chdir (crdPath )
117124 folderIndex += 1
118125 # get folderIndexes right according to player's needs
119- fName = playerFSnumbers (folderIndex , maxFldrCnt )
126+ fName = playerFSnumbers (folderIndex , MAXFLDRCNT )
120127 if not os .path .exists (fName ):
121128 try :
122129 os .makedirs (fName )
123130 except :
124- quitWithMessage ("Couldn't create folder. Is directory read-only?" )
131+ quitWithMessage ("--- ERROR --- Couldn't create folder. Is directory read-only?" )
125132 else :
126133 # folder already exists. Auto-Overwrite.
127134 print ("Folder '%s' exists. Will auto-overwrite contents." % fName )
@@ -135,25 +142,25 @@ def quitWithMessage(messageString):
135142 os .rename (str (item ), fName + " " + str (item ))
136143 item = fName + " " + str (item )
137144 except :
138- quitWithMessage ("Couldn't rename playlist " + str (item ) + ". Aborting." )
145+ quitWithMessage (" --- ERROR --- Couldn't rename playlist " + str (item ) + ". Aborting." )
139146 playlistFile = open (item , "r" )
140147 fileIndex = 0
141148 for line in playlistFile .readlines ():
142149 lineLower = line .lower () # convert to lower case to also find .MP3
143- if lineLower .find (".mp3" ) > - 1 : # only list lines that describe MP3 file path
150+ if lineLower .find (M3UFILEMARKER ) > - 1 : # only list lines that describe MP3 file path
144151 fileIndex += 1
145- fName = playerFSnumbers (fileIndex , maxFileCnt )
146- tgtFileName = os .sep + fName + ".mp3"
152+ fName = playerFSnumbers (fileIndex , MAXFILECNT )
153+ tgtFileName = os .sep + fName + MP3FILEMARKER
147154 line = line .strip () # strip removes New Line markers \n
148- if line .find ("file:///" ) > - 1 : # strip line of file marker within m3u
149- line = line .split ("file:///" )[- 1 ]
150- line = replaceSpecialCharactersInPlaylist (line )
151- tgtFileName = fName + ".mp3" # new target file name
155+ line = line .split (M3UFILEMARKER )[- 1 ]
156+ line = replaceSpecialCharactersInPlaylist (line )
157+ tgtFileName = fName + MP3FILEMARKER # new target file name
152158 try :
153159 shutil .copy2 (line , os .path .join (os .sep , tgtFldrPath ,
154160 tgtFileName )) # copies & renames file from playlist to tgt folder
155161 except :
156- quitWithMessage ("Couldn't copy file\n " + str (line ) + " to:\n " + str (tgtFldrPath ) + "\n Aborting." )
157- quitWithMessage ("Successfully copied %s files!" % fileIndex )
162+ quitWithMessage ("--- ERROR --- Couldn't copy file\n " + str (line ) + " to:\n " + str (tgtFldrPath ) + "\n Aborting." )
163+ print ("...completed for folder %d of %d: %s with %d files" % (folderIndex , len (listOfPlaylists ), item , fileIndex ))
164+ quitWithMessage ("--- SUCCESS --- All actions completed successfully." )
158165else :
159- quitWithMessage ("No playlists found in folder. Aborting." )
166+ quitWithMessage ("--- ERROR --- No playlists found in folder. Place .m3u file(s) here, please . Aborting." )
0 commit comments