Beispiel #1
0
def init_streams():
    reddit = reddit_instantiator.get_reddit_instance()
    scanned_subreddits = 'all'
    subreddit = reddit.subreddit(scanned_subreddits)
    comment_stream = subreddit.stream.comments(skip_existing=True, pause_after=-1)
    inbox_stream = reddit.inbox.stream(mark_read=False, pause_after=-1)
    return (comment_stream, inbox_stream)
Beispiel #2
0
def handle_comment_reply(comment):
    author = None
    if comment.distinguished == 'moderator':
        author = 'moderator'
    else:
        author = f'/u/{comment.author.name}'
    logging.info(f'Received inbox comment by {author}: {comment.body}')
    reddit = reddit_instantiator.get_reddit_instance()
    if not isinstance(comment, praw.models.Comment):
        return
    sentiment = check_sentiment(comment.body)
    try:
        if sentiment == 'positive':
            respond_to_positive_sentiment(comment)
        elif sentiment == 'negative':
            respond_to_negative_sentiment(comment)

        if sentiment is not None:
            reddit.inbox.mark_read([comment])
    except prawcore.exceptions.Forbidden as e:
        if e.response.reason == 'Forbidden':
            logging.info(
                f'Got 403 Forbidden error, probably because the bot is banned in {comment.subreddit_name_prefixed}'
            )
            reddit.inbox.mark_read([comment])
        else:
            raise
Beispiel #3
0
def get_full_comment_from_reddit(permalink_without_prefix):
    reddit = reddit_instantiator.get_reddit_instance()
    return reddit.comment(url=r'https://www.reddit.com' + permalink_without_prefix)
def get_latest_submissions(limit=40):
    reddit = reddit_instantiator.get_reddit_instance()
    return reddit.redditor('AutoCrosspostBot').submissions.new(limit=limit)
Beispiel #5
0
def get_posts_with_same_content(comment, subreddit):
    class Result:
        posts_found = False
        posts = []
        unable_to_search = False
        unable_to_search_reason = None

    result = Result()

    reddit = reddit_instantiator.get_reddit_instance()
    try:
        # The format of the query string is explained here: https://github.com/praw-dev/praw/issues/880
        query = f'url:\"{comment.submission.url}\"'
        submissions = reddit.subreddit(subreddit).search(query=query,
                                                         sort='new',
                                                         time_filter='all')
        # iterate over submissions to fetch them
        submissions = [s for s in submissions]
    except Exception as e:  #TODO change exception type to be specific
        error_message = e.args[0]
        # when reddit tries redirecting a search query of a link to the submission page, that means 0 results were found for the search query
        if error_message == 'Redirect to /submit':
            return result
        # when reddit redirects to /subreddits/search that means the subreddit doesn't exist
        elif error_message in [
                'Redirect to /subreddits/search', 'received 404 HTTP response'
        ]:
            if e.response.text:
                try:
                    response_obj = json.loads(e.response.text)
                    if response_obj['reason'] == 'banned':
                        result.unable_to_search = True
                        result.unable_to_search_reason = 'SUBREDDIT_IS_BANNED'
                        return result
                except json.JSONDecodeError:
                    pass

            result.unable_to_search = True
            result.unable_to_search_reason = 'SUBREDDIT_DOES_NOT_EXIST'
            return result
        # this error is recieved when the subreddit is private
        # "You must be invited to visit this community"
        elif error_message == 'received 403 HTTP response':
            result.unable_to_search = True
            result.unable_to_search_reason = 'SUBREDDIT_IS_PRIVATE'
            return result
        else:
            raise

    if len(submissions) > 0:
        result.posts_found = True
        result.posts = submissions
        return result

    prior_posts = repost_detector.get_reposts_in_sub(comment, subreddit)
    if prior_posts:
        result.posts_found = True
        result.posts = [
            reddit.submission(id=p['post_id']) for p in prior_posts
        ]

    return result
Beispiel #6
0
def get_comment_with_different_praw_instance(comment, username):
    reddit2 = reddit_instantiator.get_reddit_instance(username=username)
    comment2 = reddit2.comment(id=comment.id)
    return comment2