2008-02-21 07:06:33 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
wikipedia.py - Phenny Wikipedia Module
|
2009-06-07 05:08:49 -04:00
|
|
|
Copyright 2008-9, Sean B. Palmer, inamidst.com
|
2008-02-21 07:06:33 -05:00
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
2011-09-22 14:17:27 -04:00
|
|
|
import re, urllib.request, urllib.parse, urllib.error, gzip, io
|
2012-06-14 00:58:31 -04:00
|
|
|
import wiki
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2012-06-15 23:04:29 -04:00
|
|
|
wikiapi = 'https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={0}&limit=1&prop=snippet&format=json'
|
|
|
|
wikiuri = 'https://en.wikipedia.org/wiki/{0}'
|
|
|
|
wikisearch = 'https://en.wikipedia.org/wiki/Special:Search?' \
|
2012-06-14 00:58:31 -04:00
|
|
|
+ 'search={0}&fulltext=Search'
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
def wik(phenny, input):
|
2011-07-15 13:43:27 -04:00
|
|
|
origterm = input.groups()[1]
|
|
|
|
if not origterm:
|
|
|
|
return phenny.say('Perhaps you meant ".wik Zen"?')
|
2008-02-23 07:16:43 -05:00
|
|
|
|
2011-09-22 14:17:27 -04:00
|
|
|
term = urllib.parse.unquote(origterm)
|
2011-07-15 13:43:27 -04:00
|
|
|
term = term[0].upper() + term[1:]
|
|
|
|
term = term.replace(' ', '_')
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2012-06-14 00:58:31 -04:00
|
|
|
w = wiki.Wiki(wikiapi, wikiuri, wikisearch)
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = w.search(term)
|
2011-07-15 13:43:27 -04:00
|
|
|
except IOError:
|
2012-06-14 00:58:31 -04:00
|
|
|
error = "Can't connect to en.wikipedia.org ({0})".format(wikiuri.format(term))
|
2011-07-15 13:43:27 -04:00
|
|
|
return phenny.say(error)
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2011-07-15 13:43:27 -04:00
|
|
|
if result is not None:
|
|
|
|
phenny.say(result)
|
2012-06-14 00:58:31 -04:00
|
|
|
else:
|
|
|
|
phenny.say('Can\'t find anything in Wikipedia for "{0}".'.format(origterm))
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
wik.commands = ['wik']
|
|
|
|
wik.priority = 'high'
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2011-09-22 14:17:27 -04:00
|
|
|
print(__doc__.strip())
|