0

使用我从本教程中获得的代码,我正在尝试从 Python 创建 twitter 机器人,以转发使用某些关键字找到的任何推文。它使用来自 Python-Twitter Developers 和 Redis 的python-twitter API 包装器。我已经安装并获得了包装器和 Redis,它们运行没有问题,但是当我从命令行运行 twit_bot.py 时,出现以下错误:

Traceback (most recent call last):
  File "twit_bot.py", line 116, in <module>
    twitter_session = auth_to_twitter(API_CREDENTIALS)
  File "twit_bot.py", line 27, in auth_to_twitter
    api = twitter.Api(consumer_key=api_credentials['consumer_key'],
AttributeError: 'module' object has no attribute 'Api'

这是我的机器人的粘贴箱。我是一个完全的python新手,真的不知道如何解决这个问题。

从上面的 pastebin 中,我从第 27 行开始复制到第 36 行:

def auth_to_twitter(api_credentials):
    """Returns an authenticated twitter session object"""
    api = twitter.Api(consumer_key=api_credentials['consumer_key'],
                      consumer_secret=api_credentials['consumer_secret'],
                      access_token_key=api_credentials['access_token_key'],
                      access_token_secret=api_credentials['access_token_secret'])

    if (api.VerifyCredentials() is not None):
        return api

    return False

更新:新错误按照用户 Bibhas 的建议, 在运行pip freeze并卸载了路径中的额外 twitter 包装器后,我从命令行运行twit_bot.py,但仍然出现错误,这次它看起来如下所示:

Traceback (most recent call last):
  File "twit_bot.py", line 118, in <module>
    run_bot(twitter_session, redis, TWITTERBOT_KEYWORDS)
  File "twit_bot.py", line 108, in run_bot
    update_search_stack(api_session, redis, key)
  File "twit_bot.py", line 65, in update_search_stack
    since_id = get_since_id(redis, "%s:%s" % (TWITTERBOT_LIST, keyword))
  File "twit_bot.py", line 41, in get_since_id
    since_id = redis.get(key + ":last_since_id")
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/redis/client.py", line 585, in get
    return self.execute_command('GET', name)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/redis/client.py", line 381, in execute_command
    connection.send_command(*args)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/redis/connection.py", line 304, in send_command
    self.send_packed_command(self.pack_command(*args))
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/redis/connection.py", line 286, in send_packed_command
    self.connect()
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/redis/connection.py", line 234, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 61 connecting localhost:6379. Connection refused.

再次更新!

由于看起来 Redis 服务器是给我一个问题 ( redis.exceptions.ConnectionError: Error 61 connecting localhost:6379. Connection refused.) 的服务器,如上面的错误所示,我查看了这个 stackoverflow 问题,希望得到一个潜在的答案,但进入 python 控制台并运行

>>> import redis
>>> print redis.Redis("localhost")
<redis.client.Redis object at 0x10e5480d0>
>>> 

没有给我任何有用的东西,因为它说它已连接(我想!?!?!)所以我仍然不确定如何解决这个问题,以便我的机器人工作。:C

def run_bot(api_session, redis, keywords):
    """"""
    while 42:
        for key in keywords:
            update_search_stack(api_session, redis, key)
            update_since_id(redis, "%s:%s" % (TWITTERBOT_LIST, key))
            tweet_and_shout(api_session, redis, key, timeout=1)
        print "Waiting for one minute\n\n\n"
        sleep(60)


if __name__ == "__main__":
    twitter_session = auth_to_twitter(API_CREDENTIALS)
    redis = redis.Redis("localhost")
    run_bot(twitter_session, redis, TWITTERBOT_KEYWORDS)
4

0 回答 0