2011-02-14 18:38:12 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
archwiki.py - Phenny ArchWiki Module
|
|
|
|
Copyright 2008-9, Sean B. Palmer, inamidst.com
|
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
|
|
|
|
modified from Wikipedia module
|
|
|
|
author: mutantmonkey <mutantmonkey@gmail.com>
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re, urllib
|
|
|
|
import web
|
2011-07-15 13:43:27 -04:00
|
|
|
import json
|
2011-02-14 18:38:12 -05:00
|
|
|
|
2011-07-15 13:43:27 -04:00
|
|
|
wikiapi = 'https://wiki.archlinux.org/api.php?action=query&list=search&srsearch=%s&limit=1&prop=snippet&format=json'
|
2011-02-14 18:38:12 -05:00
|
|
|
wikiuri = 'https://wiki.archlinux.org/index.php/%s'
|
|
|
|
wikisearch = 'https://wiki.archlinux.org/index.php/Special:Search?' \
|
2011-07-15 13:43:27 -04:00
|
|
|
+ 'search=%s&fulltext=Search'
|
2011-02-14 18:38:12 -05:00
|
|
|
|
|
|
|
r_tr = re.compile(r'(?ims)<tr[^>]*>.*?</tr>')
|
2011-02-14 18:51:13 -05:00
|
|
|
r_content = re.compile(r'(?ims)</p>\n</div>.*?<!-- end content -->')
|
2011-02-14 18:38:12 -05:00
|
|
|
r_paragraph = re.compile(r'(?ims)<p[^>]*>.*?</p>|<li(?!n)[^>]*>.*?</li>')
|
|
|
|
r_tag = re.compile(r'<(?!!)[^>]+>')
|
|
|
|
r_whitespace = re.compile(r'[\t\r\n ]+')
|
|
|
|
r_redirect = re.compile(
|
2011-07-15 13:43:27 -04:00
|
|
|
r'(?ims)class=.redirectText.>\s*<a\s*href=./wiki/([^"/]+)'
|
2011-02-14 18:38:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
abbrs = ['etc', 'ca', 'cf', 'Co', 'Ltd', 'Inc', 'Mt', 'Mr', 'Mrs',
|
2011-07-15 13:43:27 -04:00
|
|
|
'Dr', 'Ms', 'Rev', 'Fr', 'St', 'Sgt', 'pron', 'approx', 'lit',
|
|
|
|
'syn', 'transl', 'sess', 'fl', 'Op'] \
|
|
|
|
+ list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') \
|
|
|
|
+ list('abcdefghijklmnopqrstuvwxyz')
|
2011-02-14 18:38:12 -05:00
|
|
|
t_sentence = r'^.{5,}?(?<!\b%s)(?:\.(?=[\[ ][A-Z0-9]|\Z)|\Z)'
|
|
|
|
r_sentence = re.compile(t_sentence % r')(?<!\b'.join(abbrs))
|
|
|
|
|
|
|
|
def unescape(s):
|
2011-07-15 13:43:27 -04:00
|
|
|
s = s.replace('>', '>')
|
|
|
|
s = s.replace('<', '<')
|
|
|
|
s = s.replace('&', '&')
|
|
|
|
s = s.replace(' ', ' ')
|
|
|
|
return s
|
2011-02-14 18:38:12 -05:00
|
|
|
|
|
|
|
def text(html):
|
2011-07-15 13:43:27 -04:00
|
|
|
html = r_tag.sub('', html)
|
|
|
|
html = r_whitespace.sub(' ', html)
|
|
|
|
return unescape(html).strip()
|
2011-02-14 18:38:12 -05:00
|
|
|
|
|
|
|
def archwiki(term, last=False):
|
2011-07-15 13:43:27 -04:00
|
|
|
global wikiapi, wikiuri
|
|
|
|
url = wikiapi % term
|
|
|
|
bytes = web.get(url)
|
|
|
|
result = json.loads(bytes)
|
|
|
|
result = result['query']['search']
|
|
|
|
if len(result) <= 0:
|
|
|
|
return None
|
|
|
|
term = result[0]['title']
|
|
|
|
term = term.replace(' ', '_')
|
|
|
|
snippet = text(result[0]['snippet'])
|
|
|
|
return "%s - %s" % (snippet, wikiuri % term)
|
2011-02-14 18:38:12 -05:00
|
|
|
|
|
|
|
def awik(phenny, input):
|
2011-07-15 13:43:27 -04:00
|
|
|
origterm = input.groups()[1]
|
|
|
|
if not origterm:
|
|
|
|
return phenny.say('Perhaps you meant ".awik dwm"?')
|
|
|
|
origterm = origterm.encode('utf-8')
|
2011-02-14 18:38:12 -05:00
|
|
|
|
2011-07-15 13:43:27 -04:00
|
|
|
term = urllib.unquote(origterm)
|
|
|
|
term = term[0].upper() + term[1:]
|
|
|
|
term = term.replace(' ', '_')
|
2011-02-14 18:38:12 -05:00
|
|
|
|
2011-07-15 13:43:27 -04:00
|
|
|
try: result = archwiki(term)
|
|
|
|
except IOError:
|
|
|
|
error = "Can't connect to wiki.archlinux.org (%s)" % (wikiuri % term)
|
|
|
|
return phenny.say(error)
|
2011-02-14 18:38:12 -05:00
|
|
|
|
2011-07-15 13:43:27 -04:00
|
|
|
if result is not None:
|
|
|
|
phenny.say(result)
|
|
|
|
else: phenny.say('Can\'t find anything in the ArchWiki for "%s".' % origterm)
|
2011-02-14 18:38:12 -05:00
|
|
|
|
|
|
|
awik.commands = ['awik']
|
|
|
|
awik.priority = 'high'
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2011-07-15 13:43:27 -04:00
|
|
|
print __doc__.strip()
|