Skip to content

Commit d2baac0

Browse files
committed
moved operations from artist method to song
1 parent 0845073 commit d2baac0

1 file changed

Lines changed: 63 additions & 62 deletions

File tree

python2.7/music-organizer.py

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ def toNeat(s):
102102
return s
103103

104104

105-
def artist(artistDir):
106-
print("Organizing artist '" + artistDir + "'.")
105+
def artist():
106+
print("Organizing artist")
107107
if not args.ignore_multiple_artists:
108108
artists = set()
109-
for dirname, dirnames, filenames in os.walk(artistDir):
109+
for dirname, dirnames, filenames in os.walk("."):
110+
print "Random test"
110111
# Make sure there aren't a lot of different artists
111112
# in case this was called from the wrong directory.
112113
for filename in filenames:
@@ -119,10 +120,8 @@ def artist(artistDir):
119120

120121
if len(artists) > 2:
121122
while True:
122-
print("Warning: More than 2 artists found in '{}'.".format(
123-
artistDir))
124-
print("This will move all songs to the '{}' directory.".format(
125-
artistDir))
123+
print("Warning: More than 2 artists found in '{}'.".format("."))
124+
print("This will move all songs to the '{}' directory.".format("."))
126125
print("Continue? yes/no")
127126
choice = raw_input().lower()
128127
valid = {"yes": True, "y": True, "no": False, "n": False}
@@ -134,13 +133,14 @@ def artist(artistDir):
134133
sys.exit(-1)
135134

136135
delete_dirs = []
137-
for dirname, dirnames, filenames in os.walk(artistDir):
136+
for dirname, dirnames, filenames in os.walk("."):
138137
# Move all the files to the root directory.
139138
for filename in filenames:
139+
fullPath = os.path.join(dirname, filename)
140+
song(fullPath)
140141
ext = os.path.splitext(filename)[1]
141-
#if ext == ".mp3":
142+
'''
142143
if ext in (".mp3" , ".ogg"):
143-
fullPath = os.path.join(dirname, filename)
144144
print("file: " + str(fullPath))
145145
146146
try:
@@ -191,63 +191,14 @@ def artist(artistDir):
191191
print("Error: Unrecognized file extension in '{}'.".format(
192192
filename))
193193
sys.exit(-42)
194+
'''
194195

195196
# Delete all subdirectories.
196197
for subdirname in dirnames:
197198
delete_dirs.append(subdirname)
198199

199200
for d in delete_dirs:
200-
shutil.rmtree(os.path.join(artistDir, d), ignore_errors=True)
201-
202-
203-
def song(filename):
204-
if filename[0] == '.':
205-
print("Ignoring dotfile: '{}'".format(filename))
206-
return
207-
print("Organizing song '" + filename + "'.")
208-
ext = os.path.splitext(filename)[1]
209-
try:
210-
if ext == ".mp3":
211-
audio = EasyID3(filename)
212-
elif ext == ".ogg":
213-
audio = OggVorbis(filename)
214-
artist = audio['artist'][0].encode('ascii', 'ignore')
215-
title = audio['title'][0].encode('ascii', 'ignore')
216-
album = audio['album'][0].encode('ascii', 'ignore')
217-
if args.numbering:
218-
tracknumber = audio['tracknumber'][0].encode('ascii', 'ignore')
219-
print(" artist: " + artist)
220-
print(" title: " + title)
221-
if args.album:
222-
print(" album: " + album)
223-
except:
224-
artist = None
225-
title = None
226-
if args.album:
227-
album = None
228-
if args.numbering:
229-
tracknumber = None
230-
neatArtist = toNeat(artist)
231-
if args.numbering:
232-
neatTitle = tracknumber + ".-" + toNeat(title)
233-
else:
234-
neatTitle = toNeat(title)
235-
if args.album:
236-
neatAlbum = toNeat(album)
237-
print(" neatArtist: " + neatArtist)
238-
print(" neatTitle: " + neatTitle)
239-
if args.album:
240-
print(" neatAlbum: " + neatAlbum)
241-
if not os.path.isdir(neatArtist):
242-
os.mkdir(neatArtist)
243-
if args.album:
244-
if not os.path.isdir(neatArtist + "/" + neatAlbum):
245-
os.mkdir(neatArtist + "/" + neatAlbum)
246-
newFullPath = os.path.join(neatArtist, neatAlbum, neatTitle + ext)
247-
else:
248-
newFullPath = os.path.join(neatArtist, neatTitle + ext)
249-
os.rename(filename, newFullPath)
250-
201+
shutil.rmtree(os.path.join(".", d), ignore_errors=True)
251202

252203
def collection():
253204
for f in glob.glob('*'):
@@ -257,8 +208,58 @@ def collection():
257208
elif os.path.isfile(f):
258209
song(f)
259210

211+
def song(filename):
212+
# if filename[0] == '.':
213+
# print("Ignoring dotfile: '{}'".format(filename))
214+
# return
215+
print("Organizing song '" + filename + "'.")
216+
ext = os.path.splitext(filename)[1]
217+
if ext in (".mp3" , ".ogg"):
218+
try:
219+
if ext == ".mp3":
220+
audio = EasyID3(filename)
221+
elif ext == ".ogg":
222+
audio = OggVorbis(filename)
223+
print audio
224+
artist = audio['artist'][0].encode('ascii', 'ignore')
225+
title = audio['title'][0].encode('ascii', 'ignore')
226+
album = audio['album'][0].encode('ascii', 'ignore')
227+
if args.numbering:
228+
tracknumber = audio['tracknumber'][0].encode('ascii', 'ignore')
229+
print(" artist: " + artist)
230+
print(" title: " + title)
231+
if args.album:
232+
print(" album: " + album)
233+
except:
234+
artist = None
235+
title = None
236+
if args.album:
237+
album = None
238+
if args.numbering:
239+
tracknumber = None
240+
neatArtist = toNeat(artist)
241+
if args.numbering:
242+
neatTitle = tracknumber + ".-" + toNeat(title)
243+
else:
244+
neatTitle = toNeat(title)
245+
if args.album:
246+
neatAlbum = toNeat(album)
247+
print(" neatArtist: " + neatArtist)
248+
print(" neatTitle: " + neatTitle)
249+
if args.album:
250+
print(" neatAlbum: " + neatAlbum)
251+
if not os.path.isdir(neatArtist):
252+
os.mkdir(neatArtist)
253+
if args.album:
254+
if not os.path.isdir(neatArtist + "/" + neatAlbum):
255+
os.mkdir(neatArtist + "/" + neatAlbum)
256+
newFullPath = os.path.join(neatArtist, neatAlbum, neatTitle + ext)
257+
else:
258+
newFullPath = os.path.join(neatArtist, neatTitle + ext)
259+
os.rename(filename, newFullPath)
260+
260261
if args.artist:
261-
artist('.')
262+
artist()
262263
else:
263264
collection()
264265
print("\nComplete!")

0 commit comments

Comments
 (0)