annotate 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
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:
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
21 uri = URIRef(ev['uri'])
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
22
42
7d9609edcf9c track calendar feed summary/description text and emit them in graphs
drewp@bigasterisk.com
parents: 28
diff changeset
23 def add(p: URIRef, o: URIRef|Literal):
28
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
24 return graph.add((uri, p, o, ctx))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
25
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
26 add(RDF.type, EV['Event'])
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
27 for cls in extraClasses:
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
28 add(RDF.type, cls)
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
29 add(EV['title'], Literal(ev['title']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
30 add(EV['start'], Literal(ev['start']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
31 add(EV['startDate'], Literal(ev['startDate']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
32 add(EV['end'], Literal(ev['end']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
33 add(EV['feed'], URIRef(ev['feed']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
34 # graph.add((feed, RDFS.label, Literal(ev['feedTitle'])))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
35 if 'htmlLink' in ev:
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
36 add(EV['htmlLink'], URIRef(ev['htmlLink']))
e2209226b001 rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff changeset
37 return graph