-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_python_twitter.py
77 lines (66 loc) · 2.54 KB
/
script_python_twitter.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
import boto3
import time
#Variables that contains the user credentials to access Twitter API
consumer_key = ''
consumer_secret =''
access_token = ''
access_token_secret = ''
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
tweet = json.loads(data)
try:
if 'text' in tweet.keys():
#print (tweet['text'])
message_lst = [str(tweet['id']),
str(tweet['user']['name']),
str(tweet['user']['screen_name']),
tweet['text'].replace('\n',' ').replace('\r',' '),
str(tweet['user']['followers_count']),
str(tweet['user']['location']),
str(tweet['geo']),
str(tweet['created_at']),
'\n'
]
message = '\t'.join(message_lst)
print(message)
client.put_record(
DeliveryStreamName=delivery_stream,
Record={
'Data': message
}
)
except (AttributeError, Exception) as e:
print (e)
return True
def on_error(self, status):
print (status)
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
listener = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#tweets = Table('tweets_ft',connection=conn)
client = boto3.client('firehose',
region_name='us-east-1',
aws_access_key_id='',
aws_secret_access_key=''
)
delivery_stream = 'twitter_stream'
#This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
#stream.filter(track=['trump'], stall_warnings=True)
while True:
try:
print('Twitter streaming...')
stream = Stream(auth, listener)
stream.filter(track=['giletsjaunes'], languages=['fr','en'], stall_warnings=True)
except Exception as e:
print(e)
print('Disconnected...')
time.sleep(5)
continue