phenny/modules/wikipedia.py

44 lines
1.1 KiB
Python
Raw Permalink Normal View History

#!/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
Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import wiki
2018-03-16 09:27:18 -04:00
endpoints = {
'api': 'https://en.wikipedia.org/w/api.php?format=json&action=query&list=search&srsearch={0}&prop=snippet&limit=1',
'url': 'https://en.wikipedia.org/wiki/{0}',
'search': 'https://en.wikipedia.org/wiki/Special:Search?search={0}&fulltext=Search',
}
def wik(phenny, input):
2013-10-05 19:20:37 -04:00
""".wik <term> - Look up something on Wikipedia."""
2011-07-15 13:43:27 -04:00
origterm = input.groups()[1]
if not origterm:
return phenny.say('Perhaps you meant ".wik Zen"?')
2018-03-16 09:27:18 -04:00
origterm = origterm.strip()
term, section = wiki.parse_term(origterm)
2018-03-16 09:27:18 -04:00
w = wiki.Wiki(endpoints)
match = w.search(term)
2018-03-16 09:27:18 -04:00
if not match:
phenny.say('Can\'t find anything in Wikipedia for "{0}".'.format(origterm))
2018-03-16 09:27:18 -04:00
return
snippet, url = wiki.extract_snippet(match, section)
phenny.say('"{0}" - {1}'.format(snippet, url))
wik.commands = ['wik']
wik.priority = 'high'
if __name__ == '__main__':
2011-09-22 14:17:27 -04:00
print(__doc__.strip())