Linked Data Fragments

I’m trying to construct a function that will allow you to access LDF in conjunction with PWB. Should look a bit like this:

for subject, predicate, object in WikidataTriples(predicate="P31", object="Q5"):
    print(subject)

where the parameters could, for convenience, be IDs, namespaced IRIs ("wd:Q5"), or full pywikibot pages.

Current status

Just playing around.

import json
from pywikibot.comms import http
from urllib.parse import quote
ldfBase = "https://query.wikidata.org/bigdata/ldf?"
itemBase = "http://www.wikidata.org/entity/"
propertyBase = "http://www.wikidata.org/prop/direct/"
cat = "Q146"
url = ldfBase + "&".join([
        #"predicate=" + quote(propertyBase + "P31"),
        "subject=" + quote(itemBase + "Q26536085"),
        "predicate=",
        "object="
    ])
response = http.fetch(url, headers={'Accept': 'application/ld+json'})
data = json.loads(response.content)
# iterate over data["@graph"], find every object with an "@id" key with value "wd:Q_", other triples are in "P_": "wd:Q_" form
data["@context"]
{'P1216': {'@id': 'http://www.wikidata.org/prop/P1216', '@type': '@id'},
 'P131': {'@id': 'http://www.wikidata.org/prop/direct/P131', '@type': '@id'},
 'P1435': {'@id': 'http://www.wikidata.org/prop/direct/P1435', '@type': '@id'},
 'P17': {'@id': 'http://www.wikidata.org/prop/P17', '@type': '@id'},
 'P31': {'@id': 'http://www.wikidata.org/prop/direct/P31', '@type': '@id'},
 'P625': {'@id': 'http://www.wikidata.org/prop/P625', '@type': '@id'},
 'cc': 'http://creativecommons.org/ns#',
 'dateModified': {'@id': 'http://schema.org/dateModified',
  '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},
 'dc': 'http://purl.org/dc/terms/',
 'description': 'http://schema.org/description',
 'firstPage': {'@id': 'http://www.w3.org/ns/hydra/core#firstPage',
  '@type': '@id'},
 'foaf': 'http://xmlns.com/foaf/0.1/',
 'geo': 'http://www.opengis.net/ont/geosparql#',
 'itemsPerPage': {'@id': 'http://www.w3.org/ns/hydra/core#itemsPerPage',
  '@type': 'http://www.w3.org/2001/XMLSchema#integer'},
 'label': 'http://www.w3.org/2000/01/rdf-schema#label',
 'mapping': {'@id': 'http://www.w3.org/ns/hydra/core#mapping', '@type': '@id'},
 'owl': 'http://www.w3.org/2002/07/owl#',
 'p': 'http://www.wikidata.org/prop/',
 'pq': 'http://www.wikidata.org/prop/qualifier/',
 'pqn': 'http://www.wikidata.org/prop/qualifier/value-normalized/',
 'pqv': 'http://www.wikidata.org/prop/qualifier/value/',
 'pr': 'http://www.wikidata.org/prop/reference/',
 'prn': 'http://www.wikidata.org/prop/reference/value-normalized/',
 'property': {'@id': 'http://www.w3.org/ns/hydra/core#property',
  '@type': '@id'},
 'prov': 'http://www.w3.org/ns/prov#',
 'prv': 'http://www.wikidata.org/prop/reference/value/',
 'ps': 'http://www.wikidata.org/prop/statement/',
 'psn': 'http://www.wikidata.org/prop/statement/value-normalized/',
 'psv': 'http://www.wikidata.org/prop/statement/value/',
 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
 'schema': 'http://schema.org/',
 'search': {'@id': 'http://www.w3.org/ns/hydra/core#search', '@type': '@id'},
 'skos': 'http://www.w3.org/2004/02/skos/core#',
 'subset': {'@id': 'http://rdfs.org/ns/void#subset', '@type': '@id'},
 'template': 'http://www.w3.org/ns/hydra/core#template',
 'totalItems': {'@id': 'http://www.w3.org/ns/hydra/core#totalItems',
  '@type': 'http://www.w3.org/2001/XMLSchema#integer'},
 'triples': {'@id': 'http://rdfs.org/ns/void#triples',
  '@type': 'http://www.w3.org/2001/XMLSchema#integer'},
 'variable': 'http://www.w3.org/ns/hydra/core#variable',
 'version': {'@id': 'http://schema.org/version',
  '@type': 'http://www.w3.org/2001/XMLSchema#integer'},
 'void': 'http://rdfs.org/ns/void#',
 'wd': 'http://www.wikidata.org/entity/',
 'wdata': 'https://www.wikidata.org/wiki/Special:EntityData/',
 'wdno': 'http://www.wikidata.org/prop/novalue/',
 'wdref': 'http://www.wikidata.org/reference/',
 'wds': 'http://www.wikidata.org/entity/statement/',
 'wdt': 'http://www.wikidata.org/prop/direct/',
 'wdv': 'http://www.wikidata.org/value/',
 'wikibase': 'http://wikiba.se/ontology#',
 'xsd': 'http://www.w3.org/2001/XMLSchema#'}
data["@graph"]
[{'@id': '_:b0', 'property': 'rdf:predicate', 'variable': 'predicate'},
 {'@id': '_:b1',
  'mapping': ['_:b2', '_:b0', '_:b3'],
  'template': 'http://query.wikidata.org/bigdata/ldf{?subject,predicate,object}'},
 {'@id': '_:b2', 'property': 'rdf:subject', 'variable': 'subject'},
 {'@id': '_:b3', 'property': 'rdf:object', 'variable': 'object'},
 {'@id': 'http://query.wikidata.org/bigdata/ldf#dataset',
  '@type': ['void:Dataset', 'http://www.w3.org/ns/hydra/core#Collection'],
  'http://www.w3.org/ns/hydra/core#itemsPerPage': {'@type': 'xsd:long',
   '@value': '100'},
  'search': '_:b1',
  'subset': 'http://query.wikidata.org/bigdata/ldf?subject=http%3A//www.wikidata.org/entity/Q26536085&predicate=&object='},
 {'@id': 'http://query.wikidata.org/bigdata/ldf?subject=http%3A//www.wikidata.org/entity/Q26536085&predicate=&object=',
  '@type': ['http://www.w3.org/ns/hydra/core#Collection',
   'http://www.w3.org/ns/hydra/core#PagedCollection'],
  'firstPage': 'http://query.wikidata.org/bigdata/ldf?subject=http%3A%2F%2Fwww.wikidata.org%2Fentity%2FQ26536085&predicate=&object=&page=1',
  'http://www.w3.org/ns/hydra/core#itemsPerPage': 100,
  'http://www.w3.org/ns/hydra/core#totalItems': 16,
  'void:triples': 16},
 {'@id': 'wd:Q26536085',
  'P1216': 'wds:Q26536085-FCED904F-7F06-444A-84CE-0AFCE089C92C',
  'P131': 'wd:Q635457',
  'P1435': 'wd:Q15700834',
  'P17': 'wds:Q26536085-8D04E875-1CD8-4A53-BCC9-1B8591A4AE78',
  'P31': 'wd:Q3947',
  'P625': 'wds:Q26536085-49F66E15-7AE7-44D6-95CE-A4955734EA07',
  'dateModified': '2016-08-26T23:39:37Z',
  'description': {'@language': 'en', '@value': 'Rowde, Wiltshire, SN10'},
  'label': {'@language': 'en', '@value': 'Manor House'},
  'p:P131': {'@id': 'wds:Q26536085-44302E43-6F33-4F4A-9783-BD631171BF43'},
  'p:P1435': {'@id': 'wds:Q26536085-01720F88-7A41-47C6-84FA-74F5E7538CDC'},
  'p:P31': {'@id': 'wds:Q26536085-6CBDDC3D-632A-41C3-8E3B-D9E1D0C103F7'},
  'schema:version': 370087255,
  'wdt:P1216': '1243406',
  'wdt:P17': {'@id': 'wd:Q145'},
  'wdt:P625': {'@type': 'geo:wktLiteral',
   '@value': 'Point(-2.027844 51.36333)'}}]
url
'https://query.wikidata.org/bigdata/ldf?subject=http%3A//www.wikidata.org/entity/Q26536085&predicate=&object='