-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsumer.py
93 lines (76 loc) · 2.75 KB
/
consumer.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import json
import boto3
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler, Stream
from accounts import KEYWORDS_MAP
twitter_credentials = {
'consumer_key': os.getenv('TWITTER_CONSUMER_KEY'),
'consumer_secret': os.getenv('TWITTER_CONSUMER_SECRET'),
'access_token': os.getenv('TWITTER_ACCESS_TOKEN'),
'access_token_secret': os.getenv('TWITTER_ACCESS_TOKEN_SECRET'),
}
aws_creds = {
'key': os.getenv('AWS_APP_KEY'),
'secret': os.getenv('AWS_APP_SECRET'),
'topic_arn': os.getenv('AWS_TOPIC_ARN')
}
class TweetListener(StreamListener):
""" A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_data(self, data):
print('incoming data')
try:
list_data = json.loads(data)
username = list_data['user']['screen_name']
user_id = list_data['user']['id_str']
tweet = list_data['text'].lower()
print(username)
print(tweet)
tweet_url = 'https://twitter.com/{}/status/{}'.format(
username,
list_data['id_str']
)
except:
print(list_data)
print('Something went wrong with that tweet.')
return
user_keywords = KEYWORDS_MAP.get(user_id)
if not user_keywords:
print('dont have user for user id: ')
print(user_id)
return
if (user_keywords['any'] and any(keyword.lower() in tweet for keyword in user_keywords['any'])) or \
(user_keywords['all'] and all(keyword.lower() in tweet for keyword in user_keywords['all'])):
client = boto3.client(
"sns",
aws_access_key_id=aws_creds['key'],
aws_secret_access_key=aws_creds['secret'],
region_name='us-east-1'
)
print('tweet matched keyword')
msg = '{} tweeted: {} ---- {}'.format(
username,
tweet,
tweet_url,
)
print('sending to sns to send text')
client.publish(Message=msg, TopicArn=aws_creds['topic_arn'])
def on_error(self, status):
print('error: ')
print(status)
if __name__ == '__main__':
print(twitter_credentials)
auth = OAuthHandler(
twitter_credentials['consumer_key'],
twitter_credentials['consumer_secret']
)
auth.set_access_token(
twitter_credentials['access_token'],
twitter_credentials['access_token_secret'],
)
listener = TweetListener()
stream = Stream(auth=auth, listener=listener)
print('authenticated')
stream.filter(follow=KEYWORDS_MAP.keys())