Example #1
0
def remove_spam_submission(submission: praw.models.reddit.submission) -> bool:
    """Remove submission that links to spam and reply with explanation
    or constructive advice if warranted.

    Args:
        submission (praw.models.reddit.submission): Submission to remove as
            spam

    Returns:
        bool: True if submission is removed, else False
    """
    logger.debug("Enter remove_spam_submission")

    # Remove videos without comment
    if any(url in submission.url for url in BLACKLISTED_URLS):
        submission.mod.remove(spam=True)
        logger.info(
            f"Removed submission {submission.id} by u/{submission.author} "
            f"from r/{submission.subreddit.display_name}; "
            f"{submission.permalink}")
    else:
        return False

    # Reply with explanation or constructive advice if warranted.

    # Remove p**n without comment
    if any(url in submission.url for url in PORN_URLS):
        return True

    # Remove video and explain
    elif any(url in submission.url for url in VIDEO_URLS):
        text = add_boilerplate(
            "I removed your submission. "
            f"Videos are not allowed in r/{submission.subreddit.display_name}."
        )
        comment = submission.reply(text)
        comment.mod.distinguish(how="yes", sticky=True)
        return True

    # Remove blog posts and comment alternative
    elif any(url in submission.url for url in BLOG_URLS):
        text = add_boilerplate(
            "I removed your submission. "
            f"r/{submission.subreddit.display_name} receives a lot of spam "
            "from that domain. Try sharing the original article and offer "
            "context for discussion in the title of your submission.")
        comment = submission.reply(text)
        comment.mod.distinguish(how="yes", sticky=True)
        return True

    else:
        logger.warning(
            "The given submission exists in BLACKLISTED_URLS, but none of the "
            "constituent lists. This is a programming error.")
        return True

    logger.debug("Exit remove_spam_submission")
def remove_troll_submission(submission: praw.models.reddit.submission) -> None:
    """Remove submission that posted by a troll or underqualified users.
    Reply with explanation or constructive advice if warranted.

    Args:
        submission (praw.models.reddit.submission): Submission to remove as
            troll or underqualified user.
    """
    logger.debug("Enter remove_troll_submission")

    redditor = submission.author
    if submission.approved:
        return None

    # check if user is a moderator first
    subreddit_moderators = list(submission.subreddit.moderator())
    if redditor in subreddit_moderators:
        logger.info(
            f"Submission {submission.id} was authored by an "
            f"r/{submission.subreddit.display_name} moderator, "
            f"u/{redditor.name}"
        )
        return None

    total_karma = redditor.link_karma + redditor.comment_karma

    if total_karma <= -10:
        submission.mod.remove(spam=True)
    if total_karma <= 50:
        # long urls
        weekly_thread = "[weekly entering & transitioning thread](https://www.reddit.com/r/datascience/search?q=Weekly%20Entering%20%26%20Transitioning%20Thread&restrict_sr=1&t=week)"
        the_wiki = "[the wiki](https://www.reddit.com/r/datascience/wiki/index)"
        message_the_mods = "[message the mods](https://www.reddit.com/message/compose?to=%2Fr%2Fdatascience)"

        text = add_boilerplate(
            f"I removed your submission to r/{submission.subreddit.display_name}.\n"
            f"\n"
            f"r/{submission.subreddit.display_name} gets a lot of posts from "
            f"new redditors. It's likely your topic or question has been "
            f"discussed at length before, so we remove posts from authors with "
            f"less than 50 karma as a rule. You only have {total_karma} "
            f"karma right now.\n"
            f"\n"
            f"The {weekly_thread} is a good place to start. You may also find "
            f"useful resources on {the_wiki}.\n"
            f"\n"
            f"If you believe this is an error, or you're intentionally posting "
            f"with a throwaway account, please {message_the_mods} to approve "
            f"your submission."
        )
        comment = submission.reply(text)
        comment.mod.distinguish(how="yes", sticky=True)

        submission.mod.remove(spam=False)

    logger.debug("Exit remove_troll_submission")
def post_weekly_thread(reddit: praw.models.reddit) -> praw.models.Submission:
    """Post the weekly thread with required attributes

    `post_weekly_thread` does three things:
        1. Create the submission title and selftext
        2. Post the submission
        3. Distinguish, sticky, flair, etc.

    Args:
        reddit (praw.models.reddit): which reddit to post weekly thread

    Return:
        praw.models.Submission: New weekly thread
    """
    logger.info("Post weekly entering & transitioning thread")

    ## 1. Create the submission title and selftext
    # e.g. Weekly Entering & Transitioning Thread | 15 Sep 2019 - 22 Sep 2019
    title = ("Weekly Entering & Transitioning Thread | "
             f"{datetime.utcnow().strftime('%d %b %Y')} - "
             f"{(datetime.utcnow() + timedelta(days=7)).strftime('%d %b %Y')}"
             ).strip()

    # Long URLs we'll use to format the selftext
    faq = "[FAQ](https://www.reddit.com/r/datascience/wiki/frequently-asked-questions)"
    resources = "[Resources](https://www.reddit.com/r/datascience/wiki/resources)"
    past_weekly_threads = "[past weekly threads](https://www.reddit.com/r/datascience/search?q=weekly%20thread&restrict_sr=1&sort=new)"

    selftext = add_boilerplate(
        "Welcome to this week's entering & transitioning thread! "
        "This thread is for any questions about getting started, studying, or "
        "transitioning into the data science field. Topics include:\n"
        "\n"
        "* Learning resources (e.g. books, tutorials, videos)\n"
        "* Traditional education (e.g. schools, degrees, electives)\n"
        "* Alternative education (e.g. online courses, bootcamps)\n"
        "* Job search questions (e.g. resumes, applying, career prospects)\n"
        "* Elementary questions (e.g. where to start, what next)\n"
        "\n"
        f"While you wait for answers from the community, check out the {faq} "
        f"and {resources} pages on our wiki. You can also search for "
        f"{past_weekly_threads}.")

    ## 2. Post the submission
    submission = reddit.subreddit(SUBREDDIT_NAME).submit(title=title,
                                                         selftext=selftext,
                                                         send_replies=False)

    ## 3. Distinguish, sticky, flair, etc.
    submission.mod.flair(text="Discussion")
    submission.mod.approve()
    submission.mod.distinguish()
    submission.mod.sticky(state=True, bottom=True)

    return submission
def direct_unanswered_comments_to_weekly_thread(reddit: praw.models.reddit,
                                                old_thread_id: str,
                                                new_thread_id: str) -> None:
    """Direct unanswered comments in last weekly thread to the new weekly thread

    Args:
        reddit (praw.models.reddit): Reddit account to comment with
        old_thread_id (str)
        new_thread_id (str)
    """
    old_thread = reddit.submission(id=old_thread_id)
    new_thread = reddit.submission(id=new_thread_id)

    new_weekly_thread_md = f"[new weekly thread]({new_thread.permalink})"
    msg = add_boilerplate(
        f"I created a {new_weekly_thread_md}. Since you haven't received any "
        "replies yet, please feel free to resubmit your comment in the new "
        "thread.")

    for comment in old_thread.comments:
        if len(comment.replies) == 0:
            reply = comment.reply(msg)
            reply.mod.distinguish(how="yes")
Example #5
0
def test__add_boilerplate():
    test = "This is a test."
    assert test != add_boilerplate(test)