PageRenderTime 280ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/edsu-plugins/Etymology/plugin.py

https://github.com/lbjay/supybot-plugins
Python | 47 lines | 41 code | 6 blank | 0 comment | 3 complexity | 88f413ce70d55f13111bb71b3c862094 MD5 | raw file
  1. from urllib import urlencode
  2. from urllib2 import urlopen, HTTPError
  3. from supybot.commands import *
  4. import supybot.plugins as plugins
  5. import supybot.ircmsgs as ircmsgs
  6. import supybot.ircutils as ircutils
  7. import supybot.callbacks as callbacks
  8. from BeautifulSoup import BeautifulSoup
  9. def lookup(word):
  10. from socket import setdefaulttimeout
  11. setdefaulttimeout(60)
  12. url = "http://www.etymonline.com/index.php?%s" \
  13. % urlencode({'search':word, 'searchmode':'or'})
  14. doc = None
  15. try:
  16. doc = urlopen(url)
  17. except HTTPError, e:
  18. return 'http error %s for %s' % (e.code, url)
  19. soup = BeautifulSoup(doc)
  20. dd = soup.find('dd', 'highlight')
  21. if dd:
  22. etym = u''
  23. for part in dd:
  24. try:
  25. etym += part.string
  26. except:
  27. pass
  28. return etym.strip()
  29. else:
  30. return "%s not found" % word
  31. class Etymology(callbacks.Privmsg):
  32. def etym(self,irc,msg,args):
  33. """etym <word> lookup the etymology for a word/phrase
  34. """
  35. etymology = lookup(''.join(args))
  36. irc.reply(etymology.encode('utf-8'))
  37. Class = Etymology