-
Notifications
You must be signed in to change notification settings - Fork 90
/
streaming_simple.py
60 lines (44 loc) · 1.82 KB
/
streaming_simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
from auth import TwitterAuth
#Very simple (non-production) Twitter stream example
#1. Download / install python and tweepy (pip install tweepy)
#2. Fill in information in auth.py
#3. Run as: python streaming_simple.py
#4. It will keep running until the user presses ctrl+c to exit
#All output stored to output.json (one tweet per line)
#Text of tweets also printed as recieved (see note about not doing this in production (final) code
class StdOutListener(StreamListener):
#This function gets called every time a new tweet is received on the stream
def on_data(self, data):
#Just write data to one line in the file
fhOut.write(data)
#Convert the data to a json object (shouldn't do this in production; might slow down and miss tweets)
j=json.loads(data)
#See Twitter reference for what fields are included -- https://dev.twitter.com/docs/platform-objects/tweets
text=j["text"] #The text of the tweet
print(text) #Print it out
def on_error(self, status):
print("ERROR")
print(status)
if __name__ == '__main__':
try:
#Create a file to store output. "a" means append (add on to previous file)
fhOut = open("output.json","a")
#Create the listener
l = StdOutListener()
auth = OAuthHandler(TwitterAuth.consumer_key, TwitterAuth.consumer_secret)
auth.set_access_token(TwitterAuth.access_token, TwitterAuth.access_token_secret)
#Connect to the Twitter stream
stream = Stream(auth, l)
#Terms to track
stream.filter(track=["oxford","london","wolverhampton"])
#Alternatively, location box for geotagged tweets
#stream.filter(locations=[-0.530, 51.322, 0.231, 51.707])
except KeyboardInterrupt:
#User pressed ctrl+c -- get ready to exit the program
pass
#Close the
fhOut.close()