Example #1
0
def check_chollos():
        logging.debug("Checking chollos")
        result = []
        try:
            for userConfiguration in repository.get_dbc().get_table('UserConfiguration').all():
                user_id = userConfiguration['user_id']
                chollos = get_user_chollos(user_id)
                repository.get_dbc().insert_user_sent_chollos(user_id)
                sent_chollos = []
                for chollo in chollos:
                    if chollo.link:
                        sent_chollos.append(chollo.link)
                    if chollo.link and chollo.link not in repository.get_dbc().get_user_sent_chollos(user_id):
                        result.append(chollo)
                        # repository.get_dbc().add_user_sent_chollo(chollo.link, user_id)
                        notify_new_chollo(get_bot(), user_id, chollo)
                        # print(chollo.titulo+' - '+chollo.comercio)
                repository.get_dbc().replace_user_sent_chollos(sent_chollos, user_id)
        except Exception as e:
            if(str(e) == 'Forbidden: bot was blocked by the user' or str(e) == 'Chat not found'):
                repository.get_dbc().remove_user(user_id)
            else:
                logging.error("Failed checking chollos")
                logging.error("User ID: "+str(user_id))
                logging.error(str(e))
                repository.get_dbc().replace_user_sent_chollos(sent_chollos, user_id)
            # get_bot().send_message(chat_id=user_id, parse_mode="Markdown", text="Something go really bad. You couldn't be notify of news chollos")
        return result
Example #2
0
def modify_user_keywords(bot, update):
    keywords = update.message.text
    get_dbc().modify_keywords(keywords, update.message.chat_id)

    update.message.reply_text("Se ha actualizado su lista de Palabras Clave.")

    return ConversationHandler.END
def set_new_alert(bot, update, user_data):
    type_of_alert = update.message.text

    if type_of_alert == "CPU" and repository.get_dbc().get_cpu_alert(
            update.message.chat_id):
        update.message.reply_text(
            'You already have a CPU alarm configured, use the command /modify_alert and then select "CPU" to modify it.'
        )
        return ConversationHandler.END
    elif type_of_alert == "TEMP" and repository.get_dbc().get_temp_alert(
            update.message.chat_id):
        update.message.reply_text(
            'You already have a TEMP alarm configured, use the command /modify_alert and then select "TEMP" to modify it.'
        )
        return ConversationHandler.END
    elif type_of_alert == "RAM" and repository.get_dbc().get_ram_alert(
            update.message.chat_id):
        update.message.reply_text(
            'You already have a RAM alarm configured, use the command /modify_alert and then select "RAM" to modify it.'
        )
        return ConversationHandler.END
    elif type_of_alert == "DISK" and repository.get_dbc().get_disk_alert(
            update.message.chat_id):
        update.message.reply_text(
            'You already have a DISK alarm configured, use the command /modify_alert and then select "DISK" to modify it.'
        )
        return ConversationHandler.END
    else:
        update.message.reply_text(
            "Enter an integer value (the desired Degrees in case of TEMP alarm, and percentage for the rest) to adjust the alarm:"
        )
        user_data['type_of_alert'] = type_of_alert
        return SAVE_NEW_ALERT
Example #4
0
def modify_user_merchants(bot, update):
    merchants = update.message.text
    get_dbc().modify_merchants(merchants, update.message.chat_id)

    update.message.reply_text("Se ha actualizado su lista de Comercios.")

    return ConversationHandler.END
def verify_password(bot, update):
    password = update.message.text

    if(get_config().password_raspberryPi == password):
        update.message.reply_text("Your user has been successfully verified!")  
        get_dbc().set_user_authenticated(update.message.chat_id)
        return ConversationHandler.END 
    else:
        update.message.reply_text("Wrong password! Try again:")  
        return PASSWORD
def modify_user_price(bot, update):
    price = update.message.text
    pattern = re.compile("^(\d*,)?\d+$")
    text = ''

    if pattern.match(price) or price == '*':
        get_dbc().modify_price(price, update.message.chat_id)
        text = "Se ha actualizado su Precio máximo."
    else:
        text = "No se ha introducido un precio válido."

    update.message.reply_text(text)

    return ConversationHandler.END
Example #7
0
def get_user_chollos(user_id):
    repository.set_dbc(repository.DBC())
    keywords = repository.get_dbc().get_keywords(user_id)
    merchants = repository.get_dbc().get_merchants(user_id)
    chollos = []
    chollos.extend(extraer_datos_pagina_chollometro() + extraer_datos_pagina_michollo())
    result = []

    # Use "chcp 65001" command on windows console in order to show the string correctly
    for chollo in chollos:
        for keyword in keywords:
            if (keyword == '*' or keyword in chollo.titulo) and ('*' in merchants or chollo.comercio in merchants):
                        # print(chollo.titulo+' - '+chollo.comercio+' - '+chollo.precio+' - '+chollo.descripcion+' - '+chollo.cupon+' - '+chollo.link)
                        result.append(chollo)
    
    return result
Example #8
0
def modify_keywords(bot, update):
    keywords = get_dbc().get_keywords_str(update.message.chat_id)

    update.message.reply_text(
        """Introduzca una lista de Palabras Clave separadas por \",\" o escriba \"*\" si no desea configurar Palabras Clave.
    \nSu lista actual de Palabras clave es: \n""" + keywords)
    return KEYWORDS
Example #9
0
def modify_merchants(bot, update):
    merchants = get_dbc().get_merchants_str(update.message.chat_id)

    update.message.reply_text(
        """Introduzca una lista de Comercios separadas por \",\" o escriba \"*\" si no desea configurar Comercios.
    \nSu lista actual de Comercios es: \n""" + merchants)
    return MERCHANTS
def show_price(bot, update):
    price = get_dbc().get_price(update.message.chat_id)
    text = ''
    if price == '*':
        text = "Su precio máximo es:\n\n" + price
    else:
        text = "Su precio máximo es:\n\n" + price + " €"

    update.message.reply_text(text)
Example #11
0
def check_chollos_first_time(user_id):
        logging.debug("Checking chollos")
        result = []
        try:
            user_id = user_id
            chollos = get_user_chollos(user_id)
            repository.get_dbc().insert_user_sent_chollos(user_id)
            for chollo in chollos:
                if chollo.link not in repository.get_dbc().get_user_sent_chollos(user_id):
                    result.append(chollo)
                    repository.get_dbc().add_user_sent_chollo(chollo.link, user_id)
                    notify_new_chollo(get_bot(), user_id, chollo)
                    # print(chollo.titulo+' - '+chollo.comercio)
        except Exception as e:
            logging.error("Failed checking chollos")
            logging.error(e)
            logging.error("User ID: "+str(user_id))
            # get_bot().send_message(chat_id=user_id, parse_mode="Markdown", text="Something go really bad. You couldn't be notify of news chollos")
        return result
def start(bot, update):
    # print(update.message.chat_id)
    is_inserted = get_dbc().insert_user_configuration(update.message.chat_id)

    if is_inserted:
        update.message.reply_text("¡Se ha configurado el bot para ti ;)!")
        # Check if refresh time is larger than 3 seconds for send the chollos for the first time
        if (get_config().default_refresh_chollos > 3):
            time.sleep(3)
            check_chollos_first_time(update.message.chat_id)
    else:
        update.message.reply_text("¡Su usuario ya existe!")
def modify_price(bot, update):
    price = get_dbc().get_price(update.message.chat_id)
    text = ''

    if price == '*':
        text = """Introduzca un precio máximo, en caso de tener decimales, ha de incluirse con \",\" y no se ha de incluir el símbolo \"€\" o escriba \"*\" si no desea configurar Precio.
    \nSu Precio máximo actual es: \n""" + price
    else:
        text = """Introduzca un precio máximo, en caso de tener decimales, ha de incluirse con \",\" y no se ha de incluir el símbolo \"€\" o escriba \"*\" si no desea configurar Precio.
    \nSu Precio máximo actual es: \n""" + price + """ €"""

    update.message.reply_text(text)
    return PRICE
Example #14
0
def get_user_chollos(user_id):
    repository.set_dbc(repository.DBC())
    keywords = repository.get_dbc().get_keywords(user_id)
    merchants = repository.get_dbc().get_merchants(user_id)
    price = repository.get_dbc().get_price(user_id)
    chollos = []
    chollos.extend(extraer_datos_pagina_chollometro() + extraer_datos_pagina_michollo())
    result = []

    # Use "chcp 65001" command on windows console in order to show the string correctly
    for chollo in chollos:
        try:
            precio = float(chollo.precio.replace(',','.').replace('€',''))
        except ValueError:
            precio = 0
        for keyword in keywords:
            if ((keyword.strip() == '*' or keyword.strip().lower() in chollo.titulo.lower()) and 
                ('*' in merchants or chollo.comercio.strip() in merchants or not chollo.comercio) and
                ('*' == price or  precio <= float(price.replace(',','.')))):
                        # print(chollo.titulo+' - '+chollo.comercio+' - '+chollo.precio+' - '+chollo.descripcion+' - '+chollo.cupon+' - '+chollo.link)
                        result.append(chollo)
    
    return result
def start(bot, update):
    # print(update.message.chat_id)
    is_inserted = get_dbc().insert_user_configuration(update.message.chat_id)

    if is_inserted:
        update.message.reply_text(
            "User configured! Use the command /password to put the same password "
            +
            "that you had put at the config file. \n\nWhen you enter the password, your user "
            + "will be verified and you will be able to use the commands.")
    else:
        update.message.reply_text(
            "You user already exists! Remember, use the command /password to put the same password "
            +
            "that you had put at the config file. \n\nWhen you enter the password, your user "
            + "will be verified and you will be able to use the commands.")
def save_new_alert(bot, update, user_data):
    type_of_alert = user_data['type_of_alert']

    if type_of_alert == "CPU":
        repository.get_dbc().set_cpu_alert(update.message.chat_id,
                                           update.message.text)
    if type_of_alert == "TEMP":
        repository.get_dbc().set_temp_alert(update.message.chat_id,
                                            update.message.text)
    if type_of_alert == "RAM":
        repository.get_dbc().set_ram_alert(update.message.chat_id,
                                           update.message.text)
    if type_of_alert == "DISK":
        repository.get_dbc().set_disk_alert(update.message.chat_id,
                                            update.message.text)

    update.message.reply_text('Your alarm has been configured successfully!')

    return ConversationHandler.END
Example #17
0
def check_chollos():
        logging.debug("Checking chollos")
        result = []
        try:
            for userConfiguration in repository.get_dbc().get_table('UserConfiguration').all():
                user_id = userConfiguration['user_id']
                chollos = get_user_chollos(user_id)
                if user_id not in old_chollos:
                    old_chollos[user_id] = []
                for chollo in chollos:
                    if chollo.link not in old_chollos[user_id]:
                        result.append(chollo)
                        old_chollos[user_id].append(chollo.link)
                        notify_new_chollo(get_bot(), user_id, chollo)
                        # print(chollo.titulo+' - '+chollo.comercio)
        except Exception as e:
            logging.error("Failed checking chollos")
            logging.error(e)
            # get_bot().send_message(chat_id=user_id, parse_mode="Markdown", text="Something go really bad. You couldn't be notify of news chollos")
        return result
def shutdown(bot, update):
    if get_dbc().get_user_authenticated(update.message.chat_id):
        update.message.reply_text("Your Raspberry Pi will be shut down now.")
        raspberryPiService.shutdown()
    else:
        update.message.reply_text("You haven't putted your password yet. Use the command /password to confirm it.")
def save_removed_alert(bot, update):
    type_of_alert = update.message.text

    if type_of_alert == "CPU" and repository.get_dbc().get_cpu_alert(
            update.message.chat_id) == None:
        update.message.reply_text(
            'You  haven\'t yet a CPU alarm configured, use the command /add_alert and then select "CPU" to create it.'
        )
    elif type_of_alert == "TEMP" and repository.get_dbc().get_temp_alert(
            update.message.chat_id) == None:
        update.message.reply_text(
            'You  haven\'t yet a TEMP alarm configured, use the command /add_alert and then select "CPU" to create it.'
        )
    elif type_of_alert == "RAM" and repository.get_dbc().get_ram_alert(
            update.message.chat_id) == None:
        update.message.reply_text(
            'You  haven\'t yet a RAM alarm configured, use the command /add_alert and then select "CPU" to create it.'
        )
    elif type_of_alert == "DISK" and repository.get_dbc().get_disk_alert(
            update.message.chat_id) == None:
        update.message.reply_text(
            'You  haven\'t yet a DISK alarm configured, use the command /add_alert and then select "CPU" to create it.'
        )
    else:
        if type_of_alert == "CPU":
            repository.get_dbc().set_cpu_alert(update.message.chat_id, None)
        if type_of_alert == "TEMP":
            repository.get_dbc().set_temp_alert(update.message.chat_id, None)
        if type_of_alert == "RAM":
            repository.get_dbc().set_ram_alert(update.message.chat_id, None)
        if type_of_alert == "DISK":
            repository.get_dbc().set_disk_alert(update.message.chat_id, None)
        update.message.reply_text('Your alarm has been removed successfully!')

    return ConversationHandler.END
Example #20
0
def list_merchants(bot, update):
    merchants = get_dbc().get_merchants_str(update.message.chat_id)

    update.message.reply_text("Su lista de Comercios es:\n\n" + merchants)
Example #21
0
def list_keywords(bot, update):
    keywords = get_dbc().get_keywords_str(update.message.chat_id)

    update.message.reply_text("Su lista de Palabras Clave es:\n\n" + keywords)
def check_raspberryPi():
    repository.set_dbc(repository.DBC())
    logging.debug("Checking alerts")

    for userConfiguration in repository.get_dbc().get_table(
            'UserConfiguration').all():
        user_id = userConfiguration['user_id']
        repository.get_dbc().insert_user_configuration(user_id)
        cpu_percentage = get_cpu_percentage()
        temp_degrees = get_temp_info()  # 40 -> For testing purposes
        ram_percentage = get_ram_info().percent
        disk_percentage = get_disk_info().percent

        if repository.get_dbc().get_cpu_alert(user_id) != None and int(
                repository.get_dbc().get_cpu_alert(user_id)) <= cpu_percentage:
            print(cpu_percentage)
            print("CPU PERCENTAGE IS HIGH")

        if repository.get_dbc().get_temp_alert(user_id) != None and int(
                repository.get_dbc().get_temp_alert(user_id)) <= temp_degrees:
            print(temp_degrees)
            print("TEMP IS HIGH")

        if repository.get_dbc().get_ram_alert(user_id) != None and int(
                repository.get_dbc().get_ram_alert(user_id)) <= ram_percentage:
            print(ram_percentage)
            print("RAM USAGE IS HIGH")

        if repository.get_dbc().get_disk_alert(user_id) != None and int(
                repository.get_dbc().get_disk_alert(
                    user_id)) <= disk_percentage:
            print(disk_percentage)
            print("DISK USAGE IS HIGH")