Skip to content

Instantly share code, notes, and snippets.

@zednis
Created April 13, 2016 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zednis/226fc1a65e468565e08640794edeb63f to your computer and use it in GitHub Desktop.
Save zednis/226fc1a65e468565e08640794edeb63f to your computer and use it in GitHub Desktop.
RDF generation script for IANA Media Types
import csv
from rdflib import Graph, Literal, URIRef, Namespace, RDF, RDFS
base_uri = "http://www.iana.org/assignments/media-types/"
DCT = Namespace("http://purl.org/dc/terms/")
DCTYPE = Namespace("http://purl.org/dc/dcmitype/")
DCAM = Namespace("http://purl.org/dc/dcam/")
g = Graph()
g.bind("dct", DCT)
g.bind("dcam", DCAM)
# get CSV of MIME type registry assignments from
# http://www.iana.org/assignments/media-types/media-types.xml
mimetype_registries = ["application", "text", "video", "image"]
for mimetype_registry in mimetype_registries:
with open(mimetype_registry+'.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
# pop header row
reader.__next__()
for row in reader:
if not row[1]:
uri = base_uri + mimetype_registry + "/" + row[0]
else:
uri = base_uri + row[1]
mimetype = g.resource(URIRef(uri))
mimetype.add(RDF.type, DCT.FileFormat)
mimetype.add(RDF.type, DCT.MediaTypeOrExtent)
mimetype.add(RDF.value, Literal(row[0]))
mimetype.add(DCAM.memberOf, DCT.IMT)
if mimetype_registry == "image" and row[2]:
mimetype.add(RDFS.label, Literal(row[2]))
g.serialize("mime.ttl", format="turtle")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment