Mercurial > code > home > repos > gcalendarwatch
diff gcalendarwatch.py @ 45:e53a1bc87f99
cleanup and some fixes to starred event graph
author | drewp@bigasterisk.com |
---|---|
date | Thu, 06 Jun 2024 15:57:26 -0700 |
parents | b5d3d9a8c83d |
children | 5f7ae444ecae |
line wrap: on
line diff
--- a/gcalendarwatch.py Mon Feb 19 13:53:51 2024 -0800 +++ b/gcalendarwatch.py Thu Jun 06 15:57:26 2024 -0700 @@ -1,6 +1,5 @@ """ -sync google calendar into mongodb, return queries from that as -JSON-LD. +sync google calendar into mongodb, return a few preset queries as RDF graphs gcalendarwatch.conf looks like this: { @@ -16,10 +15,9 @@ import functools import json import logging -import re import time import traceback -from typing import Any, Dict, Iterable, Optional, cast +from typing import Iterable, cast import background_loop import pymongo.collection @@ -27,14 +25,14 @@ from patchablegraph import PatchableGraph from patchablegraph.handler import GraphEvents, StaticGraph from pymongo import MongoClient -from rdflib import RDF, ConjunctiveGraph, Graph, Namespace, URIRef +from rdflib import Namespace, URIRef from starlette.applications import Starlette from starlette.requests import Request -from starlette.responses import HTMLResponse, JSONResponse, PlainTextResponse, Response +from starlette.responses import HTMLResponse, PlainTextResponse from starlette.routing import Route from starlette_exporter import PrometheusMiddleware, handle_metrics -from datetimemath import dayRange, parse +from datetimemath import dayRange from graphconvert import asGraph from ingest import SyncToMongo from localtypes import Conf, Record @@ -65,55 +63,6 @@ }""" -def asJsonLd(events): - ret = {'@graph': []} # type: Dict[Any, Any] - for ev in events: - ev['startTime'] = ev['startTime'].astimezone(tzlocal()).isoformat() - ev['endTime'] = ev['endTime'].astimezone(tzlocal()).isoformat() - ev['@id'] = ev.pop('uri') - ret['@graph'].append(ev) - - ret['@context'] = { - "xsd": "http://www.w3.org/2001/XMLSchema#", - "ev": "http://bigasterisk.com/event#", - "startTime": { - "@id": "ev:startTime", - "@type": "xsd:dateTime" - }, - "endTime": { - "@id": "ev:endTime", - "@type": "xsd:dateTime" - }, - "startDate": { - "@id": "ev:startDate", - "@type": "xsd:date" - }, - "endDate": { - "@id": "ev:endDate", - "@type": "xsd:date" - }, - "title": "ev:title", - "feed": { - "@id": "ev:feed", - "@type": "@id" - }, - "htmlLink": { - "@id": "ev:htmlLink", - "@type": "@id" - }, - } - return ret - - -def starred(graph: Graph, ev: URIRef) -> Optional[str]: - title = str(graph.value(ev, EV['title'])) - m = re.search(r'(.*)\*\s*$', title) - if m: - return m.group(1) - else: - return None - - class ReadMongoEvents(object): """read events from mongodb""" @@ -138,7 +87,7 @@ ) -> int: log.info(f"updating {cal or 'all'}") try: - n = sync.update(cal=cal) + n = sync.update(cal=cal, days=120) except Exception: traceback.print_exc() log.error("update failed") @@ -146,40 +95,6 @@ return n -# who uses this? pimscreen, frontdoor? they should use the GraphEvents version -def EventsPage(conf: Conf, read: ReadMongoEvents, req: Request) -> Response: - t1, t2 = dayRange(int(req.query_params.get('days', default='2'))) - if req.query_params.get('t1', default=None): - t1 = parse(req.query_params.get('t1', ''), tzinfo=tzlocal()) - if req.query_params.get('t2', default=None): - t2 = parse(req.query_params.get('t2', ''), tzinfo=tzlocal()) - log.info(f'get /events local t1={t1} t2={t2}') - return PlainTextResponse(media_type="text/n3", content=asGraph(conf, cals=[], events=read.getEvents(t1, t2)).serialize(format='n3')) - - -def Countdowns(countdownGraph, req: Request) -> Response: - rows = [] - graph = countdownGraph._graph - for ev in graph.subjects(RDF.type, EV['Event']): - starLabel = starred(graph, ev) - if starLabel is not None: - rows.append({'@type': 'countdown', 'time': graph.value(ev, EV['start']), 'label': starLabel}) - return JSONResponse(media_type="application/ld+json", - content={ - "@context": { - "countdown": "http://bigasterisk.com/countdown#CountdownEvent", - "label": "http://www.w3.org/2000/01/rdf-schema#label", - "time": { - "@id": "http://bigasterisk.com/event#time", - "@type": "xsd:dateTime" - }, - "xsd": "http://www.w3.org/2001/XMLSchema#", - "rdfs": "http://www.w3.org/2000/01/rdf-schema#" - }, - "@graph": rows, - }) - - def statusMsg(conf, loop: background_loop.Loop): period = conf['minutes_between_polls'] * 60 ago = time.time() - loop.lastSuccessRun @@ -221,7 +136,7 @@ sync = SyncToMongo(conf, mongoOut, agendaGraph, countdownGraph) read = ReadMongoEvents(mongoOut) - s, e = dayRange(60) + s, e = dayRange(120) sync.updateGraphs(read.getEvents(s, e)) loop = background_loop.loop_forever(functools.partial(update, sync=sync), conf['minutes_between_polls'] * 60) @@ -239,8 +154,6 @@ Route('/graph/calendar/countdown/events', GraphEvents(countdownGraph)), Route('/graph/currentEvents', StaticGraph(currentEventsGraph)), Route('/graph/currentEvents/events', GraphEvents(currentEventsGraph)), - Route('/countdowns.json', functools.partial(Countdowns, countdownGraph)), - Route('/events', functools.partial(EventsPage, conf, read)), Route('/pollNow', functools.partial(PollNow, loop), methods=['POST']) ])