.dict/.define (courtesy GhettoWizard), move more stuff into the http library
This commit is contained in:
parent
f61895fdc3
commit
1005537e63
|
@ -0,0 +1,91 @@
|
|||
import re
|
||||
|
||||
from util import hook, http
|
||||
|
||||
|
||||
@hook.command('u')
|
||||
@hook.command
|
||||
def urban(inp):
|
||||
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
|
||||
if not inp:
|
||||
return urban.__doc__
|
||||
|
||||
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']")
|
||||
|
||||
if not defs:
|
||||
return 'no definitions found'
|
||||
|
||||
out = words[0].text_content().strip() + ': ' + ' '.join(
|
||||
defs[0].text_content().split())
|
||||
|
||||
if len(out) > 400:
|
||||
out = out[:out.rfind(' ', 0, 400)] + '...'
|
||||
|
||||
return out
|
||||
|
||||
|
||||
# define plugin by GhettoWizard & Scaevolus
|
||||
@hook.command('dict')
|
||||
@hook.command
|
||||
def define(inp):
|
||||
".define/.dict <word> -- fetches definition of <word>"
|
||||
|
||||
if not inp:
|
||||
return define.__doc__
|
||||
|
||||
url = 'http://ninjawords.com/'
|
||||
|
||||
h = http.get_html(url + http.quote_plus(inp))
|
||||
|
||||
definition = h.xpath('//dd[@class="article"] | '
|
||||
'//div[@class="definition"] |'
|
||||
'//div[@class="example"]')
|
||||
|
||||
if not definition:
|
||||
return 'No results for ' + inp
|
||||
|
||||
def format_output(show_examples):
|
||||
result = ''
|
||||
|
||||
correction = h.xpath('//span[@class="correct-word"]')
|
||||
if correction:
|
||||
result = 'definition for "%s": ' % correction[0].text
|
||||
|
||||
sections = []
|
||||
for section in definition:
|
||||
if section.attrib['class'] == 'article':
|
||||
sections += [[section.text_content() + ': ']]
|
||||
elif section.attrib['class'] == 'example':
|
||||
if show_examples:
|
||||
sections[-1][-1] += ' ' + section.text_content()
|
||||
else:
|
||||
sections[-1] += [section.text_content()]
|
||||
|
||||
for article in sections:
|
||||
result += article[0]
|
||||
if len(article) > 2:
|
||||
result += ' '.join('%d. %s' % (n + 1, section)
|
||||
for n, section in enumerate(article[1:]))
|
||||
else:
|
||||
result += article[1] + ' '
|
||||
|
||||
synonyms = h.xpath('//dd[@class="synonyms"]')
|
||||
if synonyms:
|
||||
result += synonyms[0].text_content()
|
||||
|
||||
result = re.sub(r'\s+', ' ', result)
|
||||
result = re.sub('\xb0', '', result)
|
||||
return result
|
||||
|
||||
result = format_output(True)
|
||||
if len(result) > 450:
|
||||
result = format_output(False)
|
||||
|
||||
if len(result) > 450:
|
||||
result = result[:result.rfind(' ', 0, 450)]
|
||||
result = re.sub(r'[^A-Za-z]+\.?$', '', result) + ' ...'
|
||||
|
||||
return result
|
|
@ -1,7 +1,5 @@
|
|||
import random
|
||||
|
||||
from lxml import html
|
||||
|
||||
from util import hook, http
|
||||
|
||||
|
||||
|
@ -43,7 +41,7 @@ def google(inp):
|
|||
|
||||
result = parsed['responseData']['results'][0]
|
||||
|
||||
title, content = map(lambda x: html.fromstring(x).text_content(),
|
||||
title, content = map(lambda x: http.html.fromstring(x).text_content(),
|
||||
(result['titleNoFormatting'], result['content']))
|
||||
|
||||
out = '%s -- \x02%s\x02: "%s"' % (result['unescapedUrl'], title, content)
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import re
|
||||
|
||||
from lxml import html
|
||||
|
||||
from util import hook, http
|
||||
|
||||
|
||||
|
@ -23,9 +21,9 @@ def mtg(inp):
|
|||
type = card.find('td/p').text.replace('\n', '')
|
||||
|
||||
# this is ugly
|
||||
text = html.tostring(card.xpath("//p[@class='ctext']/b")[0])
|
||||
text = http.html.tostring(card.xpath("//p[@class='ctext']/b")[0])
|
||||
text = text.replace('<br>', '$')
|
||||
text = html.fromstring(text).text_content()
|
||||
text = http.html.fromstring(text).text_content()
|
||||
text = re.sub(r'(\w+\s*)\$+(\s*\w+)', r'\1. \2', text)
|
||||
text = text.replace('$', ' ')
|
||||
text = re.sub(r'\(.*?\)', '', text) # strip parenthetical explanations
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
import re
|
||||
|
||||
from util import hook, http
|
||||
|
||||
|
||||
@hook.command('u')
|
||||
@hook.command
|
||||
def urban(inp):
|
||||
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
|
||||
if not inp:
|
||||
return urban.__doc__
|
||||
|
||||
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']")
|
||||
|
||||
if not defs:
|
||||
return 'no definitions found'
|
||||
|
||||
out = words[0].text_content().strip() + ': ' + ' '.join(
|
||||
defs[0].text_content().split())
|
||||
|
||||
if len(out) > 400:
|
||||
out = out[:out.rfind(' ', 0, 400)] + '...'
|
||||
|
||||
return out
|
|
@ -8,7 +8,7 @@ import urlparse
|
|||
from urllib import quote, quote_plus
|
||||
from urllib2 import HTTPError, URLError
|
||||
|
||||
import lxml
|
||||
from lxml import etree, html
|
||||
|
||||
user_agent = 'Skybot/1.0 http://bitbucket.org/Scaevolus/skybot'
|
||||
|
||||
|
@ -22,11 +22,11 @@ def get(*args, **kwargs):
|
|||
|
||||
|
||||
def get_html(*args, **kwargs):
|
||||
return lxml.html.fromstring(get(*args, **kwargs))
|
||||
return html.fromstring(get(*args, **kwargs))
|
||||
|
||||
|
||||
def get_xml(*args, **kwargs):
|
||||
return lxml.etree.fromstring(get(*args, **kwargs))
|
||||
return etree.fromstring(get(*args, **kwargs))
|
||||
|
||||
|
||||
def get_json(*args, **kwargs):
|
||||
|
|
|
@ -52,9 +52,3 @@ def wiki(inp):
|
|||
desc = desc[:300] + '...'
|
||||
|
||||
return '%s -- %s' % (desc, http.quote(url, ':/'))
|
||||
|
||||
|
||||
@hook.command
|
||||
def dict(inp):
|
||||
".dict/.define <word> -- gets definition of <word> from Wiktionary"
|
||||
pass
|
||||
|
|
Loading…
Reference in New Issue