make wolframalpha and urban use apis instead of scraping

This commit is contained in:
Ryan Hitchman 2012-02-10 15:04:05 -06:00
parent a3b295b0c0
commit b3e97f2500
3 changed files with 24 additions and 29 deletions

14
plugins/dictionary.py Normal file → Executable file
View File

@ -8,16 +8,14 @@ from util import hook, http
def urban(inp): def urban(inp):
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com''' '''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
url = 'http://www.urbandictionary.com/define.php' url = 'http://www.urbandictionary.com/iphone/search/define'
page = http.get_html(url, term=inp) page = http.get_json(url, term=inp)
words = page.xpath("//td[@class='word']") defs = page['list']
defs = page.xpath("//div[@class='definition']")
if not defs: if page['result_type'] == 'no_results':
return 'no definitions found' return 'not found.'
out = words[0].text_content().strip() + ': ' + ' '.join( out = defs[0]['word'] + ': ' + defs[0]['definition']
defs[0].text_content().split())
if len(out) > 400: if len(out) > 400:
out = out[:out.rfind(' ', 0, 400)] + '...' out = out[:out.rfind(' ', 0, 400)] + '...'

View File

@ -28,5 +28,5 @@ def predb(inp):
size = ' :: ' + size[0].split()[0] size = ' :: ' + size[0].split()[0]
else: else:
size = '' size = ''
return '%s - %s - %s%s' % (date, section, name, size) return '%s - %s - %s%s' % (date, section, name, size)

View File

@ -5,34 +5,31 @@ from util import hook, http
@hook.command('wa') @hook.command('wa')
@hook.command @hook.command
def wolframalpha(inp): def wolframalpha(inp, bot=None):
".wa/.wolframalpha <query> -- scrapes Wolfram Alpha's" \ ".wa/.wolframalpha <query> -- computes <query> using Wolfram Alpha"
" results for <query>"
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 = [] pod_texts = []
for pod in pods: for pod in result.xpath("//pod"):
heading = pod.find('h2') title = pod.attrib['title']
if heading is not None: if pod.attrib['id'] == 'Input':
heading = heading.text_content().strip()
if heading.startswith('Input'):
continue
else:
continue continue
results = [] results = []
for alt in pod.xpath('div/div[@class="output pnt"]/img/@alt'): for subpod in pod.xpath('subpod/plaintext/text()'):
alt = alt.strip().replace('\\n', '; ') subpod = subpod.strip().replace('\\n', '; ')
alt = re.sub(r'\s+', ' ', alt) subpod = re.sub(r'\s+', ' ', subpod)
if alt: if subpod:
results.append(alt) results.append(subpod)
if results: if results:
pod_texts.append(heading + ' ' + '|'.join(results)) pod_texts.append(title + ': ' + '|'.join(results))
ret = '. '.join(pod_texts) ret = '. '.join(pod_texts)
@ -51,6 +48,6 @@ def wolframalpha(inp):
ret = re.sub(r'\W+$', '', ret) + '...' ret = re.sub(r'\W+$', '', ret) + '...'
if not ret: if not ret:
return 'no result' return 'no results'
return ret return ret