-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetStream.py
183 lines (146 loc) · 6.49 KB
/
tweetStream.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from __future__ import print_function
from random import uniform
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from http.client import IncompleteRead
import boto3
import json
import datetime
import time
import json
import sys
import geocoder
import sys
import urllib3
reload(sys)
sys.setdefaultencoding('utf8')
access_token = ''
access_token_secret = ''
consumer_key = ''
consumer_secret = ''
print('Loading function')
def tweet_stream(event, context):
def GoogGeoAPI(address,api=""):
g = geocoder.google(str(address))
if(g):
finList = g[0].latlng
else:
y, x = uniform(-120,120), uniform(-40,70)
# Generating random co-ordinates
finList = [x,y]
return finList
print("Received event: " + json.dumps(event, indent=2))
current_time = datetime.datetime.now().time()
data = json.loads(event['Records'][0]['Sns']['Message'])
topic = topic['topic']
try:
message = json.loads(event['Records'][0]['Sns']['Message'])
topic = message['topic']
except Exception as e:
print('Error in fetching data')
topic = 'Blockchain'
'''
Code for Debugging
doc = {
'text': "Aakar",
'handle': "Hui",
'id': "12354",
'latitude': "123",
'longitude':"234",
'time' : str(datetime.datetime.now().replace(microsecond=0).isoformat()),
'topic' : topic
}
response = queue.send_message(MessageBody=json.dumps(doc), MessageGroupId='509148512136', MessageDeduplicationId=str(doc['id']))
'''
class StdOutListener(StreamListener):
def __init__(self, time_limit=40):
self.start_time = time.time()
self.limit = time_limit
self.sqs = boto3.resource('sqs')
self.queue = self.sqs.get_queue_by_name(QueueName='tweetqueue.fifo')
self.sns = boto3.client('sns',aws_access_key_id='',aws_secret_access_key='',region_name='us-east-2')
def on_data(self, data):
data_dict = json.loads(data)
if (time.time() - self.start_time) < self.limit:
print ('Here is a new tweet!')
try:
if 'user' in data_dict.keys():
if data_dict['coordinates']:
longitude, latitude = data_dict['coordinates']['coordinates']
doc = {
'text': data_dict['text'],
'handle': data_dict['user']['screen_name'],
'id': data_dict['id'],
'latitude': latitude,
'longitude': longitude,
'time' : str(datetime.datetime.now().replace(microsecond=0).isoformat())
}
print(doc)
try:
#Adding the data to SQS queue
response = self.queue.send_message(MessageBody=json.dumps(doc), MessageGroupId='509148512136', MessageDeduplicationId=str(data_dict['id']))
except Exception as e:
print('Error While adding to SQS OR Tweet Parsing Error')
snsResponse = {'status' : 'OK' }
try:
#Triggering SNS for Sentiment Analysis, not sending tweets from here
self.sns.publish(
TargetArn ='arn:aws:sns:us-east-2:509148512136:sqsPoll',
Message=json.dumps({'default': json.dumps(snsResponse)}),
MessageStructure = 'json'
)
except Exception as e:
print('Error while SNS - 4 - 1', e)
elif data_dict['user']['location']:
location = GoogGeoAPI(data_dict['user']['location'])
doc = {
'text': data_dict['text'],
'handle': data_dict['user']['screen_name'],
'id': data_dict['id'],
'latitude': location[0],
'longitude': location[1],
'time' : str(datetime.datetime.now().replace(microsecond=0).isoformat())
}
print(doc)
try:
#Adding the data to SQS queue
response = self.queue.send_message(MessageBody=json.dumps(doc), MessageGroupId='509148512136', MessageDeduplicationId=str(data_dict['id']))
except Exception as e:
print('Error While adding to SQS OR Tweet Parsing Error -- Geo 2')
snsResponse = {'status' : 'OK'}
try:
#Triggering SNS for Sentiment Analysis, not sending tweets from here
self.sns.publish(
TargetArn ='arn:aws:sns:us-east-2:509148512136:sqsPoll',
Message=json.dumps({'default': json.dumps(snsResponse)}),
MessageStructure = 'json'
)
except Exception as e:
print('Error while SNS - 4 - 2', e)
return True
else:
return False
except Exception:
print("Error - Incomplete Read")
return False
else:
return False
def on_error(self, status):
print (status)
print ('Error occured')
try:
# converting the word to search for into list
topics = topic.split()
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(track = topics)
except Exception as e:
print("Exception occured Twiter Calling",e)
'''
if __name__ == '__main__':
event = json.dumps({'body' : { 'search' : 'Apple'} }
mytest = tweet_stream(event,"b")
'''