Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

python - How to restart tweepy script in case of error?

I have a python script that continuously stores tweets related to tracked keywords to a file. However, the script tends to crash repeatedly due to an error appended below. How do I edit the script so that it automatically restarts? I've seen numerous solutions including this (Restarting a program after exception) but I'm not sure how to implement it in my script.

import sys
import tweepy
import json
import os

consumer_key=""
consumer_secret=""
access_key = ""
access_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
# directory that you want to save the json file
os.chdir("C:Usersjson_files")
# name of json file you want to create/open and append json to
save_file = open("12may.json", 'a')

class CustomStreamListener(tweepy.StreamListener):
    def __init__(self, api):
        self.api = api
        super(tweepy.StreamListener, self).__init__()

        # self.list_of_tweets = []

    def on_data(self, tweet):
        print tweet
        save_file.write(str(tweet))

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream
        print "Stream restarted"

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream
        print "Stream restarted"

sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
sapi.filter(track=["test"])

===========================================================================

Traceback (most recent call last):
  File "C:Usersweets_to_json.py", line 41, in <module>
    sapi.filter(track=["test"])
  File "C:Python27libsite-packagesweepy-2.3-py2.7.eggweepystreaming.py", line 316, in filter
    self._start(async)
  File "C:Python27libsite-packagesweepy-2.3-py2.7.eggweepystreaming.py", line 235, in _start
    self._run()
  File "C:Python27libsite-packagesweepy-2.3-py2.7.eggweepystreaming.py", line 165, in _run
    self._read_loop(resp)
  File "C:Python27libsite-packagesweepy-2.3-py2.7.eggweepystreaming.py", line 206, in _read_loop
    for c in resp.iter_content():
  File "C:Python27libsite-packages
equests-1.2.3-py2.7.egg
equestsmodels.py", line 541, in generate
    chunk = self.raw.read(chunk_size, decode_content=True)
  File "C:Python27libsite-packages
equests-1.2.3-py2.7.egg
equestspackagesurllib3
esponse.py", line 171, in read
    data = self._fp.read(amt)
  File "C:Python27libhttplib.py", line 543, in read
    return self._read_chunked(amt)
  File "C:Python27libhttplib.py", line 603, in _read_chunked
    value.append(self._safe_read(amt))
  File "C:Python27libhttplib.py", line 660, in _safe_read
    raise IncompleteRead(''.join(s), amt)
IncompleteRead: IncompleteRead(0 bytes read, 1 more expected)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Figured out how to incorporate the while/try loop by writing a new function for the stream:

def start_stream():
    while True:
        try:
            sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
            sapi.filter(track=["Samsung", "s4", "s5", "note" "3", "HTC", "Sony", "Xperia", "Blackberry", "q5", "q10", "z10", "Nokia", "Lumia", "Nexus", "LG", "Huawei", "Motorola"])
        except: 
            continue

start_stream()

I tested the auto restart by manually interrupting the program with CMD + C. Nonetheless, happy to hear of better ways to test such functionality.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...