Exemplo n.º 1
0
def rss_set(context):
    job = context.job
    user_data = sql.get_all()

    # this loop checks for every row in the DB
    for row in user_data:
        row_id = row.id
        tg_feed_link = row.feed_link
        tg_old_entry_link = row.old_entry_link

        feed_processed = parse(tg_feed_link)

        new_entry_links = []
        new_entry_titles = []

        # this loop checks for every entry from the RSS Feed link from the DB row
        for entry in feed_processed.entries:
            # check if there are any new updates to the RSS Feed from the old entry
            if entry.link != tg_old_entry_link:
                new_entry_links.append(entry.link)
                new_entry_titles.append(entry.title)
            else:
                break

        # check if there's any new entries queued from the last check
        if new_entry_links:
            sql.update_url(row_id, new_entry_links)
        else:
            pass
Exemplo n.º 2
0
def rss_update(context):
    job = context.job
    user_data = sql.get_all()

    # this loop checks for every row in the DB
    for row in user_data:
        row_id = row.id
        tg_chat_id = row.chat_id
        tg_is_pinned = row.is_pinned
        tg_feed_link = row.feed_link

        feed_processed = parse(tg_feed_link)

        tg_old_entry_link = row.old_entry_link

        new_entry_links = []
        new_entry_titles = []

        # this loop checks for every entry from the RSS Feed link from the DB row
        for entry in feed_processed.entries:
            # check if there are any new updates to the RSS Feed from the old entry
            if entry.link != tg_old_entry_link:
                new_entry_links.append(entry.link)
                new_entry_titles.append(entry.title)
            else:
                break

        # check if there's any new entries queued from the last check
        if new_entry_links:
            sql.update_url(row_id, new_entry_links)
        else:
            pass

        if len(new_entry_links) < 5:
            # this loop sends every new update to each user from each group based on the DB entries
            for link, title in zip(reversed(new_entry_links),
                                   reversed(new_entry_titles)):
                final_message = "<b>{}</b>\n\n{}".format(
                    html.escape(title), html.escape(link))

                if len(final_message) <= constants.MAX_MESSAGE_LENGTH:
                    try:
                        rssmsg = context.bot.send_message(
                            chat_id=tg_chat_id,
                            text=final_message,
                            parse_mode=ParseMode.HTML)
                        if tg_is_pinned:
                            try:
                                context.bot.pinChatMessage(
                                    tg_chat_id, rssmsg.message_id)
                            except:
                                pass
                    except error.Unauthorized:
                        print("Cannot send msg bcz bot is kicked")
                        sql.remove_url(tg_chat_id, tg_feed_link)
                else:
                    try:
                        context.bot.send_message(
                            chat_id=tg_chat_id,
                            text=tl(
                                tg_chat_id,
                                "<b>Peringatan:</b> Pesan terlalu panjang untuk dikirim"
                            ),
                            parse_mode=ParseMode.HTML)
                    except error.Unauthorized:
                        print("Cannot send msg bcz bot is kicked")
                        sql.remove_url(tg_chat_id, tg_feed_link)
        else:
            for link, title in zip(reversed(new_entry_links[-5:]),
                                   reversed(new_entry_titles[-5:])):
                final_message = "<b>{}</b>\n\n{}".format(
                    html.escape(title), html.escape(link))

                if len(final_message) <= constants.MAX_MESSAGE_LENGTH:
                    try:
                        rssmsg = context.bot.send_message(
                            chat_id=tg_chat_id,
                            text=final_message,
                            parse_mode=ParseMode.HTML)
                        if tg_is_pinned:
                            try:
                                context.bot.pinChatMessage(
                                    tg_chat_id, rssmsg.message_id)
                            except:
                                pass
                    except error.Unauthorized:
                        sql.remove_url(tg_chat_id, tg_feed_link)
                else:
                    try:
                        context.bot.send_message(
                            chat_id=tg_chat_id,
                            text=tl(
                                tg_chat_id,
                                "<b>Peringatan:</b> Pesan terlalu panjang untuk dikirim"
                            ),
                            parse_mode=ParseMode.HTML)
                    except error.Unauthorized:
                        sql.remove_url(tg_chat_id, tg_feed_link)

            try:
                context.bot.send_message(
                    chat_id=tg_chat_id,
                    parse_mode=ParseMode.HTML,
                    text=tl(
                        tg_chat_id,
                        "<b>Peringatan: </b>{} kejadian telah ditinggalkan untuk mencegah spam"
                    ).format(len(new_entry_links) - 5))
            except error.Unauthorized:
                sql.remove_url(tg_chat_id, tg_feed_link)