Browse code
initial commit
fiddlerwoaroof authored on 02/03/2014 07:57:23
Showing 1 changed files
Showing 1 changed files
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,91 @@ |
1 |
+import os |
|
2 |
+import string |
|
3 |
+import mutagen |
|
4 |
+import argparse |
|
5 |
+ |
|
6 |
+parser=argparse.ArgumentParser() |
|
7 |
+parser.add_argument('destination') |
|
8 |
+args = parser.parse_args() |
|
9 |
+ |
|
10 |
+destination = args.destination |
|
11 |
+print 'copying music to, %s' % destination |
|
12 |
+ |
|
13 |
+import sys |
|
14 |
+ |
|
15 |
+class PathFinder(object): |
|
16 |
+ def __init__(self): |
|
17 |
+ pass |
|
18 |
+ |
|
19 |
+ def get_path(self, data, orig_name): |
|
20 |
+ orig_name = orig_name.rpartition('.')[0] |
|
21 |
+ title=self.clean(data.get('title', ['Unknown Title'])[0].encode('utf-8')) |
|
22 |
+ if title.strip() == '': title = orig_name |
|
23 |
+ album=self.clean(data.get('album', ['Unknown Album'])[0].encode('utf-8')) |
|
24 |
+ if album.strip() == '': album = 'Unknown Album' |
|
25 |
+ |
|
26 |
+ if 'performer' in data: artist = data['performer'][0].encode('utf-8') |
|
27 |
+ elif 'conductor' in data: artist = data['conductor'][0].encode('utf-8') |
|
28 |
+ else: artist = data.get('artist', ['Unknown Artist'])[0].encode('utf-8') |
|
29 |
+ artist = self.clean(artist) |
|
30 |
+ if artist.strip() == '': artist = 'Unknown Artist' |
|
31 |
+ |
|
32 |
+ tracknumber=self.clean(data.get('tracknumber', ['--'])[0].encode('utf-8')) |
|
33 |
+ if tracknumber != '--': |
|
34 |
+ try: |
|
35 |
+ tracknumber = tracknumber.partition('_')[0] |
|
36 |
+ tracknumber = '%02d' % int(tracknumber) |
|
37 |
+ except ValueError: |
|
38 |
+ pass |
|
39 |
+ discnumber=self.clean(data.get('discnumber', ['--'])[0].partition('/')[0].encode('utf-8')) |
|
40 |
+ if discnumber != '--': discnumber = '%02d' % int(discnumber) |
|
41 |
+ result =[ os.path.join(artist[0].upper(),artist,album), '%s_%s' % (tracknumber, title) ] |
|
42 |
+ if discnumber != '--': |
|
43 |
+ result[1] = '%s_%s' % (discnumber, result[1]) |
|
44 |
+ return result |
|
45 |
+ |
|
46 |
+ validchars = frozenset('_.- %s%s' % (string.ascii_letters, string.digits)) |
|
47 |
+ def clean(self, strin): |
|
48 |
+ strin = ''.join((c if c in self.validchars else " ") for c in strin) |
|
49 |
+ strin = '_'.join(strin.split()) |
|
50 |
+ if len(strin) > 40: |
|
51 |
+ pa = strin[:28] |
|
52 |
+ pb = strin[-31:] |
|
53 |
+ strin = '...'.join([pa,pb]) |
|
54 |
+ return strin |
|
55 |
+ |
|
56 |
+ |
|
57 |
+ |
|
58 |
+ |
|
59 |
+ |
|
60 |
+pf = PathFinder() |
|
61 |
+for line in sys.stdin: |
|
62 |
+ line = line.strip() |
|
63 |
+ try: |
|
64 |
+ file = mutagen.File(line, easy=True) |
|
65 |
+ x = 0 |
|
66 |
+ suf = '' |
|
67 |
+ path, name = pf.get_path(file, os.path.basename(line)) |
|
68 |
+ ext = line.rpartition('.')[-1] |
|
69 |
+ |
|
70 |
+ path = os.path.join(destination, path) |
|
71 |
+ |
|
72 |
+ checkname = '%s.%s' % ('__'.join(filter(None, [name, suf])), ext) |
|
73 |
+ |
|
74 |
+ while os.path.exists(os.path.join(path, '%s.%s' % ('__'.join(filter(None, [name, suf])), ext))): |
|
75 |
+ x += 1 |
|
76 |
+ suf = '%02d' % x |
|
77 |
+ |
|
78 |
+ name = '%s.%s' % ('__'.join(filter(None, [name, suf])), ext) |
|
79 |
+ try: |
|
80 |
+ os.makedirs(path) |
|
81 |
+ except OSError, e: |
|
82 |
+ if e.errno == 17: |
|
83 |
+ pass |
|
84 |
+ else: |
|
85 |
+ raise |
|
86 |
+ |
|
87 |
+ print os.path.join(path, name) |
|
88 |
+ os.link(line, os.path.join(path, name)) |
|
89 |
+ except Exception, e: |
|
90 |
+ print >> sys.stderr, 'Error Processing: %s' % line,e |
|
91 |
+ print 'Error Processing: %s' % line,e |