-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtwitter-streaming.py
51 lines (38 loc) · 1.68 KB
/
twitter-streaming.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
import boto3
import random
import time
import json
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from configparser import SafeConfigParser
parser = SafeConfigParser()
parser.read('api_auth.cfg')
#This is the super secret information that will allow access to the Twitter API and AWS Comprehend
access_token = parser.get('api_tracker', 'access_token')
access_token_secret = parser.get('api_tracker', 'access_token_secret')
consumer_key = parser.get('api_tracker', 'consumer_key')
consumer_secret = parser.get('api_tracker', 'consumer_secret')
aws_key_id = parser.get('api_tracker', 'aws_key_id')
aws_key = parser.get('api_tracker', 'aws_key')
DeliveryStreamName = 'twitter-stream'
client = boto3.client('firehose', region_name='us-west-2',
aws_access_key_id=aws_key_id,
aws_secret_access_key=aws_key
)
#This is a basic listener that just prints received tweets and put them into the stream.
class StdOutListener(StreamListener):
def on_data(self, data):
client.put_record(DeliveryStreamName=DeliveryStreamName,Record={'Data': json.loads(data)["text"]})
msg=json.loads(data)["text"]
print (msg)
return True
def on_error(self, status):
print (status)
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(track=['realdonaldtrump'])