forked from sfu-dataminers/cmpt733-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka_producer.py
executable file
·43 lines (30 loc) · 1.12 KB
/
kafka_producer.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
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import config_file as conf
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='localhost:9092')
topic_name = "dataminers"
class twitterAuth():
"""SET UP TWITTER AUTHENTICATION"""
def authenticateTwitterApp(self):
auth = OAuthHandler(conf.consumer_key,conf.consumer_secret)
auth.set_access_token(conf.access_token,conf.access_token_secret)
return auth
class TwitterStreamer():
"""SET UP STREAMER"""
def __init__(self):
self.twitterAuth = twitterAuth()
def stream_tweets(self):
while True:
listener = ListenerTS()
auth = self.twitterAuth.authenticateTwitterApp()
stream = Stream(auth, listener)
stream.filter(track=["@himalyabachwani"], stall_warnings=True, languages= ["en"])
class ListenerTS(StreamListener):
def on_data(self, raw_data):
producer.send(topic_name, str.encode(raw_data))
return True
if __name__ == "__main__":
TS = TwitterStreamer()
TS.stream_tweets()