Skip to content

Commit 67c438f

Browse files
committed
add Error and detection for non-m3u files in PlayListM3U folder
add Error markers in console output clarified file names, set to variables clarified completion status and file/folder numbers fix console closing after script completion fix wrong detection of file item in EXTINF information of playlist files
1 parent 720b751 commit 67c438f

1 file changed

Lines changed: 42 additions & 35 deletions

File tree

AutoStructureFilesForDFplayer.py

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from sys import exit
2+
import sys
33
import os.path
44
import shutil
55

@@ -49,59 +49,67 @@ def quitWithMessage(messageString):
4949
print("\
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"
6063
listOfPlaylists = None
6164

6265
# get current working directory
6366
myPath = 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)
6770
tgtFldrPath = -1
6871
tgtFileName = -1
6972

7073
print("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)
8487
else:
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?")
102110
else:
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):
110118
fName = ""
111119
lineLower = ''
112120
if 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) + "\nAborting.")
157-
quitWithMessage("Successfully copied %s files!" % fileIndex)
162+
quitWithMessage("--- ERROR --- Couldn't copy file\n" + str(line) + " to:\n" + str(tgtFldrPath) + "\nAborting.")
163+
print("...completed for folder %d of %d: %s with %d files" % (folderIndex, len(listOfPlaylists), item, fileIndex))
164+
quitWithMessage("--- SUCCESS --- All actions completed successfully.")
158165
else:
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

Comments
 (0)