def search_release(update: Update,
                   status_to_return_on_invalid_query=ConversationHandler.END):
    if len(update.message.text) < 3:
        update.message.reply_text(Strings.RELEASE_TOO_SHORT)

        logger.debug('returning status: %d', status_to_return_on_invalid_query)
        return status_to_return_on_invalid_query

    releases = db.search(update.message.text)[:64]
    if not releases:
        update.message.reply_text(Strings.RELEASES_EMPTY,
                                  reply_markup=Keyboard.HIDE,
                                  quote=True)

        logger.debug('returning status: %d', status_to_return_on_invalid_query)
        return status_to_return_on_invalid_query
    else:
        markup = Keyboard.from_list([
            '{id}. {titolo} • {dimensione_no_decimal} [{descrizione}]'.format(
                **release) for release in releases
        ])
        update.message.reply_text(Strings.SELECT_RELEASE, reply_markup=markup)

        logger.debug('returning status: %d', WAITING_RELEASE_SELECTION_FROM_KB)
        return WAITING_RELEASE_SELECTION_FROM_KB
Exemple #2
0
def search_release(update: Update, status_to_return_on_invalid_query=ConversationHandler.END):
    if len(update.message.text) < 3:
        update.message.reply_text(Strings.RELEASE_TOO_SHORT)
        return status_to_return_on_invalid_query

    releases = db.search(update.message.text, as_dict=True)[:64]
    if not releases:
        update.message.reply_text(Strings.RELEASES_EMPTY, reply_markup=Keyboard.HIDE, quote=True)

        return status_to_return_on_invalid_query
    else:
        markup = Keyboard.from_list(['{id}. {titolo}'.format(**release) for release in releases])
        update.message.reply_text(Strings.SELECT_RELEASE, reply_markup=markup)

        return WAITING_RELEASE
Exemple #3
0
def on_search_query(bot, update, user_data):
    logger.debug('search query')

    query = update.message.text

    if len(query.replace('%', '')) < 3:
        update.message.reply_text(
            'Il testo da cercare deve contenere almeno 3 caratteri (escluso il carattere jolly)'
        )
        return

    torrents_list = db.search(query, as_namedtuple=True)
    if not torrents_list:
        update.message.reply_text(
            'Non ho trovato alcun torrent che soddisfa la tua ricerca')
        return

    # massimo 100 entities in un msg
    strings_list = list()
    for t in torrents_list[:MAX_RESULT_ITEMS]:
        string = TORRENT_ROW.format(title=html_escape(truncate(t.title)),
                                    bot_username=bot.username,
                                    id=t.id,
                                    torrent_url=t.torrent_download_url,
                                    leech='-' if t.leech is None else t.leech,
                                    seed='-' if t.seed is None else t.seed)
        strings_list.append(string)

    # salva un dizionario di {id: magnet} per restituire il magnet quando l'utente aprirà il deeplink (senza eseguire
    # una nuova query al db)
    user_data['query_result'] = {t.id: t for t in torrents_list}

    text = '{0}\n\n{1}'.format(
        '\n'.join(strings_list),
        BOTTOM_TEXT.format(
            MAX_RESULT_ITEMS
            if len(torrents_list) > MAX_RESULT_ITEMS else len(torrents_list),
            len(torrents_list), db.total_entries, db.last_update))
    update.message.reply_html(text, disable_web_page_preview=True)