minetestbot-modules/wiki.py

74 lines
1.8 KiB
Python
Raw Normal View History

2013-01-07 10:00:44 -08:00
#!/usr/bin/env python
"""
2013-06-18 13:21:52 -07:00
wiki.py - Phenny Wiki Module
2014-07-20 07:13:59 -07:00
Copyright 2014, sfan5
2013-01-07 10:00:44 -08:00
"""
2014-07-20 07:13:59 -07:00
import re
2013-01-07 10:00:44 -08:00
import web
2014-07-20 07:13:59 -07:00
import urllib.parse
2014-07-20 11:46:13 -07:00
wikiuri_g = 'http://wiki.minetest.net/index.php?title=%s&printable=yes'
2014-07-20 07:13:59 -07:00
wikiuri_r = 'http://wiki.minetest.net/%s'
r_content = re.compile(r'(?i)<div[^>]+class=.mw-content-ltr.>')
r_paragraph = re.compile(r'(?ims)<p>(.+?)</p>')
r_sentenceend = re.compile(r'\.[^\.]')
transforms = [
2014-07-20 11:46:13 -07:00
(re.compile(r'(?i)<a [^>]+>(.+?)</a>'), "\g<1>"),
(re.compile(r'(?i)<b>(.+?)</b>'), "\x02\g<1>\x02"),
(re.compile(r'(?i)<i>(.+?)</i>'), "\x1d\g<1>\x1d"),
(re.compile(r'(?i)<u>(.+?)</u>'), "\x1f\g<1>\x1f"),
(re.compile(r'(?i)<code>(.+?)</code>'), "\x0315\g<1>\x03 "),
(re.compile(r'(?i)<br\s*/?>'), ""),
]
nottext = [
re.compile(r'(?i)^<br\s*/?>$')
2014-07-20 07:13:59 -07:00
]
def wiki(phenny, input):
term = input.group(2)
if not term:
return
log.log("event", "%s queried Wiki for '%s'" % (log.fmt_user(input), term), phenny)
2014-07-20 11:46:13 -07:00
term = term.replace(" ", "_")
2014-07-20 07:13:59 -07:00
term = web.urlencode(term)
data, scode = web.get(wikiuri_g % term)
if scode == 404:
return phenny.say("No such page.")
data = str(data, "utf-8")
m = re.search(r_content, data)
if not m:
return phenny.say("Sorry, did not find anything.")
data = data[m.span()[1]:]
2014-07-20 11:46:13 -07:00
mi = re.finditer(r_paragraph, data)
text = ""
for m in mi:
abort = False
for e in nottext:
if re.search(e, m.group(1)):
abort = True
break
if abort:
continue
text = m.group(1)
break
if not text:
2014-07-20 07:13:59 -07:00
return phenny.say("Sorry, did not find anything.")
2014-07-20 11:46:13 -07:00
for tf in transforms:
text = re.sub(tf[0], tf[1], text)
m = re.search(r_sentenceend, text)
2014-07-20 07:13:59 -07:00
if m:
2014-07-20 11:46:13 -07:00
text = text[:m.span()[1]-1]
phenny.say('"%s" - %s ' % (web.decode(text), wikiuri_r % term))
2014-07-20 07:13:59 -07:00
wiki.commands = ['wik', 'wiki']
wiki.priority = 'high'
2013-01-07 10:00:44 -08:00
if __name__ == '__main__':
2014-07-20 07:13:59 -07:00
print(__doc__.strip())