From 13ca93a4e5312e61f5708ee1d541dcd5b2ba865e Mon Sep 17 00:00:00 2001
From: mutantmonkey ]*>.*? ')
r_content = 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 'There is currently no text in this page.' 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 archwiki(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 (('using the Article Wizard if you wish' in sentence) - or ('or add a request for it' in sentence)): - if not last: - term = search(term) - return archwiki(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) + global wikiapi, wikiuri + url = wikiapi % term + bytes = web.get(url) + result = json.loads(bytes) + result = result['query']['search'] + if len(result) <= 0: + return None + term = result[0]['title'] + term = term.replace(' ', '_') + snippet = text(result[0]['snippet']) + return "%s - %s" % (snippet, wikiuri % term) def awik(phenny, input): - origterm = input.groups()[1] - if not origterm: - return phenny.say('Perhaps you meant ".awik dwm"?') - origterm = origterm.encode('utf-8') + origterm = input.groups()[1] + if not origterm: + return phenny.say('Perhaps you meant ".awik dwm"?') + origterm = origterm.encode('utf-8') - term = urllib.unquote(origterm) - term = term[0].upper() + term[1:] - term = term.replace(' ', '_') + term = urllib.unquote(origterm) + term = term[0].upper() + term[1:] + term = term.replace(' ', '_') - try: result = archwiki(term) - except IOError: - error = "Can't connect to wiki.archlinux.org (%s)" % (wikiuri % term) - return phenny.say(error) + try: result = archwiki(term) + except IOError: + error = "Can't connect to wiki.archlinux.org (%s)" % (wikiuri % term) + return phenny.say(error) - if result is not None: - phenny.say(result) - else: phenny.say('Can\'t find anything in the ArchWiki for "%s".' % origterm) + if result is not None: + phenny.say(result) + else: phenny.say('Can\'t find anything in the ArchWiki for "%s".' % origterm) awik.commands = ['awik'] awik.priority = 'high' if __name__ == '__main__': - print __doc__.strip() + print __doc__.strip() diff --git a/modules/vtluugwiki.py b/modules/vtluugwiki.py index f4cac2c..f739d2b 100755 --- a/modules/vtluugwiki.py +++ b/modules/vtluugwiki.py @@ -12,158 +12,75 @@ author: mutantmonkey]*>.*?
|') 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 'There is currently no text in this page.' 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 vtluugwiki(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 (('using the Article Wizard if you wish' in sentence) - or ('or add a request for it' in sentence)): - if not last: - term = search(term) - return vtluugwiki(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) + global wikiapi, wikiuri + url = wikiapi % term + bytes = web.get(url) + result = json.loads(bytes) + result = result['query']['search'] + if len(result) <= 0: + return None + term = result[0]['title'] + term = term.replace(' ', '_') + snippet = text(result[0]['snippet']) + return "%s - %s" % (snippet, wikiuri % term) def vtluug(phenny, input): - origterm = input.groups()[1] - if not origterm: - return phenny.say('Perhaps you meant ".vtluug Zen"?') - origterm = origterm.encode('utf-8') + origterm = input.groups()[1] + if not origterm: + return phenny.say('Perhaps you meant ".vtluug Zen"?') + origterm = origterm.encode('utf-8') - term = urllib.unquote(origterm) - term = term[0].upper() + term[1:] - term = term.replace(' ', '_') + term = urllib.unquote(origterm) + term = term[0].upper() + term[1:] + term = term.replace(' ', '_') - try: result = vtluugwiki(term) - except IOError: - error = "Can't connect to vtluug.org (%s)" % (wikiuri % term) - return phenny.say(error) + try: result = vtluugwiki(term) + except IOError: + error = "Can't connect to vtluug.org (%s)" % (wikiuri % term) + return phenny.say(error) - if result is not None: - phenny.say(result) - else: phenny.say('Can\'t find anything in the VTLUUG Wiki for "%s".' % origterm) + if result is not None: + phenny.say(result) + else: phenny.say('Can\'t find anything in the VTLUUG Wiki for "%s".' % origterm) vtluug.commands = ['vtluug'] vtluug.priority = 'high' if __name__ == '__main__': - print __doc__.strip() + print __doc__.strip() diff --git a/modules/wikipedia.py b/modules/wikipedia.py index b476ba3..510707a 100755 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -9,165 +9,75 @@ http://inamidst.com/phenny/ import re, urllib import web +import json -wikiuri = 'http://%s.wikipedia.org/wiki/%s' -# wikisearch = 'http://%s.wikipedia.org/wiki/Special:Search?' \ -# + 'search=%s&fulltext=Search' +wikiapi = 'http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=%s&limit=1&prop=snippet&format=json' +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, 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 wikipedia(term, last=False): + global wikiapi, wikiuri + url = wikiapi % term + bytes = web.get(url) + result = json.loads(bytes) + result = result['query']['search'] + if len(result) <= 0: + return None + term = result[0]['title'] + term = term.replace(' ', '_') + snippet = text(result[0]['snippet']) + return "%s - %s" % (snippet, 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') + origterm = input.groups()[1] + if not origterm: + return phenny.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(' ', '_') + term = urllib.unquote(origterm) + 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 phenny.say(error) + 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) + 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() + print __doc__.strip()