view graphconvert.py @ 90:5ace21c3de98

add endDate
author drewp@bigasterisk.com
date Fri, 03 Jan 2025 18:06:08 -0800
parents f75b3a109b66
children
line wrap: on
line source

from pprint import pformat, pprint
from dateutil.tz import tzlocal
from rdflib import RDF, RDFS, ConjunctiveGraph, Literal, Namespace, URIRef
import logging
from localtypes import Conf, Record

log = logging.getLogger('conv')
EV = Namespace("http://bigasterisk.com/event#")


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

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

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

        graph.add((URIRef(ev['calendarUrl']), EV['event'], uri, ctx))

        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['endDate'], Literal(ev['endDate']))
        add(EV['end'], Literal(ev['end']))
        add(EV['feed'], URIRef(ev['calendarUrl']))
        if 'htmlLink' in ev:
            add(EV['htmlLink'], URIRef(ev['htmlLink']))
    return graph