Skip to content

Instantly share code, notes, and snippets.

@hugobowne
Last active July 4, 2026 11:26
Show Gist options
  • Select an option

  • Save hugobowne/18f1c0c0709ed1a52dc5bcd462ac69f4 to your computer and use it in GitHub Desktop.

Select an option

Save hugobowne/18f1c0c0709ed1a52dc5bcd462ac69f4 to your computer and use it in GitHub Desktop.
NOTE: this code is for a previous version of the Twitter API and I will not be updating in the near future. If someone else would like to, I'd welcome that! Feel free to ping me. END NOTE. Here I define a Tweet listener that creates a file called 'tweets.txt', collects streaming tweets as .jsons and writes them to the file 'tweets.txt'; once 100…
class MyStreamListener(tweepy.StreamListener):
def __init__(self, api=None):
super(MyStreamListener, self).__init__()
self.num_tweets = 0
self.file = open("tweets.txt", "w")
def on_status(self, status):
tweet = status._json
self.file.write( json.dumps(tweet) + '\n' )
self.num_tweets += 1
if self.num_tweets < 100:
return True
else:
return False
self.file.close()
def on_error(self, status):
print(status)
@itsShauns

Copy link
Copy Markdown

I've dealt with similar integrations, and one thing that really helped was adding proper error handling for API failures and rate limits. Keeping detailed logs also made troubleshooting much easier. After making a few small optimizations on a website integration, the whole setup became far more reliable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment