Search code examples
pythonpraw

How to query the latest submission by time in praw?


I am using python praw to query data from reddit. And I can use below code to query the latest 10 posts for the topic movies.

for submission in reddit.subreddit('movies').new(limit=10):
    print(submission)

I wonder how I can query the posts posted within the last 5 minutes.


Solution

  • I don't think there is a way to do this simply, but something like this should work. It will find the age of each post in minutes and check if it is less than 5. Then process the post only if it is.

    import time
    
    SUBMISSION_LIMIT = 10 # some number more than the number of posts every 5 minutes
    for submission in reddit.subreddit('movies').new(limit=SUBMISSION_LIMIT):
        minutes_since_post = (time.time() - submission.created_utc) / 60
        if minutes_since_post < 5:
            print(submission)