하위 레딧에서 최신 댓글을 확인하는 레딧 봇을 만들려고하는데, 댓글에 잘못된 인용문이 포함되어 있으면 봇이 실제 인용문으로 답장하기를 원합니다. 내 문제는 내 봇이 reddit 시간 초과로 인해 몇 분 동안 기다린 후 대기가 끝난 후 예외 오류가 발생한다는 것입니다.
exc 변수를 만들고 0 또는 1로 설정하여 한 번에 하나의 예외 만 처리하도록 시도했지만 작동하지 않았습니다.
다음은 내 코드입니다 (식별 정보 제외).
import praw
import re
import time
import os
reddit = praw.Reddit(client_id= 'id',
client_secret= 'secret',
user_agent='<console: reddit_bot: 0.0.1 (by /u/username)>',
username= 'username',
password= 'password'
)
if not os.path.isfile("comments_replied_to.txt"):
comments_replied_to = []
else:
with open("comments_replied_to.txt", "r") as f:
comments_replied_to = f.read()
comments_replied_to = comments_replied_to.split("\n")
comments_replied_to = filter(None, comments_replied_to)
subreddit = reddit.subreddit('subreddit')
pos=0
exc = 0
keywords = [ 'Luke, I am your father', 'Do you feel lucky, punk?']
for comment in subreddit.stream.comments():
for keyword in keywords:
if keyword in comment.body and comment.id not in comments_replied_to and comment.author != 'thatotteraccount':
print("String with " + keyword + " found in comment " + comment.id)
if keyword == 'Luke, I am your father':
if exc==0:
try:
comment.reply('* "No, I am your Father."')
except praw.exceptions.APIException as e:
exc=1
if (e.error_type == "RATELIMIT"):
delay = re.search("(\d+) minutes", e.message)
if delay:
delay_seconds = float(int(delay.group(1)) * 60)
time.sleep(delay_seconds)
comment.reply('* "No, I am your Father."')
exc=0
else:
delay = re.search("(\d+) seconds", e.message)
delay_seconds = float(delay.group(1))
time.sleep(delay_seconds)
comment.reply('* "No, I am your Father."')
exc=0
if keyword == 'Do you feel lucky, punk?':
if exc==0:
try:
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
except praw.exceptions.APIException as e:
exc=1
if (e.error_type == "RATELIMIT"):
delay = re.search("(\d+) minutes?", e.message)
if delay:
delay_seconds = float(int(delay.group(1)) * 60)
time.sleep(delay_seconds)
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
exc=0
else:
delay = re.search("(\d+) seconds", e.message)
delay_seconds = float(delay.group(1))
time.sleep(delay_seconds)
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
exc=0
print("Replied to comment" + comment.id)
list(comments_replied_to).append(comment.id)
with open ("comments_replied_to.txt", "a") as f:
f.write(comment.id + "\n")
발생하는 오류는 다음과 같습니다.
File "C:\Users\Blaze\Desktop\reddit_bot2.py", line 56, in <module>
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\models\reddit\mixins\replyable.py", line 26, in reply
return self._reddit.post(API_PATH['comment'], data=data)[0]
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\reddit.py", line 483, in post
return self._objector.objectify(data)
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\objector.py", line 149, in objectify
raise APIException(*errors[0])
praw.exceptions.APIException: RATELIMIT: 'you are doing that too much. try again in 8 minutes.' on field 'ratelimit'
__During handling of the above exception, another exception occurred:__
Traceback (most recent call last):
File "C:\Users\Blaze\Desktop\reddit_bot2.py", line 65, in <module>
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\models\reddit\mixins\replyable.py", line 26, in reply
return self._reddit.post(API_PATH['comment'], data=data)[0]
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\reddit.py", line 483, in post
return self._objector.objectify(data)
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\objector.py", line 149, in objectify
raise APIException(*errors[0])
praw.exceptions.APIException: RATELIMIT: 'you are doing that too much. try again in 6 seconds.' on field 'ratelimit'
모든 도움을 주시면 감사하겠습니다.
당신이 기다리는 시간을 정확히 기다리는 것처럼 보이지만 Reddit은 냉각을 끝내지 않았습니다. 수면 시간에 10 ~ 30 초를 추가해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다