#!/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
"""
import re, urllib
import web
import json
wikiapi = 'https://wiki.archlinux.org/api.php?action=query&list=search&srsearch=%s&limit=1&prop=snippet&format=json'
wikiuri = 'https://wiki.archlinux.org/index.php/%s'
wikisearch = 'https://wiki.archlinux.org/index.php/Special:Search?' \
+ 'search=%s&fulltext=Search'
r_tr = re.compile(r'(?ims)]*>.*?
')
r_content = re.compile(r'(?ims)
\n.*?')
r_paragraph = re.compile(r'(?ims)]*>.*?
|]*>.*?')
r_tag = re.compile(r'<(?!!)[^>]+>')
r_whitespace = re.compile(r'[\t\r\n ]+')
r_redirect = re.compile(
r'(?ims)class=.redirectText.>\s*')
s = s.replace('<', '<')
s = s.replace('&', '&')
s = s.replace(' ', ' ')
return s
def text(html):
html = r_tag.sub('', html)
html = r_whitespace.sub(' ', html)
return unescape(html).strip()
def archwiki(term, last=False):
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)
def awik(phenny, input):
origterm = input.groups()[1]
if not origterm:
return phenny.say('Perhaps you meant ".awik dwm"?')
origterm = origterm.encode('utf-8')
term = urllib.unquote(origterm)
term = term[0].upper() + term[1:]
term = term.replace(' ', '_')
try: result = archwiki(term)
except IOError:
error = "Can't connect to wiki.archlinux.org (%s)" % (wikiuri % term)
return phenny.say(error)
if result is not None:
phenny.say(result)
else: phenny.say('Can\'t find anything in the ArchWiki for "%s".' % origterm)
awik.commands = ['awik']
awik.priority = 'high'
if __name__ == '__main__':
print __doc__.strip()