Skip to content

Instantly share code, notes, and snippets.

@krishh-konar
Last active March 22, 2016 21:20
Show Gist options
  • Save krishh-konar/7176cda95237e7d231d7 to your computer and use it in GitHub Desktop.
Save krishh-konar/7176cda95237e7d231d7 to your computer and use it in GitHub Desktop.
Print and store streaming tweets
#!/usr/bin/python
'''
Fetches streaming tweets in real time depending on the search query.
'''
import json
from tweepy import OAuthHandler, API
from tweepy.streaming import StreamListener
from tweepy import Stream
import os
## all the 4 required Tokens
CONSUMER_KEY = "your key here"
CONSUMER_SECRET = "your secret here"
ACCESS_TOKEN = "your token here"
ACCESS_TOKEN_SECRET = "your token secret here"
### Custom made listener Class; deals with all the incoming streaming data
class MyListener(StreamListener):
def __init__(self):
pass
# Write all the incoming data in buffer.json file
def on_data(self,data):
try:
with open("tweets.txt","a") as f:
j = json.loads(data)
#See Twitter reference for what fields are included -- https://dev.twitter.com/docs/platform-objects/tweets
line1 = "@" + str(j['user']['screen_name']) + " on " + j['created_at'][:-11] + ", language= "+ j["lang"] + ": "
line2 = '\n' + j['text']
#line3 = '\n Retweets: %d, Favourites: %d' % (j['retweet_count'],j['favorite_count'])
text = line1 + line2
print text + "\n\n"
f.write(text + "\n\n")
except Exception as e:
print "Error: " +str(e)
return True
def on_error(self,status):
print status
return True
def main():
# Authentication for using twitter data
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN,ACCESS_TOKEN_SECRET)
#creating a REST_API instance
api = API(auth)
try:
#creating Streaming_API instance
stream = Stream(auth, MyListener())
selfInfo(api) ## Show self information
#search query
query = searchQuery()
print "\n Tweets: \n\n"
stream.filter(track = [query])
except KeyboardInterrupt:
print '\n Stopped Fetching Tweets.'
exit()
def searchQuery():
raw_query = raw_input('\n Search twitter for: ')
return raw_query
def selfInfo(api):
'''print information about authenticated user '''
user = api.me()
#get window size
columns = os.popen('stty size', 'r').read().split()[1]
print
print "{:^{}}".format("User-Information",columns)
print '{:^{}}'.format("================",columns)
print "\n"+"{:^{}}".format(user.name,columns) + '\n' + "{:^{}}".format("@"+user.screen_name,columns) + '\n ' + "{:^{}}".format(user.description,columns) + "\n\n"
return
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment