Search code examples
pythonredditpraw

Python praw reddit api: Reliably get posts as they are posted


At the moment I have a script that queries some subreddits every 30 seconds and returns the newest submission:

while True:

    for post in reddit.subreddit(query_list).new(limit=1):

        if previous != post:
             # Do something

        previous = post

    time.sleep(30)

The problem with this is that if there are more than two posts in that time frame it'll skip one of them. I know I can set a smaller wait time, or I can get more than one post at a time and sort through the results, but that doesn't really fix the problem, it just makes it less likely.

What I would much rather do, is 'subscribe' to a feed by having a continuously open connection that receives posts as they are posted. Does this exist? And if not, is there another solution I haven't thought of?

(I realise what I'm talking about would put a large strain on the reddit api servers, so it probably doesn't exist, but I thought it was worth asking just in case)


Solution

  • Yes, this exists in PRAW and it's called stream. Your entire code block can be replaced with the following:

    for post in reddit.subreddit(query_list).stream.submissions():
        # Do something
    

    You can stream subreddit comments by replacing submissions with comments.

    Other models can be streamed as well, such as Multireddit and Redditor.