45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
wikipedia.py - Phenny Wikipedia Module
|
|
Copyright 2008-9, Sean B. Palmer, inamidst.com
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
http://inamidst.com/phenny/
|
|
"""
|
|
|
|
import re, urllib.request, urllib.parse, urllib.error, gzip, io
|
|
import wiki
|
|
|
|
wikiapi = 'http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={0}&limit=1&prop=snippet&format=json'
|
|
wikiuri = 'http://en.wikipedia.org/wiki/{0}'
|
|
wikisearch = 'http://en.wikipedia.org/wiki/Special:Search?' \
|
|
+ 'search={0}&fulltext=Search'
|
|
|
|
def wik(phenny, input):
|
|
origterm = input.groups()[1]
|
|
if not origterm:
|
|
return phenny.say('Perhaps you meant ".wik Zen"?')
|
|
|
|
term = urllib.parse.unquote(origterm)
|
|
term = term[0].upper() + term[1:]
|
|
term = term.replace(' ', '_')
|
|
|
|
w = wiki.Wiki(wikiapi, wikiuri, wikisearch)
|
|
|
|
try:
|
|
result = w.search(term)
|
|
except IOError:
|
|
error = "Can't connect to en.wikipedia.org ({0})".format(wikiuri.format(term))
|
|
return phenny.say(error)
|
|
|
|
if result is not None:
|
|
phenny.say(result)
|
|
else:
|
|
phenny.say('Can\'t find anything in Wikipedia for "{0}".'.format(origterm))
|
|
|
|
wik.commands = ['wik']
|
|
wik.priority = 'high'
|
|
|
|
if __name__ == '__main__':
|
|
print(__doc__.strip())
|