|
18 | 18 | from mutagen.easyid3 import EasyID3 |
19 | 19 |
|
20 | 20 | parser = argparse.ArgumentParser( |
21 | | - description='''Organizes a music collection using tag information. |
| 21 | + description='''Organizes a music collection using tag information. |
22 | 22 | The directory format is that the music collection consists of |
23 | 23 | artist subdirectories, and there are 2 modes to operate on |
24 | 24 | the entire collection or a single artist. |
25 | 25 | All names are made lowercase and separated by dashes for easier |
26 | 26 | navigation in a Linux filesystem.''' |
27 | 27 | ) |
28 | 28 | parser.add_argument('--delete-conflicts', action='store_true', |
29 | | - dest='delete_conflicts', |
30 | | - help='''If an artist has duplicate tracks with the same name, |
31 | | - delete them. Note this might always be best in case an artist |
32 | | - has multiple versions. To keep multiple versions, |
33 | | - fix the tag information.''') |
| 29 | + dest='delete_conflicts', |
| 30 | + help='''If an artist has duplicate tracks with the same name, |
| 31 | + delete them. Note this might always be best in case an |
| 32 | + artist has multiple versions. To keep multiple versions, |
| 33 | + fix the tag information.''') |
34 | 34 | parser.add_argument('--ignore-multiple-artists', action='store_true', |
35 | | - dest='ignore_multiple_artists', |
36 | | - help='''This script will prompt for confirmation if an artist |
37 | | - directory has songs with more than 2 different tags. |
38 | | - This flag disables the confirmation and won't perform this check.''') |
| 35 | + dest='ignore_multiple_artists', |
| 36 | + help='''This script will prompt for confirmation if an artist |
| 37 | + directory has songs with more than 2 different tags. |
| 38 | + This flag disables the confirmation and won't perform |
| 39 | + this check.''') |
39 | 40 | parser.add_argument('--collection', action='store_true', |
40 | | - help='''Operate in 'collection' mode and run 'artist' mode on |
41 | | - every subdirectory.''') |
42 | | -parser.add_argument('--artist', action='store_true', |
43 | | - help='''Operate in 'artist' mode and copy all songs to the |
44 | | - root of the directory and cleanly format the names to |
45 | | - be easily typed and navigated in a shell.''') |
| 41 | + help='''Operate in 'collection' mode and run 'artist' mode |
| 42 | + on every subdirectory.''') |
| 43 | +parser.add_argument('--artist', action='store_true', |
| 44 | + help='''Operate in 'artist' mode and copy all songs to the |
| 45 | + root of the directory and cleanly format the names to |
| 46 | + be easily typed and navigated in a shell.''') |
46 | 47 | parser.add_argument('--delete-unrecognized-extensions', action='store_true', |
47 | | - dest='delete_unrecognized') |
| 48 | + dest='delete_unrecognized') |
48 | 49 | args = parser.parse_args() |
49 | 50 |
|
50 | 51 | if args.collection and args.artist: |
51 | | - print("Error: Only provide 1 of '--collection' or '--artist'.") |
52 | | - sys.exit(-1) |
| 52 | + print("Error: Only provide 1 of '--collection' or '--artist'.") |
| 53 | + sys.exit(-1) |
53 | 54 | elif not (args.collection or args.artist): |
54 | | - print("Error: Mode '--collection' or '--artist' not provided.") |
55 | | - sys.exit(-1) |
| 55 | + print("Error: Mode '--collection' or '--artist' not provided.") |
| 56 | + sys.exit(-1) |
| 57 | + |
56 | 58 |
|
57 | 59 | # Maps a string such as 'The Beatles' to 'the-beatles'. |
58 | 60 | def toNeat(s): |
59 | | - s = s.lower().replace("&","and") |
60 | | - |
61 | | - # Put spaces between and remove blank characters. |
62 | | - blankCharsPad = r"()\[\],.\\\?\#/\!\$\:" |
63 | | - blankCharsNoPad = r"'\"" |
64 | | - s = re.sub(r"([" + blankCharsPad + r"])([^ ])", "\\1 \\2", s) |
65 | | - s = re.sub("[" + blankCharsPad + blankCharsNoPad + "]", "", s) |
66 | | - |
67 | | - # Replace spaces with a single dash. |
68 | | - s = re.sub(r"[ \*\_]+", "-", s) |
69 | | - s = re.sub("-+", "-", s) |
70 | | - s = re.sub("^-*", "", s) |
71 | | - s = re.sub("-*$", "", s) |
72 | | - |
73 | | - # Ensure the string is only alphanumeric with '-', '+', and '='. |
74 | | - search = re.search("[^0-9a-z\-\+\=]", s) |
75 | | - if search: |
76 | | - print("Error: Unrecognized character in '" + s + "'") |
77 | | - sys.exit(-42) |
78 | | - return s |
| 61 | + s = s.lower().replace("&", "and") |
| 62 | + |
| 63 | + # Put spaces between and remove blank characters. |
| 64 | + blankCharsPad = r"()\[\],.\\\?\#/\!\$\:" |
| 65 | + blankCharsNoPad = r"'\"" |
| 66 | + s = re.sub(r"([" + blankCharsPad + r"])([^ ])", "\\1 \\2", s) |
| 67 | + s = re.sub("[" + blankCharsPad + blankCharsNoPad + "]", "", s) |
| 68 | + |
| 69 | + # Replace spaces with a single dash. |
| 70 | + s = re.sub(r"[ \*\_]+", "-", s) |
| 71 | + s = re.sub("-+", "-", s) |
| 72 | + s = re.sub("^-*", "", s) |
| 73 | + s = re.sub("-*$", "", s) |
| 74 | + |
| 75 | + # Ensure the string is only alphanumeric with '-', '+', and '='. |
| 76 | + search = re.search("[^0-9a-z\-\+\=]", s) |
| 77 | + if search: |
| 78 | + print("Error: Unrecognized character in '" + s + "'") |
| 79 | + sys.exit(-42) |
| 80 | + return s |
| 81 | + |
79 | 82 |
|
80 | 83 | def artist(artistDir): |
81 | | - print("Organizing artist '" + artistDir + "'.") |
82 | | - if not args.ignore_multiple_artists: |
83 | | - artists = set() |
| 84 | + print("Organizing artist '" + artistDir + "'.") |
| 85 | + if not args.ignore_multiple_artists: |
| 86 | + artists = set() |
| 87 | + for dirname, dirnames, filenames in os.walk(artistDir): |
| 88 | + # Make sure there aren't a lot of different artists |
| 89 | + # in case this was called from the wrong directory. |
| 90 | + for filename in filenames: |
| 91 | + try: |
| 92 | + audio = EasyID3(os.path.join(dirname, filename)) |
| 93 | + artist = audio['artist'][0].decode() |
| 94 | + artists.add(artist) |
| 95 | + except: |
| 96 | + pass |
| 97 | + |
| 98 | + if len(artists) > 2: |
| 99 | + while True: |
| 100 | + print("Warning: More than 2 artists found in '{}'.".format( |
| 101 | + artistDir)) |
| 102 | + print("This will move all songs to the '{}' directory.".format( |
| 103 | + artistDir)) |
| 104 | + print("Continue? yes/no") |
| 105 | + choice = raw_input().lower() |
| 106 | + valid = {"yes": True, "y": True, "no": False, "n": False} |
| 107 | + if choice in valid: |
| 108 | + if valid[choice]: |
| 109 | + break |
| 110 | + else: |
| 111 | + print("Exiting.") |
| 112 | + sys.exit(-1) |
| 113 | + |
| 114 | + delete_dirs = [] |
84 | 115 | for dirname, dirnames, filenames in os.walk(artistDir): |
85 | | - # Make sure there aren't a lot of different artists |
86 | | - # in case this was called from the wrong directory. |
87 | | - for filename in filenames: |
88 | | - try: |
89 | | - audio = EasyID3(os.path.join(dirname, filename)) |
90 | | - artist = audio['artist'][0].decode() |
91 | | - artists.add(artist) |
92 | | - except: |
93 | | - pass |
94 | | - |
95 | | - if len(artists) > 2: |
96 | | - while True: |
97 | | - print("Warning: More than 2 artists found in '" + artistDir + "'.") |
98 | | - print("This will move all songs to the '" + artistDir + "' directory.") |
99 | | - print("Continue? yes/no") |
100 | | - choice = raw_input().lower() |
101 | | - valid = {"yes":True, "y":True, "no":False, "n":False} |
102 | | - if choice in valid: |
103 | | - if valid[choice]: break |
104 | | - else: |
105 | | - print("Exiting.") |
106 | | - sys.exit(-1) |
107 | | - |
108 | | - delete_dirs = [] |
109 | | - for dirname, dirnames, filenames in os.walk(artistDir): |
110 | | - # Move all the files to the root directory. |
111 | | - for filename in filenames: |
112 | | - ext = os.path.splitext(filename)[1] |
113 | | - if ext == ".mp3": |
114 | | - fullPath = os.path.join(dirname, filename) |
115 | | - print("file: " + str(fullPath)) |
116 | | - |
117 | | - try: |
118 | | - audio = EasyID3(fullPath) |
119 | | - title = audio['title'][0].encode('ascii', 'ignore') |
120 | | - print(" title: " + title) |
121 | | - except: title = None |
122 | | - |
123 | | - if not title: |
124 | | - print("Error: title not found for '" + filename + "'") |
125 | | - sys.exit(-42) |
126 | | - |
127 | | - neatTitle = toNeat(title) |
128 | | - print(" neatTitle: " + neatTitle) |
129 | | - |
130 | | - newFullPath = os.path.join(artistDir, neatTitle + ext) |
131 | | - print(" newFullPath: " + newFullPath) |
132 | | - |
133 | | - if newFullPath != fullPath: |
134 | | - if os.path.isfile(newFullPath): |
135 | | - if args.delete_conflicts: |
136 | | - os.remove(fullPath) |
137 | | - print("File exists: '" + newFullPath + "'") |
138 | | - print("Deleted: '" + fullPath + "'") |
| 116 | + # Move all the files to the root directory. |
| 117 | + for filename in filenames: |
| 118 | + ext = os.path.splitext(filename)[1] |
| 119 | + if ext == ".mp3": |
| 120 | + fullPath = os.path.join(dirname, filename) |
| 121 | + print("file: " + str(fullPath)) |
| 122 | + |
| 123 | + try: |
| 124 | + audio = EasyID3(fullPath) |
| 125 | + title = audio['title'][0].encode('ascii', 'ignore') |
| 126 | + print(" title: " + title) |
| 127 | + except: |
| 128 | + title = None |
| 129 | + |
| 130 | + if not title: |
| 131 | + print("Error: title not found for '" + filename + "'") |
| 132 | + sys.exit(-42) |
| 133 | + |
| 134 | + neatTitle = toNeat(title) |
| 135 | + print(" neatTitle: " + neatTitle) |
| 136 | + |
| 137 | + newFullPath = os.path.join(artistDir, neatTitle + ext) |
| 138 | + print(" newFullPath: " + newFullPath) |
| 139 | + |
| 140 | + if newFullPath != fullPath: |
| 141 | + if os.path.isfile(newFullPath): |
| 142 | + if args.delete_conflicts: |
| 143 | + os.remove(fullPath) |
| 144 | + print("File exists: '" + newFullPath + "'") |
| 145 | + print("Deleted: '" + fullPath + "'") |
| 146 | + else: |
| 147 | + print("Error: File exists: '" + newFullPath + "'") |
| 148 | + sys.exit(-42) |
| 149 | + else: |
| 150 | + os.rename(fullPath, newFullPath) |
| 151 | + elif ext == ".pdf": |
| 152 | + pass |
139 | 153 | else: |
140 | | - print("Error: File exists: '" + newFullPath + "'") |
141 | | - sys.exit(-42) |
142 | | - else: |
143 | | - os.rename(fullPath, newFullPath) |
144 | | - elif ext == ".pdf": |
145 | | - pass |
146 | | - else: |
147 | | - if not args.delete_unrecognized: |
148 | | - print("Error: Unrecognized file extension in '" + filename + "'") |
149 | | - sys.exit(-42) |
150 | | - |
151 | | - # Delete all subdirectories. |
152 | | - for subdirname in dirnames: |
153 | | - delete_dirs.append(subdirname) |
154 | | - |
155 | | - for d in delete_dirs: |
156 | | - shutil.rmtree(os.path.join(artistDir,d),ignore_errors=True) |
| 154 | + if not args.delete_unrecognized: |
| 155 | + print("Error: Unrecognized file extension in '{}'.".format( |
| 156 | + filename)) |
| 157 | + sys.exit(-42) |
| 158 | + |
| 159 | + # Delete all subdirectories. |
| 160 | + for subdirname in dirnames: |
| 161 | + delete_dirs.append(subdirname) |
| 162 | + |
| 163 | + for d in delete_dirs: |
| 164 | + shutil.rmtree(os.path.join(artistDir, d), ignore_errors=True) |
| 165 | + |
157 | 166 |
|
158 | 167 | def song(filename): |
159 | | - if filename[0] == '.': |
160 | | - print("Ignoring dotfile: '{}'".format(filename)) |
161 | | - return |
162 | | - print("Organizing song '" + filename + "'.") |
163 | | - ext = os.path.splitext(filename)[1] |
164 | | - try: |
165 | | - audio = EasyID3(filename) |
166 | | - artist = audio['artist'][0].encode('ascii', 'ignore') |
167 | | - title = audio['title'][0].encode('ascii', 'ignore') |
168 | | - print(" artist: " + artist) |
169 | | - print(" title: " + title) |
170 | | - except: |
171 | | - artist = None |
172 | | - title = None |
173 | | - neatArtist = toNeat(artist) |
174 | | - neatTitle = toNeat(title) |
175 | | - print(" neatArtist: " + neatArtist) |
176 | | - print(" neatTitle: " + neatTitle) |
177 | | - if not os.path.isdir(neatArtist): |
178 | | - os.mkdir(neatArtist) |
179 | | - newFullPath = os.path.join(neatArtist,neatTitle+ext) |
180 | | - os.rename(filename, newFullPath) |
| 168 | + if filename[0] == '.': |
| 169 | + print("Ignoring dotfile: '{}'".format(filename)) |
| 170 | + return |
| 171 | + print("Organizing song '" + filename + "'.") |
| 172 | + ext = os.path.splitext(filename)[1] |
| 173 | + try: |
| 174 | + audio = EasyID3(filename) |
| 175 | + artist = audio['artist'][0].encode('ascii', 'ignore') |
| 176 | + title = audio['title'][0].encode('ascii', 'ignore') |
| 177 | + print(" artist: " + artist) |
| 178 | + print(" title: " + title) |
| 179 | + except: |
| 180 | + artist = None |
| 181 | + title = None |
| 182 | + neatArtist = toNeat(artist) |
| 183 | + neatTitle = toNeat(title) |
| 184 | + print(" neatArtist: " + neatArtist) |
| 185 | + print(" neatTitle: " + neatTitle) |
| 186 | + if not os.path.isdir(neatArtist): |
| 187 | + os.mkdir(neatArtist) |
| 188 | + newFullPath = os.path.join(neatArtist, neatTitle + ext) |
| 189 | + os.rename(filename, newFullPath) |
| 190 | + |
181 | 191 |
|
182 | 192 | def collection(): |
183 | | - for f in os.listdir("."): |
184 | | - if os.path.isdir(f): |
185 | | - if f != 'iTunes' and f != 'playlists': |
186 | | - artist(f) |
187 | | - elif os.path.isfile(f): song(f) |
| 193 | + for f in os.listdir("."): |
| 194 | + if os.path.isdir(f): |
| 195 | + if f != 'iTunes' and f != 'playlists': |
| 196 | + artist(f) |
| 197 | + elif os.path.isfile(f): |
| 198 | + song(f) |
188 | 199 |
|
189 | 200 | if args.artist: |
190 | | - artist('.') |
| 201 | + artist('.') |
191 | 202 | else: |
192 | | - collection() |
| 203 | + collection() |
193 | 204 | print("\nComplete!") |
0 commit comments