From b3e97f25003883f079833d1f7fd2ca009f61444d Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Fri, 10 Feb 2012 15:04:05 -0600 Subject: [PATCH] make wolframalpha and urban use apis instead of scraping --- plugins/dictionary.py | 14 ++++++-------- plugins/pre.py | 2 +- plugins/wolframalpha.py | 37 +++++++++++++++++-------------------- 3 files changed, 24 insertions(+), 29 deletions(-) mode change 100644 => 100755 plugins/dictionary.py diff --git a/plugins/dictionary.py b/plugins/dictionary.py old mode 100644 new mode 100755 index 8c22175..951099c --- a/plugins/dictionary.py +++ b/plugins/dictionary.py @@ -8,16 +8,14 @@ from util import hook, http def urban(inp): '''.u/.urban -- looks up on urbandictionary.com''' - url = 'http://www.urbandictionary.com/define.php' - page = http.get_html(url, term=inp) - words = page.xpath("//td[@class='word']") - defs = page.xpath("//div[@class='definition']") + url = 'http://www.urbandictionary.com/iphone/search/define' + page = http.get_json(url, term=inp) + defs = page['list'] - if not defs: - return 'no definitions found' + if page['result_type'] == 'no_results': + return 'not found.' - out = words[0].text_content().strip() + ': ' + ' '.join( - defs[0].text_content().split()) + out = defs[0]['word'] + ': ' + defs[0]['definition'] if len(out) > 400: out = out[:out.rfind(' ', 0, 400)] + '...' diff --git a/plugins/pre.py b/plugins/pre.py index 45c0115..c41271e 100644 --- a/plugins/pre.py +++ b/plugins/pre.py @@ -28,5 +28,5 @@ def predb(inp): size = ' :: ' + size[0].split()[0] else: size = '' - + return '%s - %s - %s%s' % (date, section, name, size) diff --git a/plugins/wolframalpha.py b/plugins/wolframalpha.py index 4a6e444..ce2812b 100644 --- a/plugins/wolframalpha.py +++ b/plugins/wolframalpha.py @@ -5,34 +5,31 @@ from util import hook, http @hook.command('wa') @hook.command -def wolframalpha(inp): - ".wa/.wolframalpha -- scrapes Wolfram Alpha's" \ - " results for " +def wolframalpha(inp, bot=None): + ".wa/.wolframalpha -- computes using Wolfram Alpha" - url = "http://www.wolframalpha.com/input/?asynchronous=false" + api_key = bot.config.get("api_keys", {}).get("wolframalpha", None) + if api_key is None: + return "error: no api key set" - h = http.get_html(url, i=inp) + url = 'http://api.wolframalpha.com/v2/query?format=plaintext' - pods = h.xpath("//div[@class='pod ']") + result = http.get_xml(url, input=inp, appid=api_key) pod_texts = [] - for pod in pods: - heading = pod.find('h2') - if heading is not None: - heading = heading.text_content().strip() - if heading.startswith('Input'): - continue - else: + for pod in result.xpath("//pod"): + title = pod.attrib['title'] + if pod.attrib['id'] == 'Input': continue results = [] - for alt in pod.xpath('div/div[@class="output pnt"]/img/@alt'): - alt = alt.strip().replace('\\n', '; ') - alt = re.sub(r'\s+', ' ', alt) - if alt: - results.append(alt) + for subpod in pod.xpath('subpod/plaintext/text()'): + subpod = subpod.strip().replace('\\n', '; ') + subpod = re.sub(r'\s+', ' ', subpod) + if subpod: + results.append(subpod) if results: - pod_texts.append(heading + ' ' + '|'.join(results)) + pod_texts.append(title + ': ' + '|'.join(results)) ret = '. '.join(pod_texts) @@ -51,6 +48,6 @@ def wolframalpha(inp): ret = re.sub(r'\W+$', '', ret) + '...' if not ret: - return 'no result' + return 'no results' return ret