Created
April 13, 2016 20:43
-
-
Save zednis/226fc1a65e468565e08640794edeb63f to your computer and use it in GitHub Desktop.
RDF generation script for IANA Media Types
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
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