Created
May 11, 2016 07:13
-
-
Save drhanlau/a1de9ee9a9128e6440fa5f4fcb730b7e to your computer and use it in GitHub Desktop.
Twitter Streaming Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import, print_function | |
from tweepy.streaming import StreamListener | |
from tweepy import OAuthHandler | |
from tweepy import Stream | |
import json | |
import sys,os,time | |
print(sys.version) | |
# Go to http://apps.twitter.com and create an app. | |
# The consumer key and secret will be generated for you after | |
#consumer_key="6K5SM8NeRGxXZykMewMQ" | |
#consumer_secret="volXxEciGYDzcoIwI2x84e9GDrPUYIB4cBr7LdYkLQ" | |
consumer_key = "3ujYvf8PTx5FkWaedUrLOl3V6" | |
consumer_secret="z6vr75YFRgAmfJJQktEN2xPrBXPyl85WnmOhgMLlNzLIjXmAG5" | |
# After the step above, you will be redirected to your app's page. | |
# Create an access token under the the "Your access token" section | |
access_token="18099296-JYERdEn1Pc8yubrivrVV1DsXesVGOJLszPw3iFg4G" | |
access_token_secret="nqVTUo3HhoKFor4QPuP2zeysBfrMaDONWUVQkIdGD2fRz" | |
class StdOutListener(StreamListener): | |
""" A listener handles tweets are the received from the stream. | |
This is a basic listener that just prints received tweets to stdout. | |
""" | |
def on_data(self, data): | |
j = json.loads(data) | |
lang = j["lang"] #If we only want English tweet | |
if not lang == "en": | |
return True | |
user_id = j["user"]["id"] | |
screen_name = j["user"]["screen_name"] | |
text = j['text'] | |
tweet_id = j["id"] | |
created_at=j["created_at"] | |
ts = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(j['created_at'],'%a %b %d %H:%M:%S +0000 %Y')) | |
#print (screen_name, text) | |
print(text) | |
def on_error(self, status): | |
print(status) | |
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.filter(track=['trump']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment