YTH.post_init()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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