Search code examples
python-3.xredditpraw

PRAW: How can I continuously check for new reddit submissions (and perform action)


Currently, the script I'm working on will look at the newest submission for a subreddit and return the flair from that post. But it will only do this once and ignore any new submissions while it is running.

How can I get this to continuously check in intervals and provide flairs for future incoming submissions?

Current code:

def subdata():

    for submission in subreddit.new(limit=1):
        flair = submission.link_flair_text

        return flair


subdata()

Solution

  • # ... (creation of subreddit object, logging into Reddit, ...)
    
    for submission in subreddit.stream.submissions():
      flair = submission.link_flair_text
      # call a function here that processes your flair
      custom_method(flair)
    
    def custom_method(flair):
      print(flair)
    

    When you are returning in a for loop the loop will be cancelled. You however want to continuously retrieve new submissions. It is not possible to return it.

    edit: fixed some mistakes thanks jarhill0