diff --git a/wikipedia.py b/wikipedia.py index 2ae1a8d20c..b261378c9d 100644 --- a/wikipedia.py +++ b/wikipedia.py @@ -1,182 +1,64 @@ """ wikipedia.py - Willie Wikipedia Module -Copyright 2008-9, Sean B. Palmer, inamidst.com +Copyright 2013 Edward Powell - embolalia.net Licensed under the Eiffel Forum License 2. http://willie.dftba.net """ - -import re, urllib, gzip, StringIO import willie.web as web - -wikiuri = 'http://%s.wikipedia.org/wiki/%s' -# wikisearch = 'http://%s.wikipedia.org/wiki/Special:Search?' \ -# + 'search=%s&fulltext=Search' - -r_tr = re.compile(r'(?ims)
]*>.*?
|') and - para.endswith('
')) - and not 'disambiguation)"' in para) - and not '(images and media)' in para - and not 'This article contains a' in para - and not 'id="coordinates"' in para - and not 'class="thumb' in para] - # and not 'style="display:none"' in para] - - for i, para in enumerate(paragraphs): - para = para.replace('', '|') - para = para.replace('', '|') - paragraphs[i] = text(para).strip() - - # Post-process - paragraphs = [para for para in paragraphs if - (para and not (para.endswith(':') and len(para) < 150))] - - para = text(paragraphs[0]) - m = r_sentence.match(para) - - if not m: - if not last: - term = search(term) - return wikipedia(term, language=language, last=True) - return None - sentence = m.group(0) - - maxlength = 275 - if len(sentence) > maxlength: - sentence = sentence[:maxlength] - words = sentence[:-5].split(' ') - words.pop() - sentence = ' '.join(words) + ' [...]' - - if (('using the Article Wizard if you wish' in sentence) - or ('or add a request for it' in sentence) - or ('in existing articles' in sentence)): - if not last: - term = search(term) - return wikipedia(term, language=language, last=True) - return None - - sentence = '"' + sentence.replace('"', "'") + '"' - sentence = sentence.decode('utf-8').encode('utf-8') - wikiuri = wikiuri.decode('utf-8').encode('utf-8') - term = term.decode('utf-8').encode('utf-8') - return sentence + ' - ' + (wikiuri % (language, term)) - -def wik(willie, trigger): - """Look up something on Wikipedia""" - origterm = trigger.groups()[1] - if not origterm: - return willie.say('Perhaps you meant ".wik Zen"?') - origterm = origterm.encode('utf-8') - - term = urllib.unquote(origterm) - language = 'en' - if term.startswith(':') and (' ' in term): - a, b = term.split(' ', 1) - a = a.lstrip(':') - if a.isalpha(): - language, term = a, b - term = term[0].upper() + term[1:] - term = term.replace(' ', '_') - - try: result = wikipedia(term, language) - except IOError: - args = (language, wikiuri % (language, term)) - error = "Can't connect to %s.wikipedia.org (%s)" % args - return willie.say(error) - - if result is not None: - willie.say(result) - else: willie.say('Can\'t find anything in Wikipedia for "%s".' % origterm) - -wik.commands = ['wiki', 'wik', 'w'] -wik.priority = 'high' - -if __name__ == '__main__': - print __doc__.strip() +import json +import re + +REDIRECT = re.compile(r'^REDIRECT (.*)') + +def mw_search(server, query, num): + """ + Searches the specified MediaWiki server for the given query, and returns + the specified number of results. + """ + search_url = ('http://%s/w/api.php?format=json&action=query' + '&list=search&srlimit=%d&srprop=timestamp&srwhat=nearmatch' + '&srsearch=') % (server, num) + search_url += web.quote(query.encode('utf-8')) + query = json.loads(web.get(search_url)) + query = query['query']['search'] + return [r['title'] for r in query] + + +def mw_snippet(server, query): + """ + Retrives a snippet of the specified length from the given page on the given + server. + """ + snippet_url = ('https://en.wikipedia.org/w/api.php?format=json' + '&action=query&prop=extracts&exintro&explaintext' + '&exchars=300&redirects&titles=') + snippet_url += web.quote(query.encode('utf-8')) + snippet = json.loads(web.get(snippet_url)) + snippet = snippet['query']['pages'] + + # For some reason, the API gives the page *number* as the key, so we just + # grab the first page number in the results. + snippet = snippet[snippet.keys()[0]] + + return snippet['extract'] + + +def wikipedia(willie, trigger): + query = trigger.group(2) + if not query: + willie.reply('What do you want me to look up?') + return willie.NOLIMIT + server = 'en.wikipedia.org' + query = mw_search(server, query, 1) + if not query: + willie.reply("I can't find any results for that.") + return willie.NOLIMIT + else: + query = query[0] + snippet = mw_snippet(server, query) + + query = query.replace(' ', '_') + willie.say('"%s" - http://en.wikipedia.org/wiki/%s' % (snippet, query)) +wikipedia.commands = ['w', 'wiki', 'wik'] +wikipedia.example = '.w San Francisco'