/
sioclogwww.py
executable file
·365 lines (299 loc) · 11.7 KB
/
sioclogwww.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python
"""sioclogwww.py - a WWW interface for displaying logs
Example usage (in a CGI script):
from sioclogwww import runcgi
runcgi("sioclogbot.log")
"""
import cgi, os
from ircbase import w3c_timestamp, convert_timestamp_to_z
from channellog import OffFilter, ChannelFilter, TimeFilter, HtmlSink, TurtleSink, RawSink, ChannelsAndDaysSink, run, AddLinksFilter, BackLogHtmlSink, ChannelMessageTailFilter, UserFilter, EventSink
from templating import new_context, get_template, expand_template
from turtle import PlainLiteral, TypedLiteral, TurtleWriter
from vocabulary import namespaces, RDF, RDFS, OWL, DC, DCTERMS, XSD, FOAF, SIOC, SIOCT, DS
from users import render_user, render_user_index, get_nick2people
from styles import css_stylesheet
def runcgi(datarooturi, logfiles):
HTTP_HOST = os.environ.get('HTTP_HOST', "")
SERVER_PORT = os.environ.get('SERVER_PORT', "")
REQUEST_URI = os.environ.get('REQUEST_URI', "")
HTTP_ACCEPT = os.environ.get('HTTP_ACCEPT', "")
PATH_INFO = os.environ.get('PATH_INFO', "")
query = cgi.FieldStorage()
up_to = query.getfirst("up_to", None)
# channel = query.getfirst("channel", "")
# timeprefix = query.getfirst("time", "")
if PATH_INFO == "/styles.css":
print "Content-type: text/css"
print
css_stylesheet()
return
elif PATH_INFO == "/sitemap.xml":
print "Content-type: text/xml"
sink = ChannelsAndDaysSink()
run(logfiles, sink)
sitemap_index(sink, datarooturi)
return
if REQUEST_URI.endswith(".html"):
extension = ".html"
format = "html"
elif REQUEST_URI.endswith(".turtle"):
extension = ".turtle"
format = "turtle"
elif REQUEST_URI.endswith(".ttl"):
extension = ".ttl"
format = "turtle"
elif REQUEST_URI.endswith(".txt"):
extension = ".txt"
format = "raw"
else:
# XXX do real content negotiation, e.g. mimeparse.py
extension = ""
if "turtle" in HTTP_ACCEPT:
format = "turtle"
elif "html" in HTTP_ACCEPT:
format = "html"
elif "text" in HTTP_ACCEPT:
format = "raw"
else:
if "Googlebot" in os.environ.get('HTTP_USER_AGENT', ""):
format = "html" # Accept: */* isn't the full truth...
else:
format = "turtle" # default
parts = PATH_INFO.split('/')
# remove extension if any:
if parts[-1].endswith(extension):
parts[-1] = parts[-1][:-len(extension) or None]
if len(parts) > 1 and parts[1] not in ["channels", "users", "backlog"]:
parts.insert(1, "channels") # XXX default type for now
if len(parts) > 1:
restype = parts[1]
else:
restype = ""
if len(parts) > 2:
channel = parts[2]
else:
channel = ""
if len(parts) > 3:
timeprefix = parts[3]
else:
timeprefix = ""
# XXX the following assumes http over port 80, no QUERY_STRING
requesturi = "http://"+HTTP_HOST+REQUEST_URI
datauri = requesturi.split("?")[0] # exclude QUERY_STRING
# remove extension if any, to reset content negotiation in datauri:
if datauri.endswith(extension):
datauri = datauri[:-len(extension) or None]
crumbs = list(create_index_crumbs(datarooturi, datauri, restype, channel,
timeprefix))
if format == "html":
print "Content-type: text/html; charset=utf-8"
print
elif format == "turtle":
print "Content-type: application/x-turtle; charset=utf-8"
print
elif format == "raw":
print "Content-type: text/plain; charset=utf-8"
print
if restype == "users" and channel:
sink = ChannelsAndDaysSink()
run(logfiles, sink)
latestsink = EventSink(datarooturi, None, None, datauri)
latestpipeline = OffFilter(UserFilter(channel, ChannelMessageTailFilter(1, AddLinksFilter(latestsink))))
run(logfiles, latestpipeline)
render_user(sink, format, crumbs, datarooturi, channel, datauri, latestsink)
elif restype == "users":
sink = ChannelsAndDaysSink()
run(logfiles, sink)
render_user_index(sink, format, crumbs, datarooturi, datauri)
elif channel and timeprefix:
# show log
if format == "html":
if restype == "backlog":
# FIXME temporary hack to get the params right:
nick = channel
channel = timeprefix
timeprefix = nick
sink = AddLinksFilter(BackLogHtmlSink(nick, up_to, crumbs, datarooturi, channel, timeprefix, datauri))
timeprefix = ""
else:
sink = AddLinksFilter(HtmlSink(crumbs, datarooturi, channel, timeprefix, datauri))
elif format == "turtle":
sink = AddLinksFilter(TurtleSink(datarooturi, channel, timeprefix))
elif format == "raw":
sink = RawSink()
pipeline = OffFilter(ChannelFilter('#'+channel,
TimeFilter(timeprefix,
sink
)
))
run(logfiles, pipeline)
else:
# show index
sink = ChannelsAndDaysSink()
if channel:
pipeline = ChannelFilter('#'+channel, sink)
elif timeprefix:
pipeline = TimeFilter(timeprefix, sink)
else:
pipeline = sink
run(logfiles, pipeline)
if format == "html":
html_index(sink, crumbs, datarooturi, datauri, channel)
elif format == "turtle":
turtle_index(sink, datarooturi, datauri, channel)
# XXX more formats
def turtle_index(sink, root, datauri, querychannel):
triples = []
freenodeURI = root + "#freenode"
triples += [(freenodeURI, RDFS.label, PlainLiteral("Freenode"))]
channels = sorted(sink.channels.keys())
channelURIs = []
for channel in channels:
channelID = channel.strip("#").lower()
channelURI = root + channelID + "#channel"
channelURIs.append(channelURI)
oldChannelURI = "irc://freenode/%23" + channelID
triples += [None,
(freenodeURI, SIOC.space_of, channelURI),
(channelURI, OWL.sameAs, oldChannelURI),
(channelURI, RDF.type, SIOC.Forum),
(channelURI, RDF.type, SIOCT.ChatChannel),
(channelURI, RDFS.label,
PlainLiteral("#" + channel)),
]
for day in sorted(sink.channel2days[channel]):
logURI = "%s%s/%s" % (root, channelID, day)
triples += [(channelURI, RDFS.seeAlso, logURI)]
if querychannel:
nicks = sorted(sink.channel2nicks[querychannel].keys())
for nick in nicks:
userURI = root + "users/%s#user" % nick
triples += [None,
(channelURI, SIOC.has_subscriber, userURI),
(userURI, RDFS.label, PlainLiteral(nick)),
]
writer = TurtleWriter(None, namespaces)
if querychannel and channels:
title = "Index of #%s" % channels[0]
writer.write([("", FOAF.primaryTopic, channelURIs[0])])
elif querychannel:
title = "Empty index"
else:
title = "Index of some IRC discussion logs"
writer.write([("", FOAF.primaryTopic, freenodeURI)])
writer.write([("", FOAF.topic, channelURI)
for channelURI in channelURIs])
writer.write([("", RDFS.label, PlainLiteral(title))])
writer.setBase(root)
writer.write(triples)
writer.close()
def hash(**kwargs):
return kwargs
def create_index_crumbs(root, datauri, restype, channel, timeprefix):
yield hash(uri=root, label="Front page")
if datauri == root:
return
if not restype:
return
yield hash(uri="%s%s" % (root, restype), label=restype)
if not channel:
return
yield hash(uri="%s%s/%s" % (root, restype, channel), label=channel)
if not timeprefix:
return
yield hash(uri="%s%s/%s/%s" % (root, restype, channel, timeprefix),
label=timeprefix)
def html_index(sink, crumbs, root, datauri, querychannel):
context = new_context()
context.addGlobal('crumbs', crumbs)
context.addGlobal('datarooturi', root)
context.addGlobal('datauri', datauri)
context.addGlobal('querychannel', querychannel)
channels = sorted(sink.channels.keys())
if querychannel:
title = "Channel #%s" % channels[0]
else:
title = "Some IRC discussion logs"
context.addGlobal('title', title)
if datauri == root:
datauri2 = root + "index"
else:
datauri2 = datauri
context.addGlobal('datauri2', datauri2)
channeldata = []
for channel in channels:
channelID = channel.strip("#").lower()
channelURI = root + channelID + "#channel"
channeldata.append({'uri': channelURI, 'name': channelID})
# XXX list works around a bug in simpletal
days = list(reversed(sorted(sink.days.keys())))
context.addGlobal('channels', channeldata)
context.addGlobal('days', days)
context.addGlobal('day2channels', sink.day2channels)
if querychannel:
nicks = sorted(sink.channel2nicks[querychannel].keys())
else:
nicks = sorted(sink.nicks)
userdata = []
for nick in nicks:
userURI = root + "users/%s#user" % nick
userdata.append({'uri': userURI, 'name': nick})
context.addGlobal('users', userdata)
context.addGlobal('nick2people', get_nick2people())
template = get_template('index')
expand_template(template, context)
def sitemap_entry(uri, timestamp, frequency, priority):
print "<url>"
print " <loc>%s</loc>""" % uri
if timestamp:
print " <lastmod>%s</lastmod>" % timestamp
if frequency:
print " <changefreq>%s</changefreq>" % frequency
if priority:
print " <priority>%s</priority>" % priority
print "</url>"
def sitemap_index(sink, root):
now = convert_timestamp_to_z(w3c_timestamp())
today = now.split("T")[0]
print """
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
"""
# individual files:
sitemap_entry(root, max(sink.channel2latest.values()), "hourly", 1.0)
sitemap_entry(root+"about", None, "weekly", 1.0)
sitemap_entry(root+"users", None, "daily", 1.0)
print
# users:
nicks = sorted(sink.nicks.keys())
for nick in nicks:
sitemap_entry(root+"users/"+nick, sink.nick2latest[nick], "weekly", 0.9)
print
# channels:
channels = sorted(sink.channels.keys())
channelURIs = []
for channel in channels:
channelID = channel.strip("#").lower()
channelURI = root + channelID + "#channel"
sitemap_entry(channelURI, sink.channel2latest[channelID], "hourly", 0.9)
print
# today's logs:
for channel in channels:
channelID = channel.strip("#").lower()
logURI = "%s%s/%s" % (root, channelID, today)
latest = max([today, sink.channel2latest[channelID]])
sitemap_entry(logURI, latest, "hourly", 0.9)
print
# daily logs:
for channel in channels:
channelID = channel.strip("#").lower()
for day in sorted(sink.channel2days[channel]):
logURI = "%s%s/%s" % (root, channelID, day)
if day != today:
sitemap_entry(logURI, day, "never", 0.8)
print "</urlset>"
if __name__ == '__main__':
import sys, logging
simpleTALLogger = logging.getLogger("simpleTAL")
simpleTALLogger.setLevel(logging.DEBUG)
runcgi(sys.argv[1])