示例#1
0
def check_posts(reddit):
    """Checks the latest posts from a given subreddit.

    Parameters
    ----------
    reddit : praw.Reddit
        A Reddit instance.

    """

    processed_posts = load_log(POSTS_LOG)

    # We iterate over all our desired subreddits.
    for subreddit in config.SUBREDDITS:

        # Then we iterate over all the new posts.
        for submission in reddit.subreddit(subreddit).new(limit=100):

            if "twitter.com" in submission.url and "status" in submission.url and submission.id not in processed_posts:

                try:
                    reddit.submission(submission.id).reply(
                        transcribe_tweet(submission.url.replace("mobile.", ""),
                                         MESSAGE_TEMPLATE))

                    update_log(POSTS_LOG, submission.id)
                    print("Replied:", submission.id)

                except Exception as e:
                    update_log(POSTS_LOG, submission.id)
                    log_error("{}:{}".format(submission.url, e))
                    print("Failed:", submission.id)
示例#2
0
def check_comments(reddit):
    """Checks the latest comments from a given subreddit.

    Parameters
    ----------
    reddit : praw.Reddit
        A Reddit instance.

    """

    processed_comments = load_log(COMMENTS_LOG)

    # We iterate over all our desired subreddits.
    for subreddit in config.SUBREDDITS:

        # Then we iterate over all the new comments.
        for comment in reddit.subreddit(subreddit).comments(limit=100):

            # Only take into account new comments with a valid twitter link and not being made by this bot.
            if "twitter.com" in comment.body_html and "/status/" in comment.body_html and comment.author != config.REDDIT_USERNAME and comment.id not in processed_comments:

                try:
                    # Sometimes a comment may contain several links, we look for all of them.
                    comment_text = list()

                    # Get all tweet links.
                    soup = BeautifulSoup(comment.body_html, "html.parser")

                    for link in soup.find_all("a"):

                        if "twitter.com" in link[
                                "href"] and "/status/" in link["href"]:

                            comment_text.append(
                                transcribe_tweet(
                                    link["href"].replace("mobile.", ""),
                                    MESSAGE_TEMPLATE))

                    reddit.comment(comment.id).reply(
                        "\n\n*****\n\n".join(comment_text))

                    update_log(COMMENTS_LOG, comment.id)
                    print("Replied:", comment.id)

                except Exception as e:
                    update_log(COMMENTS_LOG, comment.id)
                    log_error("{}:{}".format(comment.id, e))
                    print("Faiied:", comment.id)