-
Notifications
You must be signed in to change notification settings - Fork 4
/
tagfix.py
executable file
·79 lines (57 loc) · 1.73 KB
/
tagfix.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
#!/usr/bin/python3
"""ID3 Tag Fixer.
Usage:
tagfix.py [options] <file>...
[--remove=<tags>...]
[--skip=<tags>...]
[--rename]
Options:
-h, --help Show this screen.
-v, --version Show version.
"""
import os
import re
from is_tagged import is_tagged
from docopt import docopt
from mutagen.mp3 import MP3
from mutagen.id3 import TextFrame
from config import (
RE_WEBSITE,
TAGS_SKIP,
TAGS_REMOVE,
TAGS_REMOVE_PREFIX,
)
def _rename(args, file, tags):
if args['--rename']:
new_path = os.path.join(os.path.dirname(file),
"%s.mp3" % tags['TIT2'])
os.rename(file, new_path)
return new_path
if __name__ == '__main__':
args = docopt(__doc__, version='ID3 Tag Fixer 0.5')
TAGS_REMOVE.extend(args['--remove'])
TAGS_SKIP.extend(args['--skip'])
for file in args['<file>']:
try:
tags = MP3(file)
except:
print("Couldn't read: %s" % file)
continue
if is_tagged(tags):
print("Already tagged: %s " % _rename(args, file, tags))
continue
# Clean tags
for key in list(tags.keys()):
if key in TAGS_REMOVE or key.startswith(TAGS_REMOVE_PREFIX):
del tags[key]
elif key in TAGS_SKIP or not isinstance(tags[key], TextFrame):
continue
elif re.search(RE_WEBSITE, str(tags[key])):
sub = re.sub(RE_WEBSITE, "", str(tags[key]))
if sub:
tags[key].text = [sub]
else:
del tags[key]
# Fingers crossed!
tags.save()
print("Processed:\t%s" % _rename(args, file, tags))