|
| 1 | +#!/usr/bin/env python2.7 |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import sys |
| 6 | +from mutagen.easyid3 import EasyID3 |
| 7 | + |
| 8 | +replaceChars = ( |
| 9 | + (" ", "-"), |
| 10 | + ("(", ""), |
| 11 | + (")", ""), |
| 12 | + (",", ""), |
| 13 | + (".", ""), |
| 14 | + ("'", ""), |
| 15 | + ("?", "") |
| 16 | +) |
| 17 | +def toNeat(s): |
| 18 | + s = s.lower() |
| 19 | + for r in replaceChars: s = s.replace(r[0], r[1]) |
| 20 | + search = re.search("[^a-z\-]", s) |
| 21 | + if search: |
| 22 | + print("Error: Unrecognized character in '" + s + "'") |
| 23 | + sys.exit(-42) |
| 24 | + return s |
| 25 | + |
| 26 | +for dirname, dirnames, filenames in os.walk('.'): |
| 27 | + for subdirname in dirnames: |
| 28 | + print("subdir:" + str(subdirname)) |
| 29 | + for filename in filenames: |
| 30 | + fullPath = os.path.join(dirname, filename) |
| 31 | + print("file: " + str(fullPath)) |
| 32 | + audio = EasyID3(fullPath) |
| 33 | + title = audio['title'][0].decode() |
| 34 | + print(" title: " + title) |
| 35 | + |
| 36 | + neatTitle = toNeat(title) |
| 37 | + print(" neat-title: " + neatTitle) |
| 38 | + |
| 39 | + ext = os.path.splitext(filename)[1] |
| 40 | + newFullPath = os.path.join(dirname, neatTitle + ext) |
| 41 | + print(" newFullPath: " + newFullPath) |
| 42 | + |
| 43 | + if newFullPath != fullPath: |
| 44 | + if os.path.isfile(newFullPath): |
| 45 | + print("Error: File exists: '" + newFullPath + "'") |
| 46 | + sys.exit(-42) |
| 47 | + |
| 48 | + os.rename(fullPath, newFullPath) |
| 49 | + |
| 50 | +print("\nComplete!") |
0 commit comments