示例#1
0
class HebraBot(threading.Thread):
    def __init__(self, servidor, canal, nombre, puerto, patron, ID_Bot):
        # Esto hay que hacerlo siempre: 
        threading.Thread.__init__(self)    

        # Copiamos como atributos los parametros:
        self.servidor     = servidor
        self.canal        = canal
        self.nombre       = nombre
        self.puerto       = puerto
        self.patron       = patron
        self.ID_Bot       = ID_Bot
        #Conexion BD
        print "Abriendo BD..."
        self.con        = sqlite3.connect('C:/DjangoProyectos/irc.db')  #con_bd.close()
        self.cursor     = self.con.cursor()  #cursor.close()

        #Generamos un ID para el BOT y registramos el BOT:
        print "Registrar BOT"
        self.registro_Bot(self.servidor, self.canal)

        # Creamos el objeto "bot":
        self.bot    = Bot(self.servidor, self.canal, self.nombre, self.puerto, self.patron, self.ID_Bot, self.ID_convers);

    # Lo que tiene que hacer en concurrencia:
    def run(self):
        print ">> lanzando bot... " 
        return self.bot.conexion()

    def existe_ID(self, ID_Bot):
        # print "  >>>> Probando si ID '" + self.ID_Bot + "' existe..."
        self.cursor.execute("SELECT ID_Bot FROM BOT_INFO WHERE ID_Bot == '" + str(self.ID_Bot) + "'")            
        if (self.cursor.fetchone()) != None:
              return True
        else:
              return False

    def generar_ID_Bot(self):
        probarDeNuevo   = True
        numerito        = 0
        while probarDeNuevo:   
            numerito = numerito + 1         
            # numerito          = random.choice(range(100))
            self.ID_Bot       = 'BOT' + str(numerito)
            # print "\n  >> Probando ID: " + self.ID
            if self.existe_ID(self.ID_Bot) == False:
                probarDeNuevo = False
                print "  >> ID... OK"
                print "  >> El ID del BOT es: " + self.ID_Bot + "\n  >> Anota el ID para futuras modificaciones.\n"
            else: 
                probarDeNuevo = True 
                # print "  >> El ID ya existe, probando otro..."        

        threading.Thread(name = self.ID_Bot)   
        return self.ID_Bot

    def registro_Bot(self, servidor, canal):
        tiempo_ini      = time.strftime('%d %b %y / %H:%M:%S')
        t1              = time.strftime('%H %M %S')
        self.ID_convers = t1.replace(" ", "")
        tiempo_fin      = 0
        eventos         = 0
        estado          = "ON"
        num_msg         = 0
        print " Generando ID..."
        self.ID_Bot  = self.generar_ID_Bot()  
        print " Registrando " + self.ID_Bot
        self.cursor.execute(
            "INSERT INTO BOT_INFO (ID_CONVERS, ID_BOT, INICIO, ULTIMA, SERVIDOR, CANAL, EVENTOS, NUM_MSG, ESTADO) VALUES ('"
            + str(self.ID_convers) +"','"+ str(self.ID_Bot)+"','"+ str(tiempo_ini) +"', '"+ str(tiempo_fin) +"',' "
            + str(self.servidor) +"', '"+ str(self.canal) +"', '"+ str(eventos) + "', '"
            + str(num_msg) + "', '"+ str(estado) +"')"
            )
        self.con.commit() 
        self.cursor.fetchone()
        print " Ok!  Registrado correctamente.";
        self.con.close()
        reintentar = False

    def desconexion(ID_Bot):
        # Bot.desconexion(self.ID_Bot)
        thread._stop(self.ID_BOT)

    def estado(ID_BOT):
        threading.isAlive(name = ID_BOT)
        threading.enumerate()