-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter_mood_server.py
178 lines (147 loc) · 5.08 KB
/
twitter_mood_server.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
from __future__ import division
from urlparse import urlparse
from flask import Flask, render_template
from collections import Iterable
from flask import make_response, request, current_app
from functools import update_wrapper
import pymysql
import json
import os
import colorsys
import datetime
import shapefile
app = Flask(__name__)
app.debug = True
db = urlparse(os.environ['DATABASE_URL'])
mood_lag = 10
sf = shapefile.Reader("states.shp")
def crossdomain(origin=None, methods=None, headers=None,
max_age=21600, attach_to_all=True,
automatic_options=True):
if methods is not None:
methods = ', '.join(sorted(x.upper() for x in methods))
if headers is not None and not isinstance(headers, basestring):
headers = ', '.join(x.upper() for x in headers)
if not isinstance(origin, basestring):
origin = ', '.join(origin)
if isinstance(max_age, datetime.timedelta):
max_age = max_age.total_seconds()
def get_methods():
if methods is not None:
return methods
options_resp = current_app.make_default_options_response()
return options_resp.headers['allow']
def decorator(f):
def wrapped_function(*args, **kwargs):
if automatic_options and request.method == 'OPTIONS':
resp = current_app.make_default_options_response()
else:
resp = make_response(f(*args, **kwargs))
if not attach_to_all and request.method != 'OPTIONS':
return resp
h = resp.headers
h['Content-Type'] = "application/json"
h['Access-Control-Allow-Origin'] = origin
h['Access-Control-Allow-Methods'] = get_methods()
h['Access-Control-Max-Age'] = str(max_age)
if headers is not None:
h['Access-Control-Allow-Headers'] = headers
return resp
f.provide_automatic_options = False
return update_wrapper(wrapped_function, f)
return decorator
def hue2hex(hue, saturation):
if saturation > 1: saturation = 1
orgb = colorsys.hsv_to_rgb(hue, saturation, 1)
rgb = []
for c in orgb:
test = int(c * 256)
if test == 256:
test = test - 1
rgb.append(test)
hx = '#%02x%02x%02x' % (rgb[0], rgb[1], rgb[2])
return hx
def tweetData():
global mood_lag
if db.port:
cnx = pymysql.connect(charset='utf8', host=db.hostname, port=db.port, user=db.username, passwd=db.password, db=db.path[1:])
else:
cnx = pymysql.connect(charset='utf8', host=db.hostname, user=db.username, passwd=db.password, db=db.path[1:])
cursor = cnx.cursor()
tweets = []
rs = "SELECT text, sentiment, date, lat, lng FROM tweets WHERE `date` > %s ORDER BY `date` DESC LIMIT 1000"
lag = datetime.datetime.now() - datetime.timedelta(minutes=mood_lag)
params = [lag.isoformat(' ')]
cursor.execute(rs, params)
for t, s, d, lat, lng in cursor:
tweet = {}
tweet['text'] = t
tweet['sentiment'] = s
tweet['date'] = d.isoformat(' ')
tweet['lat'] = lat
tweet['lng'] = lng
tweets.append(tweet)
cursor.close()
cnx.close()
return json.dumps(tweets)
def calculateMood():
global mood_lag
if db.port:
cnx = pymysql.connect(charset='utf8', host=db.hostname, port=db.port, user=db.username, passwd=db.password, db=db.path[1:])
else:
cnx = pymysql.connect(charset='utf8', host=db.hostname, user=db.username, passwd=db.password, db=db.path[1:])
cursor = cnx.cursor()
mood = dict()
rs = "SELECT state, AVG(sentiment), COUNT(sentiment) FROM tweets WHERE `date` > %s GROUP BY state"
lag = datetime.datetime.now() - datetime.timedelta(minutes=mood_lag)
params = [lag.isoformat(' ')]
cursor.execute(rs, params)
centroid_data = open('static/map.geojson')
centroids = json.load(centroid_data)
for st in sf.shapeRecords():
mood[st.record[31]] = { }
mood[st.record[31]]['mood_score'] = 0
mood[st.record[31]]['color'] = hue2hex(0, 0)
mood[st.record[31]]['count'] = 0
mood[st.record[31]]['sentiment'] = 0
mood[st.record[31]]['std'] = 0
mood[st.record[31]]['centroid'] = centroids[st.record[31]]
mood[st.record[31]]['name'] = st.record[12]
for state, sentiment, count in cursor:
if state in mood:
mood[state]['sentiment'] = sentiment
mood[state]['count'] = count
meanQuery = "SELECT AVG(avg) FROM statistics"
cursor.execute(meanQuery)
avgStat = cursor.fetchone()[0]
stddevQuery = "SELECT state, STDDEV(sentiment) FROM tweets GROUP BY state;"
cursor.execute(stddevQuery)
for state, std in cursor:
if state in mood:
mood[state]['std'] = std
if std != 0:
print avgStat
mood[state]['mood_score'] = (mood[state]['sentiment'] - avgStat)/std
if mood[state]['mood_score'] > 0:
hue = 120/360
else:
hue = 0
else:
mood[state]['mood_score'] = 0
hue = 0
mood[state]['color'] = hue2hex(hue, abs(mood[state]['mood_score']))
cursor.close()
cnx.close()
return json.dumps(mood)
@app.route('/')
@crossdomain(origin='*')
def mood():
return calculateMood()
@crossdomain(origin='*')
@app.route('/map')
def map():
return render_template('twittermap.html')
@app.route('/tweets')
@crossdomain(origin='*')
def tweets():
return tweetData()