2017-03-16 6 views
1

こんにちはStackOverflowのコミュニティを再起動KeepRunning /、Pythonスクリプト(TwitterAPI)

TwitterのストリーミングAPIからのストリーミングデータに走り続けます。

以下

が(原因Twitterの再起動API、またはファイルシステムに、私はわからない)時には、ストリーミングは動作を停止私は

#Import the necessary methods from tweepy library 
from tweepy.streaming import StreamListener 
from tweepy import OAuthHandler 
from tweepy import Stream 

#Variables that contains the user credentials to access Twitter API 
access_token = "<>" 
access_token_secret = "<>" 
consumer_key = "<>" 
consumer_secret = "<>" 

########################################################## 
# listener received tweets to stdout - MINUTE 
class StdOutListener(StreamListener): 

    def on_data(self, data): 
     print(data) 
     return True 

    def on_error(self, status): 
     print(status) 

########################################################## 
# Main program 
if __name__ == '__main__': 

    #This handles Twitter authetification and the connection to Twitter Streaming API 
    l = StdOutListener() 
    auth = OAuthHandler(consumer_key, consumer_secret) 
    auth.set_access_token(access_token, access_token_secret) 

    # Initiate the connection to Streaming API 
    twitter_stream = Stream(auth, l) 

    # Get a sample of the public data following through Twitter 
    twitter_stream.sample() 

を使用TwitterのストリーミングAPIの基本的なバージョンであり、誤差は次のようです

StreamAPIError1

StreamAPIError2

下の写真に私の質問は、私たちがどのtechniqを持っているということですファイル1が実行されているかどうかをテストする2番目のファイルを作成するのと同じように、ファイル1が失敗するたびにファイル1をアクティブにします。
- ファイル1にいくつかの手法を統合して、またはいくつかの提案。

私はあなたの意見を持つことができます。それは

はありがとうコードを来る場合、それは多くの良いです。

+0

'すべての周りBaseException'ブロックのtry ... exceptを追加しますか? – abccd

+0

私は答えに解決策を得るときにこれを試しました。 Iircは再試行する必要があるので動作しないので、理想的ではない真の間にそれをラップしてしまいました。貢献度は –

答えて

1

私は、Python Twitterのボットとの良好な効果へretryingライブラリを使用しました。

https://pypi.python.org/pypi/retrying

は、コード例を持っていますが、それはかなりあなたの機能に@retryデコレータを使用するのと同じくらい簡単です。 EDIT: あなたはあなたのクラスのメソッドにそれを使用することができます:あなたは、これはそれを使用しないように注意する必要がありますので、あなたがそのAPIをコール回数

@retry 
def on_data(): 
    <code> 

NB twitterにログインします。

編集: 私の経験から、あなたをブロックしている相手からツイートを取得する必要がある場合は、これが失敗するため、これに対処するために何かを入れる必要があるため、これは役に立たないでしょう。

+1

です。この場合、クラスの上にコマンドを置くべきですか?
@retry
class StdOutListener(StreamListener):? – NNguyen

+1

歓迎です:) - Qを編集して例を挙げます! –