0

I'm relatively new to programming but just trying to get a feel with working for apis/data by pulling tweets from twitter using the Twython twitter wrapper. every time I do this I get the following error messages around 5,000 tweets. I can use the streaming with other wrappers, like python-twitter and get much farther ~800,000 tweets without a similar error.

--------------------------------------------------------------------------
ChunkedEncodingError                      Traceback (most recent call last)
<ipython-input-5-fdcf34f23648> in <module>()
      1 #[stream.statuses.filter(track='twitter')]
----> 2 stream.statuses.sample()

/Users/myusername/anaconda/lib/python2.7/site-packages/twython/streaming/types.pyc in sample(self, **params)
     75         url = 'https://stream.twitter.com/%s/statuses/sample.json' \
     76               % self.streamer.api_version
---> 77         self.streamer._request(url, params=params)
     78 
     79     def firehose(self, **params):

/Users/myusername/anaconda/lib/python2.7/site-packages/twython/streaming/api.pyc in _request(self, url, method, params)
    132             response = _send(retry_counter)
    133 
--> 134             for line in response.iter_lines(self.chunk_size):
    135                 if not self.connected:
    136                     break

/Users/myusername/anaconda/lib/python2.7/site-packages/requests/models.pyc in iter_lines(self, chunk_size, decode_unicode)
    643 
    644         for chunk in self.iter_content(chunk_size=chunk_size,
--> 645                                        decode_unicode=decode_unicode):
    646 
    647             if pending is not None:

/Users/myusername/anaconda/lib/python2.7/site-packages/requests/models.pyc in generate()
    616                         yield chunk
    617                 except IncompleteRead as e:
--> 618                     raise ChunkedEncodingError(e)
    619             except AttributeError:
    620                 # Standard file-like object.

ChunkedEncodingError: IncompleteRead(0 bytes read, 1 more expected)

The code I'm using to generate this follows. I'm sure it's missing a lot including a way to handle this particular exception.

from twython import TwythonStreamer
import numpy as np
import pandas as pd
from requests.exceptions import ChunkedEncodingError

counter = 0
class MyStreamer(TwythonStreamer):
    def on_success(self, data):
        global counter
        #if 'text' in data:
            #print data['text'].encode('utf-8')
        counter+=1
        print counter

    def on_error(self, status_code, data):
        print status_code

        # Want to stop trying to get data because of the error?
        # Uncomment the next line!
        # self.disconnect()

stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.sample()

Basically, right now I'm not even trying to do something with the tweets because I just want to see if I can get this to work without the error.

imightlose
  • 15
  • 4

1 Answers1

1

Well, I didn't figure out what's causing the error, but if I change the last lines of code it can work. Like this:

def streamed():
    while True:
        try:
            stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
            stream.statuses.filter(track='twitter', stall_warnings=True)
        except  ChunkedEncodingError:
            continue

streamed()

This gets the streamer going again when it errors. I also had to add

return True

to the functions in the mystreamer class.

This answer also helped from before: How to restart tweepy script in case of error?

Community
  • 1
  • 1
imightlose
  • 15
  • 4
  • 1
    According to a discussion on the Twython Github page, this likely indicates that your process is falling behind and Twitter is resetting the queue. So while your solution will work, you are likely missing some tweets from the stream. See https://github.com/ryanmcgrath/twython/issues/288 – Martin Jan 04 '15 at 15:00