|
5 | 5 | import sys |
6 | 6 | from mutagen.easyid3 import EasyID3 |
7 | 7 |
|
8 | | -replaceChars = ( |
9 | | - (" ", "-"), |
10 | | - ("(", ""), |
11 | | - (")", ""), |
12 | | - (",", ""), |
13 | | - (".", ""), |
14 | | - ("'", ""), |
15 | | - ("?", "") |
16 | | -) |
| 8 | +emptyChars = re.compile(r"[(),.'\\\?]") |
17 | 9 | def toNeat(s): |
18 | 10 | s = s.lower() |
19 | | - for r in replaceChars: s = s.replace(r[0], r[1]) |
| 11 | + s = s.replace(" ", "-") |
| 12 | + s = s.replace("&", "and") |
| 13 | + s = emptyChars.sub("", s) |
20 | 14 | search = re.search("[^0-9a-z\-]", s) |
21 | 15 | if search: |
22 | 16 | print("Error: Unrecognized character in '" + s + "'") |
23 | 17 | sys.exit(-42) |
24 | 18 | return s |
25 | 19 |
|
26 | 20 | for dirname, dirnames, filenames in os.walk('.'): |
27 | | - for subdirname in dirnames: |
28 | | - print("subdir:" + str(subdirname)) |
| 21 | + # Move all the files to the root directory. |
29 | 22 | 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) |
| 23 | + ext = os.path.splitext(filename)[1] |
| 24 | + if ext == ".mp3": |
| 25 | + fullPath = os.path.join(dirname, filename) |
| 26 | + print("file: " + str(fullPath)) |
| 27 | + audio = EasyID3(fullPath) |
| 28 | + title = audio['title'][0].decode() |
| 29 | + print(" title: " + title) |
| 30 | + |
| 31 | + if not title: |
| 32 | + print("Error: title not found for '" + filename + "'") |
| 33 | + sys.exit(-42) |
35 | 34 |
|
36 | | - neatTitle = toNeat(title) |
37 | | - print(" neat-title: " + neatTitle) |
| 35 | + neatTitle = toNeat(title) |
| 36 | + print(" neat-title: " + neatTitle) |
38 | 37 |
|
39 | | - ext = os.path.splitext(filename)[1] |
40 | | - newFullPath = os.path.join(dirname, neatTitle + ext) |
41 | | - print(" newFullPath: " + newFullPath) |
| 38 | + newFullPath = os.path.join(".", neatTitle + ext) # Remove subdirectories. |
| 39 | + print(" newFullPath: " + newFullPath) |
42 | 40 |
|
43 | | - if newFullPath != fullPath: |
44 | | - if os.path.isfile(newFullPath): |
45 | | - print("Error: File exists: '" + newFullPath + "'") |
46 | | - sys.exit(-42) |
| 41 | + if newFullPath != fullPath: |
| 42 | + if os.path.isfile(newFullPath): |
| 43 | + print("Error: File exists: '" + newFullPath + "'") |
| 44 | + sys.exit(-42) |
| 45 | + |
| 46 | + os.rename(fullPath, newFullPath) |
| 47 | + os.chmod(newFullPath, 0644) |
| 48 | + elif ext == ".pdf": |
| 49 | + pass |
| 50 | + else: |
| 51 | + print("Error: Unrecognized file extension in '" + filename + "'") |
| 52 | + sys.exit(-42) |
47 | 53 |
|
48 | | - os.rename(fullPath, newFullPath) |
| 54 | + # Delete all subdirectories. |
| 55 | + for subdirname in dirnames: |
| 56 | + os.rmdir(subdirname) |
49 | 57 |
|
50 | 58 | print("\nComplete!") |
0 commit comments