vendredi 12 juin 2020

Python times out after if in for loop

F.W. This isn't just a PRAW question, it leans toward Python more than PRAW. Python people are welcome to contribute, and please note this is not my mother language xD!

Essentially, I'm writing a Reddit bot using the PRAW that does the following:

  • Loop through "unsaved" posts
  • Loop through the comments of said posts (targeting subcomments)
  • If the comment contains "!completed", is written by the submitter OR is a moderator, and the parent comment is not by submitter:
  • Do etc., e.x. print("Hey") No, I didn't explain that too well. Examples are better, so here xD:

Use cases:

- Post by @dudeOne
 - Comment by @dudeTwo
  - Comment with "!completed" by @dudeOne
- Post by @dudeOne
 - Comment by @dudeTwo
  - Comment with "!completed" by @moderatorOne

print("Hey"), and:

- Post by @dudeOne
 - Comment by @dudeOne
  - Comment with "!completed" by @dudeOne

... does nothing, maybe even removes + messages @dudeOne.

Here's my messy code (xD):

import praw
import os
import re

sub = "RedditsQuests"

client_id = os.environ.get('client_id')
client_secret = os.environ.get('client_secret')
password = os.environ.get('pass')

reddit = praw.Reddit(client_id=client_id,
                     client_secret=client_secret,
                     password=password,
                     user_agent='r/RedditsQuests bot',
                     username='TheQuestMaster')

for submission in reddit.subreddit(sub).new(limit=None):
    submission.comments.replace_more(limit=None)
    if submission.saved is False:
        for comment in submission.comments.list():
            if ((("!completed" in comment.body)) and ((comment.is_submitter) or ('RedditsQuests' in comment.author.moderated())) and (comment.parent().author.name is not submission.author.name)):
              print("etc...")

There's a decently-sized stack, so I've added it in this bin for your reference. To me it looks like PRAW is timing out because the if-in-for loop is taking too long. I could be wrong though!

Any help is appreciated! Cheers! Thanks in advance, Bobbbay

Aucun commentaire:

Enregistrer un commentaire