#!/usr/bin/env python """ wikipedia.py - Phenny Wikipedia Module Copyright 2008, Sean B. Palmer, inamidst.com Licensed under the Eiffel Forum License 2. http://inamidst.com/phenny/ """ import re, urllib import web wikiuri = 'http://en.wikipedia.org/wiki/%s' wikisearch = 'http://en.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, 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 ((sentence == 'Wikipedia does not have an article with this exact name.') or (sentence == 'Wikipedia does not have a page with this exact name.')): if not last: term = search(term) return wikipedia(term, 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 % term) def wik(phenny, input): origterm = input.groups()[1] if not origterm: return phenny.say('Perhaps you meant ".wik Zen"?') origterm = origterm.encode('utf-8') term = urllib.unquote(origterm) term = term[0].upper() + term[1:] term = term.replace(' ', '_') try: result = wikipedia(term) except IOError: error = "Can't connect to en.wikipedia.org (%s)" % (wikiuri % term) return phenny.say(error) if result is not None: phenny.say(result) else: phenny.say('Can\'t find anything in Wikipedia for "%s".' % origterm) wik.commands = ['wik'] wik.priority = 'high' if __name__ == '__main__': print __doc__.strip()