-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
90 lines (64 loc) · 2.49 KB
/
application.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
from flask import Flask, render_template, request
from flask_socketio import SocketIO, send, emit
import json
import requests
application = Flask(__name__)
socketio = SocketIO(application)
socketConnected = False
@application.route('/', methods=['GET', 'POST'])
def hello_world():
global socketConnected
#Receiving AWS SNS
if request.method == 'POST':
try:
js = json.loads(request.data)
except:
pass
print js
hdr = request.headers.get('X-Amz-Sns-Message-Type')
# Subscribe to the SNS topic
if hdr == 'SubscriptionConfirmation' and 'SubscribeURL' in js:
r = requests.get(js['SubscribeURL'])
if hdr == 'Notification':
tweet = js['Message']
print tweet
postURL = 'ENDPOINT/tweetmap/tweet'
r = requests.post(postURL, json = tweet)
if socketConnected:
socketio.emit('realTimeResponse', tweet)
return render_template('TwitterMap.html')
@application.route('/search')
def handle_search():
return render_template('searchPage.html')
@socketio.on('realTime')
def handle_realtime_event(message):
global socketConnected
socketConnected = True
queryURL = 'ENDPOINT/tweetmap/_search?q=*:*&size=10000'
response = requests.get(queryURL)
results = json.loads(response.text)
tweets = []
for result in results['hits']['hits']:
tweet = {'sentiment': result['_source']['sentiment'], 'longitude': result['_source']['longitude'],
'latitude': result['_source']['latitude']}
tweets.append(tweet)
send(json.dumps(tweets))
#Search tweets from elastic search to display on the front end
@socketio.on('message')
def handle_message(message):
if message == 'Init':
queryURL = 'ENDPOINT/tweetmap/_search?q=*:*&size=10000'
response = requests.get(queryURL)
results = json.loads(response.text)
else:
queryKeyWord = message.replace(' ', '%20')
queryURL = 'ENDPOINT/tweetmap/_search?q=' + queryKeyWord + '&size=10000'
response = requests.get(queryURL)
results = json.loads(response.text)
tweets = []
for result in results['hits']['hits']:
tweets.append({'sentiment': result['_source']['sentiment'], 'longitude': result['_source']['longitude'],
'latitude': result['_source']['latitude']})
send(json.dumps(tweets))
if __name__ == '__main__':
socketio.run(application, host='0.0.0.0' debug=True)