Created
February 11, 2020 15:27
-
-
Save retorquere/8edd2f88118c9fd4869e97ebbdcbf54f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import xml.etree.ElementTree as ET | |
import xml.dom.minidom | |
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 = f | |
tree = ET.tostring(rdf).decode() | |
dom = xml.dom.minidom.parseString(tree) | |
with open('attachments.rdf', 'w') as f: | |
f.write(dom.toprettyxml()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment