-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
32 lines (27 loc) · 1.02 KB
/
worker.py
File metadata and controls
32 lines (27 loc) · 1.02 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
import boto3
import json
from watson_developer_cloud import AlchemyLanguageV1
from jsonmerge import merge
alchemy_language = AlchemyLanguageV1(api_key='a478af3840b5d6cef311a1b6dbab00ce879e03d3')
# Get the service resource
sqs = boto3.resource('sqs')
# Get the queue
queue = sqs.get_queue_by_name(QueueName='twittmap')
client = boto3.client('sns')
while True:
# Process messages by printing out body
for message in queue.receive_messages(MaxNumberOfMessages=10):
# Print out the body
try:
tweet = json.loads(message.body)
notification_message = merge(tweet, alchemy_language.sentiment(text=tweet['text'])['docSentiment'])
response = client.publish(
TopicArn='arn:aws:sns:us-east-1:950596917281:twittmap',
Message=json.dumps(notification_message),
Subject='New Tweet'
)
print(response)
except:
pass
# Let the queue know that the message is processed
message.delete()