Exemple #1
0
def on_switch_pack_type(update: Update, context: CallbackContext):
    logger.info('swicth pack type inline keyboard')

    if not context.user_data.get('pack', None):
        update.callback_query.answer(Strings.PACK_TYPE_BUTTONS_EXPIRED)
        update.callback_query.message.edit_reply_markup(
            reply_markup=InlineKeyboard.REMOVE)
        return

    match = context.matches[0].group(1)
    reply_markup = InlineKeyboard.static_animated_switch(
        animated=match == 'animated')

    if match == 'animated':
        context.user_data['pack']['animated'] = True
    else:
        context.user_data['pack']['animated'] = False

    try:
        update.callback_query.message.edit_reply_markup(
            reply_markup=reply_markup)
    except BadRequest:
        pass

    update.callback_query.answer(Strings.PACK_TYPE_CHANGED.format(match))
Exemple #2
0
def on_release_selected(update: Update, context: CallbackContext):
    logger.info('%d: user selected the release from the keyboard', update.effective_user.id)

    release_match = re.search(r'^(\d+)\.\s.*', update.message.text)
    if not release_match:
        # user changed his query
        return search_release(update, WAITING_RELEASE)
    
    release_id = release_match.group(1)
    release = db.release_by_id(release_id, as_dict=True)
    
    forum_url = 'http://forum.tntvillage.scambioetico.org/index.php?showtopic={}'.format(release['topic'])
    release_string = Strings.RELEASE.format(
        titolo=utils.html_escape(release['titolo']),
        descrizione=utils.html_escape(release['descrizione']),
        dimensione=utils.human_readable_size(release['dimensione']),
        autore=utils.html_escape(release['autore']),
        categoria=CATEGORIE[release['categoria']],
        data=release['data'],
        magnet='magnet:?xt=urn:btih:{}'.format(release['hash'])
        # forum_url=forum_url,
        # webarchive_url='https://web.archive.org/web/{}'.format(forum_url)
    )
    
    reply_markup = InlineKeyboard.forum_urls(forum_url, 'https://web.archive.org/web/{}'.format(forum_url))
    update.message.reply_html(release_string, disable_web_page_preview=True, reply_markup=reply_markup)

    return WAITING_RELEASE
Exemple #3
0
def on_create_static_pack_command(update: Update, context: CallbackContext):
    logger.info('/create')

    context.user_data['pack'] = dict(animated=False)

    update.message.reply_html(
        Strings.PACK_CREATION_WAITING_TITLE,
        reply_markup=InlineKeyboard.static_animated_switch())

    return Status.CREATE_WAITING_TITLE
Exemple #4
0
def on_start_deeplink(update: Update, context: CallbackContext):
    logger.info('%d: /start deeplink', update.effective_user.id)

    topic = int(context.matches[0].group(1))

    release = db.get_release(topic, search_by='topic')

    release_string = Strings.RELEASE.format(**release)
    release_string = re.sub('.*(Usa /fatto.*)$', '', release_string)  # remove the last line, shouldn't show with deeplinks

    reply_markup = InlineKeyboard.release_info(release['id'], release['webarchive_url'])
    update.message.reply_html(release_string, disable_web_page_preview=True, reply_markup=reply_markup)
Exemple #5
0
def post_to_channel(bot: Bot, torrents: [Torrent]):
    if not config.feedsjob.get('new_torrents_notifications', None):
        logger.info('new releases notifications are disabled')
        return

    for torrent in torrents:
        logger.info('posting to channel topic %d...', torrent.topic)
        text = Strings.CHANNEL_NOTIFICATION_TEMPLATE.format(
            **torrent.format_dict())

        channel_post: [Message, None] = None
        try:
            channel_post = bot.send_message(
                config.feedsjob.new_torrents_notifications,
                text,
                parse_mode=ParseMode.HTML,
                disable_notification=True,
                disable_web_page_preview=True,
                reply_markup=InlineKeyboard.release_deeplink(torrent.deeplink))
        except (BadRequest, TelegramError) as e:
            logger.error('error while posting message to channel: %s',
                         e.message)

        if not config.feedsjob.get('send_torrent_file', True):
            time.sleep(3)
            continue

        logger.info('sending torrent file...')

        # noinspection PyBroadException
        try:
            result = requests.get(torrent.torrent_url,
                                  headers={'User-Agent': USER_AGENT})

            tmpfile = tempfile.TemporaryFile(prefix=str(torrent.topic),
                                             suffix='.torrent')
            tmpfile.write(result.content)
            tmpfile.seek(0)

            channel_post.reply_document(document=InputFile(
                tmpfile, filename='{}.torrent'.format(torrent.topic)),
                                        thumb=open('assets/torrent_thumb.jpg',
                                                   'rb'),
                                        caption=torrent.titolo,
                                        disable_notification=True)
            tmpfile.close()
        except Exception:
            logger.error(
                'exception while downloading/sending the torrent file',
                exc_info=True)

        time.sleep(3)
def on_collapse_button(update: Update, context: CallbackContext):
    logger.info('%d: collapse button', update.effective_user.id)

    release_id = context.match[1]
    release = db.get_release(release_id)

    update.callback_query.edit_message_text(
        Strings.RELEASE.format(**release),
        disable_web_page_preview=True,
        reply_markup=InlineKeyboard.release_info(release_id,
                                                 release['webarchive_url']),
        parse_mode=ParseMode.HTML)
    update.callback_query.answer(Strings.CB_ANSWER_COLLAPSED)
Exemple #7
0
def on_expand_magnet_button(update: Update, context: CallbackContext):
    logger.info('%d: magnet with trackers button', update.effective_user.id)

    release_id = context.match[1]
    release = db.release_by_id(release_id)

    reply_markup = InlineKeyboard.collapse_magnet_button(release_id)
    update.callback_query.edit_message_text(MAGNET_TEXT.format(
        hash=release['hash'], trackers_list='&tr='.join(TRACKERS)),
                                            disable_web_page_preview=True,
                                            reply_markup=reply_markup,
                                            parse_mode=ParseMode.HTML)
    update.callback_query.answer(Strings.CB_ANSWER_TRACKERS)
Exemple #8
0
def on_release_selected(update: Update, _):
    logger.info('%d: user selected the release from the keyboard', update.effective_user.id)

    release_match = re.search(r'^(\d+)\.\s.*', update.message.text)
    if not release_match:
        # user changed his query
        return search_release(update, WAITING_RELEASE)
    
    release_id = release_match.group(1)
    release = db.release_by_id(release_id)
    
    release_string = Strings.RELEASE.format(**release)
    
    reply_markup = InlineKeyboard.release_info(release_id, release['webarchive_url'])
    update.message.reply_html(release_string, disable_web_page_preview=True, reply_markup=reply_markup)

    return WAITING_RELEASE
def on_expand_magnet_button(update: Update, context: CallbackContext):
    logger.info('%d: magnet with trackers button', update.effective_user.id)

    release_id = context.match[1]
    release = db.get_release(release_id, raw=True)

    # if available, use the magnet we fetched from the forum page. Otherwise build it from the hash
    if not release['magnet'] and not release['hash'] and release['torrent_url']:
        # this happens when we haven't been able to fecth the forum page to scrape the magnet
        text = Strings.TORRENT_URL_NO_MAGNET.format(**release)
    elif not release['magnet'] and release['hash']:
        text = MAGNET_TEXT.format(hash=release['hash'],
                                  trackers_list='&tr='.join(TRACKERS))
    elif release['magnet']:
        text = '<code>{}</code>'.format(release['magnet'])
    else:
        text = Strings.MAGNET_NOT_AVAILABLE

    reply_markup = InlineKeyboard.collapse_magnet_button(release_id)
    update.callback_query.edit_message_text(text,
                                            disable_web_page_preview=True,
                                            reply_markup=reply_markup,
                                            parse_mode=ParseMode.HTML)
    update.callback_query.answer(Strings.CB_ANSWER_TRACKERS)