Praw AttributeError:无法设置属性

时间:2014-06-18 21:26:59

标签: python bots reddit attributeerror praw

我编写了一个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

它根本不再运行,任何人都知道如何修复此错误?

1 个答案:

答案 0 :(得分:3)

我认为此错误是recent reddit update的结果。

从以上链接:

  

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

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

I posted an issue on the PRAW Github repo here.

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

相关问题