view graphconvert.py @ 42:7d9609edcf9c

track calendar feed summary/description text and emit them in graphs
author drewp@bigasterisk.com
date Sun, 18 Feb 2024 12:34:53 -0800
parents e2209226b001
children b5d3d9a8c83d
line wrap: on
line source

from localtypes import Conf, feedFromCalId
from rdflib import RDF, RDFS, ConjunctiveGraph, Literal, Namespace, URIRef

EV = Namespace("http://bigasterisk.com/event#")


def asGraph(conf: Conf, cals, events, extraClasses=[], ctxUri:URIRef=EV['gcalendar']):
    ctx = ConjunctiveGraph(identifier=ctxUri)
    graph = ConjunctiveGraph()
    graph.namespace_manager.bind('ev', EV)

    for doc in cals:
        feed = URIRef(feedFromCalId(conf, doc['_id']))
        graph.add((feed, RDF.type, EV['CalendarFeed'], ctx))
        if doc['summary']:
            graph.add((feed, RDFS.label, Literal(doc['summary'].strip()), ctx))
        if doc['description']:
            graph.add((feed, EV['description'], Literal(doc['description'].strip()), ctx))

    for ev in events:
        uri = URIRef(ev['uri'])

        def add(p: URIRef, o: URIRef|Literal):
            return graph.add((uri, p, o, ctx))

        add(RDF.type, EV['Event'])
        for cls in extraClasses:
            add(RDF.type, cls)
        add(EV['title'], Literal(ev['title']))
        add(EV['start'], Literal(ev['start']))
        add(EV['startDate'], Literal(ev['startDate']))
        add(EV['end'], Literal(ev['end']))
        add(EV['feed'], URIRef(ev['feed']))
        # graph.add((feed, RDFS.label, Literal(ev['feedTitle'])))
        if 'htmlLink' in ev:
            add(EV['htmlLink'], URIRef(ev['htmlLink']))
    return graph