コード例 #1
0
def test_process_comment(database, reddit):
    created = utils.datetime_now()
    username = "******"
    comment_id = reddit_test.random_id()
    thread_id = reddit_test.random_id()
    comment = reddit_test.RedditObject(
        body=f"{static.TRIGGER}! 1 day",
        author=username,
        created=created,
        id=comment_id,
        link_id="t3_" + thread_id,
        permalink=f"/r/test/{thread_id}/_/{comment_id}/",
        subreddit="test")

    reddit.add_comment(comment)

    comments.process_comment(comment.get_pushshift_dict(), reddit, database)
    result = comment.get_first_child().body

    assert "CLICK THIS LINK" in result

    reminders = database.get_all_user_reminders(username)
    assert len(reminders) == 1
    assert reminders[0].user.name == username
    assert reminders[0].message is None
    assert reminders[0].source == utils.reddit_link(comment.permalink)
    assert reminders[0].requested_date == created
    assert reminders[0].target_date == created + timedelta(hours=24)
    assert reminders[0].id is not None
    assert reminders[0].recurrence is None
コード例 #2
0
ファイル: comments.py プロジェクト: samjaninf/RemindMeBot
def parse_comment(comment, database, count_string):
    if comment['author'] == static.ACCOUNT_NAME:
        log.debug("Comment is from remindmebot")
        return None

    log.info(
        f"{count_string}: Processing comment {comment['id']} from u/{comment['author']}"
    )
    body = comment['body'].lower()
    if f"{static.TRIGGER_LOWER}!" not in body and f"!{static.TRIGGER_LOWER}" not in body:
        log.debug("Command not in comment")
        return None

    time = utils.find_reminder_time(comment['body'])

    message_text = utils.find_reminder_message(comment['body'])

    reminder = Reminder(
        source=utils.reddit_link(comment['permalink']),
        message=message_text,
        user=comment['author'],
        requested_date=utils.datetime_from_timestamp(comment['created_utc']),
        time_string=time,
        timezone=database.get_settings(comment['author']).timezone)
    if not reminder.valid:
        return None

    if not database.save_reminder(reminder):
        reminder.result_message = "Something went wrong saving the reminder"
        reminder.valid = False
        log.warning(reminder.result_message)

    return reminder
コード例 #3
0
def test_process_cakeday_comment(database, reddit):
    username = "******"
    user = reddit_test.User(
        username,
        utils.parse_datetime_string("2015-05-05 15:25:17").timestamp())
    reddit.add_user(user)
    created = utils.parse_datetime_string("2019-01-05 11:00:00")
    comment_id = reddit_test.random_id()
    thread_id = reddit_test.random_id()
    comment = reddit_test.RedditObject(
        body=f"{static.TRIGGER_CAKEDAY}!",
        author=username,
        created=created,
        id=comment_id,
        link_id="t3_" + thread_id,
        permalink=f"/r/test/{thread_id}/_/{comment_id}/",
        subreddit="test")

    reddit.add_comment(comment)

    utils.debug_time = utils.parse_datetime_string("2019-01-05 12:00:00")
    comments.process_comment(comment.get_pushshift_dict(), reddit, database)
    result = comment.get_first_child().body

    assert "to remind you of your cakeday" in result

    reminders = database.get_all_user_reminders(username)
    assert len(reminders) == 1
    assert reminders[0].user.name == username
    assert reminders[0].source == utils.reddit_link(comment.permalink)
    assert reminders[0].requested_date == created
    assert reminders[0].target_date == utils.parse_datetime_string(
        "2019-05-05 15:25:17")
    assert reminders[0].id is not None
    assert reminders[0].recurrence == "1 year"
    assert reminders[0].message == "Happy Cakeday!"
コード例 #4
0
def parse_comment(comment, database, count_string, reddit):
    if comment['author'] == static.ACCOUNT_NAME:
        log.debug("Comment is from remindmebot")
        return None, None
    if comment['author'] in static.BLACKLISTED_ACCOUNTS:
        log.debug("Comment is from a blacklisted account")
        return None, None

    log.info(
        f"{count_string}: Processing comment {comment['id']} from u/{comment['author']}"
    )
    body = comment['body'].lower().strip()
    recurring = False
    cakeday = False
    allow_default = True
    if trigger_in_text(body, static.TRIGGER_RECURRING_LOWER):
        log.debug("Recurring reminder comment")
        recurring = True
        trigger = static.TRIGGER_RECURRING_LOWER
    elif trigger_in_text(body, static.TRIGGER_LOWER):
        log.debug("Regular comment")
        trigger = static.TRIGGER_LOWER
    elif trigger_start_of_line(body, static.TRIGGER_CAKEDAY_LOWER):
        log.debug("Cakeday comment")
        cakeday = True
        recurring = True
        trigger = static.TRIGGER_CAKEDAY_LOWER
    elif trigger_start_of_line(body, static.TRIGGER_SPLIT_LOWER):
        log.debug("Regular split comment")
        trigger = static.TRIGGER_SPLIT_LOWER
        allow_default = False
    else:
        log.debug("Command not in comment")
        return None, None

    target_date = None
    if cakeday:
        if database.user_has_cakeday_reminder(comment['author']):
            log.info("Cakeday already exists")
            return None, None

        target_date = utils.get_next_anniversary(
            reddit.get_user_creation_date(comment['author']))
        message_text = static.CAKEDAY_MESSAGE
        time = "1 year"

    else:
        time = utils.find_reminder_time(comment['body'], trigger)
        message_text = utils.find_reminder_message(comment['body'], trigger)

    reminder, result_message = Reminder.build_reminder(
        source=utils.reddit_link(comment['permalink']),
        message=message_text,
        user=database.get_or_add_user(comment['author']),
        requested_date=utils.datetime_from_timestamp(comment['created_utc']),
        time_string=time,
        recurring=recurring,
        target_date=target_date,
        allow_default=allow_default)
    if reminder is None:
        return None, None

    if cakeday:
        counters.replies.labels(source='comment', type='cake').inc()
    elif recurring:
        counters.replies.labels(source='comment', type='repeat').inc()
    elif not allow_default:
        counters.replies.labels(source='comment', type='split').inc()
    else:
        counters.replies.labels(source='comment', type='single').inc()

    database.add_reminder(reminder)

    reminder.user.recurring_sent = 0

    return reminder, result_message
コード例 #5
0
def test_update_incorrect_comments(database, reddit):
    comment_id1 = reddit_test.random_id()
    thread_id1 = reddit_test.random_id()
    comment1 = reddit_test.RedditObject(
        body=f"{static.TRIGGER}! 1 day",
        author="Watchful1",
        created=utils.datetime_now(),
        id=comment_id1,
        link_id="t3_" + thread_id1,
        permalink=f"/r/test/{thread_id1}/_/{comment_id1}/",
        subreddit="test")
    reddit.add_comment(comment1)
    comments.process_comment(comment1.get_pushshift_dict(), reddit, database)

    comment_id2 = reddit_test.random_id()
    thread_id2 = reddit_test.random_id()
    comment2 = reddit_test.RedditObject(
        body=f"{static.TRIGGER}! 1 day",
        author="Watchful1",
        created=utils.datetime_now(),
        id=comment_id2,
        link_id="t3_" + thread_id2,
        permalink=f"/r/test/{thread_id2}/_/{comment_id2}/",
        subreddit="test")
    reddit.add_comment(comment2)
    comments.process_comment(comment2.get_pushshift_dict(), reddit, database)

    comment_id3 = reddit_test.random_id()
    thread_id3 = reddit_test.random_id()
    comment3 = reddit_test.RedditObject(
        body=f"{static.TRIGGER}! 1 day",
        author="Watchful1",
        created=utils.datetime_now(),
        id=comment_id3,
        link_id="t3_" + thread_id3,
        permalink=f"/r/test/{thread_id3}/_/{comment_id3}/",
        subreddit="test")
    reddit.add_comment(comment3)
    comments.process_comment(comment3.get_pushshift_dict(), reddit, database)

    reminders = [
        Reminder(
            source="https://www.reddit.com/message/messages/XXXXX",
            message=utils.reddit_link(comment1.permalink),
            user=database.get_or_add_user("Watchful1"),
            requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
            target_date=utils.parse_datetime_string("2019-01-05 05:00:00")),
        Reminder(
            source="https://www.reddit.com/message/messages/XXXXX",
            message=utils.reddit_link(comment1.permalink),
            user=database.get_or_add_user("Watchful1"),
            requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
            target_date=utils.parse_datetime_string("2019-01-06 05:00:00")),
        Reminder(
            source="https://www.reddit.com/message/messages/XXXXX",
            message=utils.reddit_link(comment1.permalink),
            user=database.get_or_add_user("Watchful1"),
            requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
            target_date=utils.parse_datetime_string("2019-01-07 05:00:00")),
        Reminder(
            source="https://www.reddit.com/message/messages/XXXXX",
            message=utils.reddit_link(comment2.permalink),
            user=database.get_or_add_user("Watchful1"),
            requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
            target_date=utils.parse_datetime_string("2019-01-08 05:00:00")),
        Reminder(
            source="https://www.reddit.com/message/messages/XXXXX",
            message=utils.reddit_link(comment2.permalink),
            user=database.get_or_add_user("Watchful1"),
            requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
            target_date=utils.parse_datetime_string("2019-01-09 05:00:00"))
    ]
    for reminder in reminders:
        database.add_reminder(reminder)

    comments.update_comments(reddit, database)

    assert "3 OTHERS CLICKED THIS LINK" in reddit.get_comment(
        comment_id1).get_first_child().body
    assert "2 OTHERS CLICKED THIS LINK" in reddit.get_comment(
        comment_id2).get_first_child().body
    assert "CLICK THIS LINK" in reddit.get_comment(
        comment_id3).get_first_child().body