【问题标题】:Reddit Bot which replies to comments回复评论的 Reddit Bot
【发布时间】:2020-03-16 15:59:06
【问题描述】:

使用 PRAW 库,当提到给定关键字时,reddit 机器人会在子线程中回复帖子标题。我想更改,以便在子中的 cmets 中提及时回复,而不是帖子。

from urllib.parse import quote_plus

    import praw

    QUESTIONS = ["!hello"]
    REPLY_TEMPLATE = "world"


    def main():
        reddit = praw.Reddit(
            user_agent="xxx",
            client_id="xxx",
            client_secret="xxx",
            username="xxx",
            password="xxx",
        )

        subreddit = reddit.subreddit("sandboxtest")
        for submission in subreddit.stream.submissions():
            process_submission(submission)


    def process_submission(submission):
        # Ignore titles with more than 10 words as they probably are not simple
        # questions.
        if len(submission.title.split()) > 15:
            return

        normalized_title = submission.title.lower()
        for question_phrase in QUESTIONS:
            if question_phrase in normalized_title:
                url_title = quote_plus(submission.title)
                reply_text = REPLY_TEMPLATE.format(url_title)
                print("Replying to: {}".format(submission.title))
                submission.reply(reply_text)
                # A reply has been made so do not attempt to match other phrases.
                break


    if __name__ == "__main__":
        main()

【问题讨论】:

    标签: python python-3.x reddit praw


    【解决方案1】:

    如果你只想处理 cmets 变化:

    for submission in subreddit.stream.submissions():
      process_submission(submission)
    

    到:

    for comment in subreddit.stream.comments():
      process_comment(comment)
    
    # def process_comment you have to write yourself
    

    如果您想处理 cmets 和提交,请执行以下操作:

    while True:
      for submission in subreddit.stream.submissions(pause_after=-1):
        process_submission(submission)
      for commentin subreddit.stream.comments(pause_after=-1):
        process_comment(comment)
    

    process_comment() 的示例:

    def process_comment(comment):
      for question_phrase in QUESTIONS:
        if question_phrase in comment.body.lower():
          comment.reply("Hello")
    

    【讨论】:

      猜你喜欢
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 2020-10-17
      • 2010-09-13
      • 1970-01-01
      • 2017-11-10
      相关资源
      最近更新 更多