-
Notifications
You must be signed in to change notification settings - Fork 4
/
twitterProducer.py
31 lines (23 loc) · 1015 Bytes
/
twitterProducer.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
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from kafka import KafkaProducer, KafkaClient
from time import sleep
class TwitterListener(StreamListener):
def on_data(self, raw_data):
temp = producer.send("covid-19", raw_data.encode('utf-8'))
producer.flush()
print(raw_data)
return True
def on_error(self, status_code):
print(status_code)
access_token = "1190761539571027970-0jteZBPfxk3J3KCKazmc5vrQEsx1jF"
access_token_secret = "yophKXjSahgcIZs4olBIQ7DGQ9vWQB01wtSwl2Tzv9D0l"
consumer_key = "oVzcEkVrHve3cEfgqAV8VnO1B"
consumer_secret = "mAlyrLi01MUHCG1v3OekClQCnqdSr3PXhPK7fWTXCUYd0oVnl3"
producer = KafkaProducer(bootstrap_servers=['localhost:9092'],api_version=(0,1,0))
t = TwitterListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, t)
stream.filter(track="#COVID")