Esempio n. 1
0
File: chat.py Progetto: pinard/Cabot
 def handler(self, write, *arguments):
     if arguments and arguments[0]:
         format = common.choice(
             'Doh.  %s',
             '... %s',
             )
         write(format % arguments[0] + '\n')
     else:
         write(common.choice('... huh?'))
Esempio n. 2
0
File: chat.py Progetto: pinard/Cabot
 def handler(self, write, *arguments):
     write(common.choice(
         "Okay, see you later",
         "later",
         "Bye then",
         "Take care now",
         "Happy hacking",
         ))
Esempio n. 3
0
def main():
    api = API()
    api.persistent_auth()

    c = Common(api)
    quit = False
    while not quit:
        query = raw_input("Recherche (q: arreter, s: mode saison) : ")
        if query == 's':
            c.download_season(raw_input("Saisissez la saison : "))
        elif query ==  'q':
            print 'Bye'
            quit = True
        else:
            torrents = c.search(query)
            chosen = choice(most_seeded(torrents, num=10))
            c.download(chosen)
Esempio n. 4
0
 def process_message(self, connection, event, private):
     line = event.arguments()[0]
     # Try some detectable encodings.  If none detected, assume Latin-1.
     for encoding in 'ASCII', 'UTF-8':
         try:
             line = line.decode(encoding)
         except UnicodeDecodeError:
             pass
         else:
             break
     else:
         if re.search('[\x80-\x9f]', line):
             # The truth is that we have no idea of the real encoding.
             # Manage to output `\uNNNN' everywhere outside ASCII.
             encoding = 'ASCII'
         else:
             encoding = 'ISO-8859-1'
         line = line.decode('ISO-8859-1')
     # Set SOURCE to the requesting nick.  Set TARGET to either a nick or
     # channel, where the command output should go.
     self.source = irclib.nm_to_n(event.source())
     if private:
         writer = connection.notice
         self.target = self.source
         code = '/'
     else:
         writer = connection.privmsg
         self.target = event.target()
         code, line = find_command(connection, line)
         if code is None:
             if self.bluemoon == self.bluemoon_interval:
                 code = ':'
                 self.bluemoon = random.randrange(self.bluemoon_interval)
             else:
                 self.bluemoon += 1
     # CODE is now the command type (`/' private, `:` naming the bot,
     # `,' using comma or double comma, `?' question mark after word,
     # or None) and LINE is the extracted command as a Unicode string.
     if code and line.rstrip():
         # Dispatch and produce output lines.
         fragments = []
         try:
             try:
                 fields = line.split(None, 1)
                 keyword = fields[0]
                 if len(fields) > 1:
                     rest = fields[1]
                 else:
                     rest = ''
                 command = common.Command.registry.get(keyword)
                 if command is not None:
                     arguments = command.split_arguments(rest)
                     command.check_number_arguments(arguments)
                     command.handler(fragments.append, *arguments)
                 if not fragments:
                     for matcher in common.Command.matchers:
                         matcher(fragments.append, line)
                         if fragments:
                             break
             except common.Error:
                 raise
             except:
                 import traceback
                 # FIXME: Should send tracebacks to the Cabot log.
                 traceback.print_exc(file=sys.stderr)
             if not fragments and code != '?':
                 choice = random.randrange(0, common.Command.burps[-1][0])
                 for weight, handler in common.Command.burps:
                     if choice < weight:
                         handler(fragments.append, line)
                     if fragments:
                         break
         except common.Error, exception:
             common.Command.error_handler(fragments.append, str(exception))
         # Schedule a reply, then log both the request and the reply.
         text = ''.join(fragments)
         if code == '?':
             text = common.choice(5, text, self.source + ': ' + text)
         # Clean spurious white space.
         lines = [line.rstrip() for line in text.splitlines()]
         while lines and not lines[-1]:
             del lines[-1]
         while lines and not lines[0]:
             del lines[0]
         if lines:
             # Append lines to proper NICK's queue.
             if self.target in self.queues:
                 queue = self.queues[self.target]
             else:
                 queue = self.queues[self.target] = Queue()
                 if self.target not in self.targets:
                     self.targets.append(self.target)
             queue.save_lines(writer, self.target, lines, encoding)
             # Restart output if not already in progress.
             if queue and not self.output_active:
                 self.send_something()
         common.Log('%s: %r -> %r\n' % (self.target, line, text))