annotate graphconvert.py @ 43:b5d3d9a8c83d

new graph of just the events happening now
author drewp@bigasterisk.com
date Mon, 19 Feb 2024 13:53:46 -0800
parents 7d9609edcf9c
children e53a1bc87f99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
1 from localtypes import Conf, feedFromCalId
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
2 from rdflib import RDF, RDFS, ConjunctiveGraph, Literal, Namespace, URIRef
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
3
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
4 EV = Namespace("http://bigasterisk.com/event#")
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
5
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
6
42
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
7 def asGraph(conf: Conf, cals, events, extraClasses=[], ctxUri:URIRef=EV['gcalendar']):
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
8 ctx = ConjunctiveGraph(identifier=ctxUri)
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
9 graph = ConjunctiveGraph()
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
10 graph.namespace_manager.bind('ev', EV)
42
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
11
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
12 for doc in cals:
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
13 feed = URIRef(feedFromCalId(conf, doc['_id']))
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
14 graph.add((feed, RDF.type, EV['CalendarFeed'], ctx))
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
15 if doc['summary']:
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
16 graph.add((feed, RDFS.label, Literal(doc['summary'].strip()), ctx))
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
17 if doc['description']:
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
18 graph.add((feed, EV['description'], Literal(doc['description'].strip()), ctx))
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
19
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
20 for ev in events:
43
b5d3d9a8c83d new graph of just the events happening now
drewp@bigasterisk.com
parents: 42
diff changeset
21 uri = URIRef(ev.get('uri', ev.get('_id')))
b5d3d9a8c83d new graph of just the events happening now
drewp@bigasterisk.com
parents: 42
diff changeset
22 if uri is None:
b5d3d9a8c83d new graph of just the events happening now
drewp@bigasterisk.com
parents: 42
diff changeset
23 raise ValueError(f"{ev=} had no event uri")
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
24
42
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
25 def add(p: URIRef, o: URIRef|Literal):
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
26 return graph.add((uri, p, o, ctx))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
27
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
28 add(RDF.type, EV['Event'])
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
29 for cls in extraClasses:
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
30 add(RDF.type, cls)
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
31 add(EV['title'], Literal(ev['title']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
32 add(EV['start'], Literal(ev['start']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
33 add(EV['startDate'], Literal(ev['startDate']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
34 add(EV['end'], Literal(ev['end']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
35 add(EV['feed'], URIRef(ev['feed']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
36 # graph.add((feed, RDFS.label, Literal(ev['feedTitle'])))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
37 if 'htmlLink' in ev:
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
38 add(EV['htmlLink'], URIRef(ev['htmlLink']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
39 return graph