Esempio n. 1
0
def update_comments(reddit, database):
    count_incorrect = database.get_pending_incorrect_comments()

    incorrect_items = database.get_incorrect_comments(
        utils.requests_available(count_incorrect))
    if len(incorrect_items):
        i = 0
        for db_comment, reminder, new_count in incorrect_items:
            i += 1
            log.info(
                f"{i}/{len(incorrect_items)}/{count_incorrect}: Updating comment : "
                f"{db_comment.comment_id} : {db_comment.current_count}/{new_count}"
            )

            bldr = utils.get_footer(
                reminder.render_comment_confirmation(db_comment.thread_id,
                                                     new_count))
            result = reddit.edit_comment(''.join(bldr),
                                         comment_id=db_comment.comment_id)
            if result != ReturnType.SUCCESS:
                log.warning(
                    f"Failed to edit comment {db_comment.comment_id}: {result}"
                )

            db_comment.current_count = new_count

    else:
        log.debug("No incorrect comments")
Esempio n. 2
0
def send_reminders(reddit, database):
    timestamp = utils.datetime_now()
    count_reminders = database.get_count_pending_reminders(timestamp)
    counters.queue.set(count_reminders)

    reminders_sent = 0
    if count_reminders > 0:
        reminders = database.get_pending_reminders(
            utils.requests_available(count_reminders), timestamp)
        for reminder in reminders:
            reminders_sent += 1
            counters.notifications.inc()
            counters.queue.dec()
            log.info(
                f"{reminders_sent}/{len(reminders)}/{count_reminders}: Sending reminder to u/{reminder.user.name} : "
                f"{reminder.id} : {utils.get_datetime_string(reminder.target_date)}"
            )
            bldr = utils.get_footer(reminder.render_notification())
            result = reddit.send_message(reminder.user.name,
                                         "RemindMeBot Here!", ''.join(bldr))
            if result in [
                    ReturnType.INVALID_USER, ReturnType.USER_DOESNT_EXIST
            ]:
                log.info(f"User doesn't exist: u/{reminder.user.name}")
            if result in [ReturnType.NOT_WHITELISTED_BY_USER_MESSAGE]:
                log.info(
                    f"User blocked notification message: u/{reminder.user.name}"
                )

            if reminder.recurrence is not None:
                if reminder.user.recurring_sent > static.RECURRING_LIMIT:
                    log.info(
                        f"User u/{reminder.user.name} hit their recurring limit, deleting reminder {reminder.id}"
                    )
                    database.delete_reminder(reminder)
                else:
                    new_target_date = utils.parse_time(reminder.recurrence,
                                                       reminder.target_date,
                                                       reminder.user.timezone)
                    log.info(
                        f"{reminder.id} recurring from {utils.get_datetime_string(reminder.target_date)} to "
                        f"{utils.get_datetime_string(new_target_date)}")
                    reminder.target_date = new_target_date
                    reminder.user.recurring_sent += 1
            else:
                log.debug(f"{reminder.id} deleted")
                database.delete_reminder(reminder)

        database.commit()

    else:
        log.debug("No reminders to send")

    return reminders_sent
Esempio n. 3
0
def update_comments(reddit, database):
    count_incorrect = database.get_pending_incorrect_comments()

    incorrect_items = database.get_incorrect_comments(
        utils.requests_available(count_incorrect))
    if len(incorrect_items):
        i = 0
        for db_comment, reminder in incorrect_items:
            i += 1
            log.info(
                f"{i}/{len(incorrect_items)}/{count_incorrect}: Updating comment : "
                f"{db_comment.comment_id} : {db_comment.current_count}/{reminder.count_duplicates}"
            )

            bldr = utils.get_footer(reminder.render_comment_confirmation())
            reddit.edit_comment(''.join(bldr),
                                comment_id=db_comment.comment_id)
            db_comment.current_count = reminder.count_duplicates
            database.save_comment(db_comment)

    else:
        log.debug("No incorrect comments")