Skip to content

Instantly share code, notes, and snippets.

@miku
Last active August 29, 2015 13:59
Show Gist options
  • Save miku/10982871 to your computer and use it in GitHub Desktop.
Save miku/10982871 to your computer and use it in GitHub Desktop.
Command line converter: RDFXML from an URL to JSON-LD
*pyc
*rdftojld.build
#!/usr/bin/env python
# coding: utf-8
"""
Convert RDFXML to JSON-LD.
Usage:
$ python rdftojld.py http://dbpedia.org/data/Category:High_fantasy_novels.rdf
{
"@context": {
"dbpedia-owl": "http://dbpedia.org/ontology/",
"dcterms": "http://purl.org/dc/terms/",
"ns6": "http://www.w3.org/ns/prov#",
"owl": "http://www.w3.org/2002/07/owl#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"skos": "http://www.w3.org/2004/02/skos/core#",
"xsd": "http://www.w3.org/2001/XMLSchema#"
},
"@graph": [
{
"@id": "http://dbpedia.org/resource/Category:Old_Kingdom_series_books",
"skos:broader": {
"@id": "http://dbpedia.org/resource/Category:High_fantasy_novels"
}
},
...
Build a static binary:
$ nuitka --recurse-all rdftojld.py
"""
from __future__ import print_function
from rdflib import Graph, plugin
from rdflib.serializer import Serializer
import argparse
import requests
import sys
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('url', type=str, nargs=1, help='url to semantic data')
parser.add_argument('-f', '--format', default='xml', help='xml, n3')
args = parser.parse_args()
g = Graph().parse(data=requests.get(args.url[0]).text, format=args.format)
print(g.serialize(format='json-ld', indent=4, auto_compact=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment