-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtweepy_kafka_producer.py
More file actions
47 lines (38 loc) · 1.17 KB
/
Copy pathtweepy_kafka_producer.py
File metadata and controls
47 lines (38 loc) · 1.17 KB
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
'''
Author: Prakhar Mishra
Date: 9/01/2016
'''
# Importing Packages
import sys
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from kafka import KafkaClient
from kafka import SimpleProducer
from kafka import SimpleConsumer
# Twitter Credentials
atoken = "< access token >"
asecret = "< access token secret >"
ckey = "< consumer key >"
csecret = "< consumer secret >"
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
class listener(StreamListener):
# When data
def on_data(self, data):
try:
# 1st parameter is the kafka topic
producer.send_messages("socialcops", str(data))
print 'Sent to producer'
except Exception as e:
print e
# When error
def on_error(self, status):
print status
# Create a stream of data
twitterStream = Stream(auth,listener())
if __name__ == "__main__":
# Create producer and set filters on stream
kafka = KafkaClient("localhost:9092")
producer = SimpleProducer(kafka)
twitterStream.filter(languages = ['en'], track=['the','i','to','an','and','is','e','a','u','o'])