yth.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. try:
  2. from modules.basemodule import BaseModule
  3. except ImportError:
  4. from basemodule import BaseModule
  5. from event import Event
  6. import difflib
  7. class YTH(BaseModule):
  8. def post_init(self):
  9. command = Event("__.yth__")
  10. command.define(msg_definition="^\.yth")
  11. command.subscribe(self)
  12. self.bot.register_event(command, self)
  13. self.help = ".yth, .yth <search terms>"
  14. self.comparer = difflib.SequenceMatcher()
  15. def handle(self, event):
  16. if len(event.msg.split()) == 1:
  17. count = 0
  18. msg = list()
  19. while count < 5 and count < len(self.bot.mem_store['youtube']):
  20. for entry, url in reversed(self.bot.mem_store['youtube'].items()):
  21. msg.append(entry + " - " + url)
  22. count += 1
  23. self.say(event.user, ", ".join(msg))
  24. elif len(event.msg.split()) > 1: # we're searching for specific terms
  25. terms = event.msg.split()[1:-1] # terms from 1 (ignore .yth) to the end
  26. msg = list()
  27. for k, v in self.bot.mem_store['youtube'].items():
  28. self.comparer.set_seq1(k.lower())
  29. self.comparer.set_seq2(" ".join(terms))
  30. if self.comparer.ratio() >= .75:
  31. msg.append(k + " - " + v)
  32. self.say(event.user, ", ".join(msg))