2017-02-06 9 views
1

私はnirgs forked version of Tweepyを使用しています。これは、2017-01-312017-02-01の間のツイートを取得するために使用する必要があります。私のコードが動作し、Twitters Rate Limitsのために、以前に述べたような遠く離れたつぶやきを扱えるように、複数の認証ハンドラを切り替える必要があります。Tweepy複数の認証ハンドラ

oauth_keys = [["consumer_key_1", "consumer_secret_1", "access_token_1", "access_token_secret_1"], ["consumer_key_2", "consumer_secret_2", "access_token_2", "access_token_secret_2"]] 

auths = [] 
for consumer_key, consumer_secret, access_key, access_secret in oauth_keys: 
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
    auth.set_access_token(access_key, access_secret) 
    auths.append(auth) 

api = tweepy.API(auths, monitor_rate_limit=True) 

while True: 
    try: 
     for tweet in tweepy.Cursor(api.search, q='bitcoin', since=datetime.date(2017, 1, 31), lang='en').items(3000): 
      ... 
    except tweepy.TweepError as e: 
     api.auth_idx += 1 
     continue 

File "build/bdist.macosx-10.11-intel/egg/tweepy/api.py", line 45, in auth_idx 
IndexError: Index out of bounds 

メインのコードは次のようになります。私は私が手に次のエラーがthis instructionを使用しますが、後にレート制限に達するとapi.auth_idx += 1を使用することにより、次の認証ハンドラへの切り替えをしようとしています私は何が間違っているのか分かりません。どんな助けでも大歓迎です!

答えて

2

ここに解決策があります。

エラーは、オブジェクト自体のインデックスを作成していました。私は、インデックスとして変数を作成する必要がありました:

switch += 1 
api.auth_idx = switch 

そしてここでは、完全なコードで、誰もが同じアプリを構築しようとする場合には:あなただけの場合がありますけれども

oauth_keys = [ 
    ["consumer_key_1", "consumer_secret_1", "access_token_1", "access_token_secret_1"], 
    ["consumer_key_2", "consumer_secret_2", "access_token_2", "access_token_secret_2"] 
    ] 

auths = [] 
for consumer_key, consumer_secret, access_key, access_secret in oauth_keys: 
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
    auth.set_access_token(access_key, access_secret) 
    auths.append(auth) 

# api 
api = tweepy.API(auths) 
currentCursor = tweepy.Cursor(api.search, q = 'bitcoin', since='2017-02-01', until='2017-02-02', lang='en') 
c = currentCursor.items() 

switch = 0 
api.auth_idx = switch 
tweets = [] 
early_dt_val = datetime.datetime.strptime('2017-01-31 22:34:48', '%Y-%m-%d %H:%M:%S') 
later_dt_val = datetime.datetime.strptime('2017-02-01 01:25:30', '%Y-%m-%d %H:%M:%S') 
maxId = 0 

while True: 
    try: 
     #for tweet in tweepy.Cursor(api.search, q='bitcoin', since=datetime.date(2017, 1, 31), lang='en').items(3000) 
     for tweet in c: 
      tweets.append(tweet) 
      maxId = tweet.id 

     for tweet in reversed(tweets): 
      tweetTime = tweet.created_at 
      if tweetTime < later_dt_val and tweetTime > early_dt_val: 
       print('Date: ' + str(tweet.created_at)) 
       print('Tweet: ' + tweet.text) 

    except tweepy.TweepError as e: 
     print e 
     print "---------------------------Rate limit exceeded.---------------------------" 
     print "switching keys..." 
     switch += 1 
     if switch > 4: 
      print "Limit reached" 
      break 
     else: 
      api.auth_idx = switch   # naechster Key 
      currentCursor = tweepy.Cursor(api.search, q='bitcoin', since='2017-02-01', until='2017-02-02', lang='en', max_id=maxId) 
+1

は、私にはよさそうです'api = tweepy.API(auths、retry_count = 3、retry_delay = 5、retry_errors = set([401、404、500、503])は、以下のようなブロッキング/待機パラメータでAPIオブジェクトを初期化します。 )、monitor_rate_limit = True、wait_on_rate_limit = True) ' いずれにしても問題ありません。 – nirg

関連する問題