/
muname.py
executable file
·260 lines (181 loc) · 5.85 KB
/
muname.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/python2.7
# vim: expandtab ts=2 sw=2 :
# TODO: need to validate source and destination directories
# TODO: Define the output format string specification.
import mimetypes
from optparse import OptionParser
import os
import pprint
import shutil
import sys
from mutagen.easyid3 import EasyID3
from mutagen.oggvorbis import OggVorbis
SUPPORTED_TYPES = ['audio/mpeg', 'audio/ogg']
TAGS = ['artist', 'track', 'album', 'album_artist', 'title', 'genre']
DEFAULT_FORMAT = '{artist}/{album}/{track} - {title}'
def GetType(path):
return mimetypes.guess_type(path)[0]
def IsSong(path):
return GetType(path) in SUPPORTED_TYPES
class Song(object):
"""Song Class."""
def __init__(self, path):
self._path = path
tags = self._GetTagInfo()
self.tag_info = self._NormalizeTags(tags)
for tag in TAGS:
setattr(self, tag, self.tag_info.get(tag, None))
def _NormalizeTags(self, tags):
tags['track'] = tags.get('track', None) or tags.get('tracknumber', None)
tag_info = {}
for k, v in tags.items():
tag_info[k] = v[0].encode('utf-8')
return tag_info
def __str__(self):
return ('{artist} - {track} - {album} - {title}'.format(**self.tag_info))
def __repr__(self):
return str(self)
def _GetTagInfo(self):
raise NotImplementedError
class Mp3(Song):
def _GetTagInfo(self):
tags = EasyID3(self._path)
return dict(tags)
class Ogg(Song):
def _GetTagInfo(self):
tags = OggVorbis(self._path)
return dict(tags)
def GetSong(path):
t = GetType(path)
if t == 'audio/mpeg':
return Mp3(path)
elif t == 'audio/ogg':
return Ogg(path)
else:
return None
def _ParseFormatString(path):
parts = []
while path:
(path, end) = os.path.split(path)
parts.insert(0, end)
return parts
class Collection(object):
"""A collection of songs."""
def __init__(self, format_string=DEFAULT_FORMAT):
self._format = format_string
self._path_parts = _ParseFormatString(self._format)
self._songs = []
self._structure = {}
def add(self, song):
"""Add a song to the collection.
Given a song add to collection, place in the collection structure.
Args:
song: Song to put in, either string path or song object.
"""
if not isinstance(song, Song):
song = Song(song)
self._songs.append(song)
self._PutInStructure(song)
def _PutInStructure(self, song):
"""Place song in dir structure."""
level = self._structure
last_level = None
piece = None
for part in self._path_parts:
piece = part.format(**song.tag_info)
next_level = level.get(piece, None)
if not next_level:
level[piece] = {}
next_level = level[piece]
last_level = level
level = next_level
# place song at the last level
last_level[piece] = song
def songs(self):
"""Generator that returns tuples of (path, song)."""
levels = [('', self._structure)]
while levels:
path, cur_level = levels.pop()
if not isinstance(cur_level, Song):
levels.extend([(os.path.join(path, name), obj) for name, obj in cur_level.items()])
continue
# we've hit a song
yield path, cur_level
def __str__(self):
return pprint.pformat(self._structure)
def __repr__(self):
return str(self)
class MuName(object):
"""MuName Class.
Takes a source and destination directory, processes music files and outputs
depending on given output_format.
"""
def __init__(self, destination=None, source=None, no_action=True,
output_format=DEFAULT_FORMAT):
"""MuName __init__
Initialize the MuName object with given parameters.
Args:
destination: Destination directory.
souce: Source directory.
output_format: String format for output.
"""
self._destination = os.path.abspath(destination)
self._output_format = output_format
if os.path.isdir(source):
self._source = os.path.abspath(source)
else:
raise IOError('Given source directory does not exist')
self._collection = None
def Scan(self, collection=None):
"""Build a representation of files in source.
Look at the files in the source directory and extract information for each
of the files.
Args:
collection: Given collection to update, creates a collection if none
given.
Returns:
A representation of the music in the directory.
"""
if not collection:
collection = Collection(format_string=self._output_format)
for root, dirs, files in os.walk(self._source, followlinks=True):
for f in files:
path = os.path.join(root, f)
song = GetSong(path)
if song:
collection.add(song)
self._collection = collection
return collection
def Move(self):
"""Move the collection to the given destination folder."""
for path, song in self._collection.songs():
print 'path: {0} ... song: {1}'.format(os.path.join(self._source, path), song)
def _GetParser():
"""Create the parser with defaults."""
parser = OptionParser()
parser.add_option('-s', '--source',
dest='source',
help='Source Directory')
parser.add_option('-d', '--destination',
dest='destination',
help='Destination Directory')
parser.add_option('-n', '--no-action',
dest='no_action',
action='store_true',
help='Do not actually do the operation')
parser.set_defaults(no_action=False)
return parser
def _GetOptions():
"""Fetch command line args."""
parser = _GetParser()
(options, args) = parser.parse_args()
opt_dict = vars(options)
# TODO at some point, positional args for src and dest would be nice.
return opt_dict
def main():
options = _GetOptions()
muname = MuName(**options)
muname.Scan()
muname.Move()
if __name__ == "__main__":
main()