forked from puddletag/puddletag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagbackup.py
104 lines (83 loc) · 3.08 KB
/
tagbackup.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
"""Routines and script to back up audio metadata (puddletag
audioinfo.Tag objects).
Data is stored as json."""
import json
import logging
import os
from optparse import OptionParser
from puddlestuff import audioinfo
from puddlestuff.audioinfo import tag_to_json, b64_to_img
def tags_to_json(dirpath, fields=None):
ret = []
for fn in os.listdir(dirpath):
fn = os.path.join(dirpath, fn)
if os.path.isdir(fn):
ret.extend(tags_to_json(fn, fields))
continue
tag = tag_to_json(fn, fields)
if tag:
ret.append(tag)
return ret
def backup_dir(dirpath, fn, fields=None):
fo = open(fn, 'w')
fo.write(json.dumps(tags_to_json(dirpath, fields)))
fo.close()
def main():
usage = "Usage: %prog [-f FIELDS] [-b dirpath | -r] filename"
parser = OptionParser(usage=usage)
parser.add_option("-b", "--backup", dest="backup",
default='',
help="Backs up all audio tags in dirpath to filename.",
metavar="BACKUP")
parser.add_option("-r", "--restore", dest="restore",
default='',
help="Restores audio tags found in filename.",
metavar="RESTORE", action="store_true")
parser.add_option("-f", "--fields", dest="fields",
default='',
help="Comma separated list of fields. "
"Backed up data will be restricted to this list, but if "
"restored will overwrite the complete file.",
metavar="FIELDS", action='store')
options, filenames = parser.parse_args()
if not (options.backup or options.restore):
"Backs ups and restores audio metadata in directories."
parser.print_help()
exit()
if not filenames:
logging.debug("Fatal Error: Require filename to write backup to!")
exit(1)
filename = filenames[0]
if os.path.exists(filename) and options.backup:
logging.debug('Fatal Error: Backup file %s already exists', filename)
exit(2)
fields = options.fields if options.fields else None
if fields:
fields = [z.strip() for z in fields.split(',')]
if options.backup:
backup_dir(options.backup, filename, fields)
elif options.restore:
restore_backup(filename)
def restore_backup(fn):
for i, tag in enumerate(json.loads(open(fn, 'r').read())):
try:
fn = tag['__path']
except KeyError:
'Error: A file was backed up without a file path.'
try:
audio = audioinfo.Tag(fn)
except EnvironmentError as e:
"Error: Couldn't restore", fn, str(e)
continue
except Exception as e:
"Error: Couldn't restore", fn, str(e)
continue
if '__image' in tag:
images = tag['__image']
del (tag['__image'])
audio.images = list(map(b64_to_img, images))
audio.clear()
audio.update(tag)
audio.save()
if __name__ == '__main__':
main()