-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarktable_to_mediagoblin.py
139 lines (103 loc) · 3.77 KB
/
darktable_to_mediagoblin.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
#!/usr/bin/env python
"""
Generate a mediagoblin batch upload package based off the metadata
from darktable-generated XML files.
Version 0.1.1
Copyright 2017 George C. Privon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import re
import sys
import glob
import argparse
from lxml import etree
def get_args():
"""
Parse arguments
"""
parser = argparse.ArgumentParser(description="Generate mediagoblin batch \
upload files from darktable metadata.")
parser.add_argument('--outfile', '-o', type=str, default='upload.csv',
help='Output csv file for batch upload.')
parser.add_argument('searchdir', type=str, default='./',
help='Directory to search for darktable_exported \
files.')
return parser.parse_args()
def find_folders(path):
"""
Given a directory, return a list of sub-folders containing
'darktable_exported' folders.
"""
folders = []
subdirs = os.walk(path)
for directory in subdirs:
if re.search('darktable_exported', directory[0]):
folders.append(directory[0])
return folders
def get_metadata(fname):
"""
Read a specified darktable xml file and return the title and tags.
"""
metadata = {'title': '',
'tags': [],
'desc': ''}
errreturn = metadata
if not os.path.isfile(fname):
return errreturn
mdata = etree.parse(fname)
xmlroot = mdata.getroot()
if not re.search('xmpmeta', xmlroot.tag):
return errreturn
for elem in xmlroot.getchildren():
if re.search('RDF', elem.tag):
continue
for selem in elem.getchildren():
if re.search('Description', selem.tag):
continue
for sselem in selem.getchildren():
if re.search('title', sselem.tag):
metadata['title'] = sselem.getchildren()[0].getchildren()[0].text
elif re.search('description', sselem.tag):
metadata['desc'] = sselem.getchildren()[0].getchildren()[0].text
elif re.search('subject', sselem.tag):
for tag in sselem.getchildren()[0].getchildren():
metadata['tags'].append(tag.text)
return metadata
def main():
"""
do the main things
"""
args = get_args()
outf = open(args.outfile, 'w')
outf.write('location,dc:title,dc:tags\n')
folders = find_folders(args.searchdir)
for folder in folders:
files = sorted(glob.glob(folder + '/*.jpg'))
for fname in files:
if not os.path.isfile(fname):
sys.stderr.write(fname + ' not found. Skipping.\n')
outf.write('"' + fname + '",')
iname = fname.split('darktable_exported/')[-1].split('.jpg')[0]
if re.search('\d\d\d\d-\d\d-\d\d', iname):
mname = iname.rsplit('-', maxsplit=1)[1]
else:
mname = iname
mname = folder.split('darktable_exported')[0] + mname + '.CR2.xmp'
mdata = get_metadata(mname)
if mdata['title'] == '':
mdata['title'] = iname
outf.write('"' + mdata['title'] + '","')
outf.write(','.join(mdata['tags']) + '"\n')
outf.close()
if __name__ == "__main__":
main()