Skip to content

Instantly share code, notes, and snippets.

@Assios
Last active March 14, 2017 10:26
Show Gist options
  • Save Assios/f93abd017d6b5e4d25a87409d01fd4a9 to your computer and use it in GitHub Desktop.
Save Assios/f93abd017d6b5e4d25a87409d01fd4a9 to your computer and use it in GitHub Desktop.
Twitter streaming API example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
import re
import datetime
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
date = datetime.date.today().strftime("%B_%d_%Y")
file_ = open(date.lower() + ".json", "a")
tweets = []
class StdOutListener(StreamListener):
def on_data(self, data):
if len(tweets)>100000:
return False
try:
tweet = json.loads(data)
if "created_at" in tweet and tweet["lang"]=="en":
tweets.append(json.loads(data))
file_.write(data)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
def on_error(self, status):
print status
return True
class twitterStats():
file_.read
if __name__ == '__main__':
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.sample()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment