Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python
import os
import xml.etree.ElementTree as ET
import sys
try:
from urllib.parse import quote
except ImportError:
from urllib import quote
ns = {
'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'z': 'http://www.zotero.org/namespaces/export#',
'dc': 'http://purl.org/dc/elements/1.1/',
'link': 'http://purl.org/rss/1.0/modules/link/',
'dcterms': 'http://purl.org/dc/terms/',
}
for prefix, url in ns.items():
ET.register_namespace(prefix, url)
extensions = ['.' + ext for ext in sys.argv[1:]]
print(extensions)
def elementID(path):
return '#' + quote(path, safe='')
rdf = ET.Element('{%s}RDF' % (ns['rdf'],))
for path, dirs, files in os.walk("."):
path = path[2:] # removes './'
if path != '':
coll = ET.SubElement(rdf, '{%s}Collection' % (ns['z'],),)
coll.set('{%s}about' % (ns['rdf'],), elementID(path))
ET.SubElement(coll, '{%s}title' % (ns['dc'],)).text = os.path.basename(path)
elt = ET.SubElement(coll, '{%s}type' % (ns['rdf'],))
elt.set('{%s}resource' % (ns['rdf'],), 'http://www.zotero.org/namespaces/export#Collection')
for d in dirs:
elt = ET.SubElement(coll, '{%s}hasPart' % (ns['dcterms'],))
elt.set('{%s}resource' % (ns['rdf'],), elementID(os.path.join(path, d)))
for f in files:
if not os.path.splitext(f)[1] in extensions: continue
elt = ET.SubElement(coll, '{%s}hasPart' % (ns['dcterms'],))
elt.set('{%s}resource' % (ns['rdf'],), elementID(os.path.join(path, f)))
for f in files:
if not os.path.splitext(f)[1] in extensions: continue
att = ET.SubElement(rdf, '{%s}Attachment' % (ns['z'],))
att.set('{%s}about' % (ns['rdf'],), elementID(os.path.join(path, f)))
ET.SubElement(att, '{%s}itemType' % (ns['z'],)).text = 'attachment'
elt = ET.SubElement(att, '{%s}resource' % (ns['rdf'],))
elt.set('{%s}resource' % (ns['rdf'],), os.path.join(path, f))
ET.SubElement(att, '{%s}title' % (ns['dc'],)).text = f
#ET.SubElement(att, '{%s}type' % (ns['link'],)).text =
with open('attachments.rdf', 'wb') as f:
ET.ElementTree(rdf).write(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.