view service/reasoning/rdflibtrig.py @ 1384:a29a55f3429c

mqtt_graph_bridge to new build rules and to py3 Ignore-this: 1a064e89a2016ed583c1d4c9bbfd6f7c darcs-hash:b40c5fcff313002f6468e7a94a2059f1dd5dd97c
author drewp <drewp@bigasterisk.com>
date Thu, 09 May 2019 22:31:04 -0700
parents bbaf0576f653
children 3c18b4b3b72c
line wrap: on
line source

import time, logging
from rdflib import ConjunctiveGraph
from rdflib.parser import StringInputSource
import treq
from twisted.internet.defer import inlineCallbacks, returnValue
log = logging.getLogger('fetch')

from private_ipv6_addresses import ipv6Addresses

@inlineCallbacks
def addTrig(graph, url, timeout=2):
    t1 = time.time()
    # workaround for some reason my ipv6 names don't resolve
    for name, addr in ipv6Addresses.iteritems():
        url = url.replace('/' + name + ':', '/[' + addr + ']:')
    log.debug('    fetching %r', url)
    response = yield treq.get(url, headers={'accept': ['application/trig']}, timeout=timeout)
    if response.code != 200:
        raise ValueError("status %s from %s" % (response.code, url))
    g = ConjunctiveGraph()
    g.parse(StringInputSource((yield response.content())), format='trig')
    fetchTime = time.time() - t1
    log.debug('    %r done in %.04f sec', url, fetchTime)
    graph.addN(g.quads())
    returnValue(fetchTime)