esdalmaijer / markovbot

Library to generate text with a Markov chain, with support to read and post updates to Twitter accounts.
http://www.pygaze.org/2016/03/how-to-code-twitter-bot/
GNU General Public License v3.0
141 stars 64 forks source link

TwitterStream can disconnect #1

Closed esdalmaijer closed 8 years ago

esdalmaijer commented 8 years ago

The Twitter library will sometimes return {'hangup':True} instead of a typical tweet dictionary. This occurs on a Stream SSLError (see this commit in the twitter library). Calling on an iterator after this disconnect will cause a StopIteration exception.

This will crash the _autoreplythread, which assumes certain keys will be in the tweet dict. In addtion, it relies on a working TwitterStream. Two changes are necessary to fix this:

  1. Check before line 690 whether 'hangup' in tweet.keys(). If it is, the TwitterStream should be restarted (see change 2), and the current tweet should not be processed.
  2. Add a _reanimate method to restart the Twitter connection. To be safe, restart both _t (Twitter login) and _ts (TwitterStream).

NOTE: A call to _reanimate should also be added to _autotweet's try-except for posting a new Tweet.

esdalmaijer commented 8 years ago

This commit should fix this issue. If it doesn't, it'll be because the OAuth expired. The solution to that would be to store the access tokens, but that would be a bit iffy... (We'll worry about that when the time comes.)