NNguyen
NNguyen

Reputation: 113

KeepRunning/Restarting a python script (TwitterAPI)

Hi Stackoverflow community,

I have a question about keeping the Python keep running to streaming data from Twitter Streaming API.

Below is the basic version of the Twitter Streaming API I used

#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()

Sometimes, the streaming stop working (due to Twitter restart API, or file system, I am not sure), the error is like in the pictures below

StreamAPIError1

StreamAPIError2

My question is that do we have any technique to keep a python file running:
- Like building a second file that test if file 1 is running, then activate file 1 everytime file 1 fails
- Or integrate some techniques in file 1 to make it restart itself
- Or some more suggestion.

Can I have your opinion. It is a lot better if it comes the code

Thank you.

Upvotes: 2

Views: 438

Answers (1)

James Wilson
James Wilson

Reputation: 942

I've used the retrying library to good effect with python Twitter bots.

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

Has code examples, but it's pretty much as simple as using a @retry decorator on your function. EDIT: You could use it on the methods for your class:

@retry
def on_data():
    <code>

NB twitter logs how many calls you make to its API so you have to be careful that this doesn't use it up.

EDIT: From my experiences, if you need to get a tweet from someone who has blocked you, this won't help because it will keep failing, so you'll need to put something in to deal with this.

Upvotes: 1

Related Questions