1

我编写了一个 reddit 机器人,它将搜索 subreddit 的评论,如果评论说感觉它会回复它。然后它将评论 id 写入一个文本文件,这样它就不会对同一个评论进行两次评论。

这是代码:

import praw
import time
import sys
import random

r = praw.Reddit('Posts Feels gif in response to someone saying feels'
                'by: Mjone77')
r.login('Feels_Bot', 'nottherealpassword')
file = open('Commentids.txt', 'r')
already_done = file.read()
file.close()
times = 0
feels = 0


while True:
   # try: 
        subreddit = r.get_subreddit('frozen')
        subreddit_comments = subreddit.get_comments()
        times+=1
        for comment in subreddit_comments:
            commentSays = comment.body
            commentSays = commentSays.lower()
            #print(commentSays)
            #print('\n')
            #if 'stop feels_bot' in commentSays:
                #sys.exit("Commanded to stop.")
            if comment.id not in already_done and 'feels' in commentSays:
                gif = random.randrange(0,3)
                if gif == 0:
                    comment.reply('[Relevant](http://i.imgur.com/pXBrf.gif)\n\n___\n\n^I ^am ^a ^bot ^not ^a ^real ^redditor \n\n ^Please ^contact ^/u/Mjone77 ^with ^any ^problems')
                if gif == 1:
                    comment.reply('[Relevant](http://gfycat.com/BraveSerpentineAzurevase)\n\n___\n\n^I ^am ^a ^bot ^not ^a ^real ^redditor \n\n ^Please ^contact ^/u/Mjone77 ^with ^any ^problems')
                if gif == 2:
                    comment.reply('[Relevant](http://www.gfycat.com/PlushMeanCowrie)\n\n___\n\n^I ^am ^a ^bot ^not ^a ^real ^redditor \n\n ^Please ^contact ^/u/Mjone77 ^with ^any ^problems')
                already_done = already_done+' '+comment.id
                file = open('Commentids.txt', 'w')
                file.write(already_done)
                file.close()
                #print('Commented~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
                time.sleep(5)
                feels+=1
        print('\n\n\n\n\n\n\n')        
        print('Feels:')
        print(feels)
        print('Times Ran:')
        print(times)
        time.sleep(60)
    #except AttriuteError:
        #pass

我运行它并让它运行并返回 HTTP 错误 504:网关超时。有谁知道如何防止这种情况发生或让它等待 60 秒然后再试一次?

此外,当我再次运行它时,没有更改工作程序中的任何代码,它决定给我这个:

Traceback (most recent call last):
  File "C:\Users\Me\Desktop\My Programs\Feels Bot\Feels Bot\FeelsBot.py", line 20, in <module>
    for comment in subreddit_comments:
  File "C:\Python34\lib\site-packages\praw\__init__.py", line 471, in get_content
    page_data = self.request_json(url, params=params)
  File "C:\Python34\lib\site-packages\praw\decorators.py", line 161, in wrapped
    return_value = function(reddit_session, *args, **kwargs)
  File "C:\Python34\lib\site-packages\praw\__init__.py", line 516, in request_json
    data = json.loads(response, object_hook=hook)
  File "C:\Python34\lib\json\__init__.py", line 331, in loads
    return cls(**kw).decode(s)
  File "C:\Python34\lib\json\decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python34\lib\json\decoder.py", line 359, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "C:\Python34\lib\site-packages\praw\__init__.py", line 403, in _json_reddit_objecter
    return object_class.from_api_response(self, json_data['data'])
  File "C:\Python34\lib\site-packages\praw\objects.py", line 58, in from_api_response
    return cls(reddit_session, json_dict=json_dict)
  File "C:\Python34\lib\site-packages\praw\objects.py", line 514, in __init__
    underscore_names=['replies'])
  File "C:\Python34\lib\site-packages\praw\objects.py", line 72, in __init__
    self.has_fetched = self._populate(json_dict, fetch)
  File "C:\Python34\lib\site-packages\praw\objects.py", line 141, in _populate
    setattr(self, name, value)
  File "C:\Python34\lib\site-packages\praw\objects.py", line 102, in __setattr__
    object.__setattr__(self, name, value)
AttributeError: can't set attribute
sys:1: ResourceWarning: unclosed <socket object at 0x0342D930>
C:\Python34\lib\importlib\_bootstrap.py:2150: ImportWarning: sys.meta_path is empty

它根本不再运行,有人知道如何解决此错误吗?

4

1 回答 1

3

我相信这个错误是最近 reddit 更新的结果。

从上面的链接:

此更改还可能对第三方扩展/应用程序/等产生一些意想不到的副作用。显示或以其他方式使用特定的向上/向下数字。我们已尝试采取各种预防措施以使过渡更加顺畅,但是如果您发现任何因此而导致的严重错误,请告诉我们。

我今天也开始使用我自己的机器人体验它,并且没有更改代码。

我在这里的 PRAW Github repo 上发布了一个问题。

编辑: AttributeError 已修复。确保将 PRAW 更新到最新版本,它应该可以再次工作。

于 2014-06-18T21:46:31.350 回答