-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproducer.py
59 lines (45 loc) · 1.62 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import tweepy
from kafka import KafkaProducer
import logging
import json
from decouple import config
"""API ACCESS KEYS"""
consumerKey = config('CONSUMERKEY')
consumerSecret = config('CONSUMERSECRET')
accessToken = config('ACCESSTOKEN')
accessTokenSecret = config('ACCESSTOKENSECRET')
bearerToken = config('BEARERTOKEN')
logging.basicConfig(level=logging.INFO)
producer = KafkaProducer(bootstrap_servers='localhost:9092')
search_term = 'ChatGPT'
topic_name = 'twitter'
def twitterAuth():
# create the authentication object
authenticate = tweepy.OAuthHandler(consumerKey, consumerSecret)
# set the access token and the access token secret
authenticate.set_access_token(accessToken, accessTokenSecret)
authenticate.secure = True
# create the API object
api = tweepy.API(authenticate, wait_on_rate_limit=True)
return api
class TweetListener(tweepy.StreamingClient):
def on_data(self, raw_data):
logging.info(raw_data)
tweet = json.loads(raw_data)
if tweet['data']:
data = {
'message': tweet['data']['text'].replace(',', '')
}
producer.send(topic_name, value=json.dumps(data).encode('utf-8'))
return True
@staticmethod
def on_error(status_code):
if status_code == 420:
# returning False in on_data disconnects the stream
return False
def start_streaming_tweets(self, search_term):
self.add_rules(tweepy.StreamRule(search_term))
self.filter()
if __name__ == '__main__':
twitter_stream = TweetListener(bearerToken)
twitter_stream.start_streaming_tweets(search_term)