forked from cloudaice/simple-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
297 lines (245 loc) · 11 KB
/
app.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#-*-coding: utf-8-*-
import os
import json
import datetime
from tornado import web
from tornado import gen
from tornado import escape
from tornado.web import asynchronous
from tornado.options import parse_command_line, options, parse_config_file
import tornado.ioloop
import tornado.log
from tornado.websocket import WebSocketHandler
from libs.geo import GeoFetch
from tornado.httpclient import AsyncHTTPClient
github_data = {}
parse_config_file("config.py")
parse_config_file("settings.py")
import workers
class ApiHandler(web.RequestHandler):
def __init__(self, *args, **kwargs):
super(ApiHandler, self).__init__(*args, **kwargs)
super(ApiHandler, self).set_header('Content-Type', 'application/json; charset=UTF-8')
def prepare(self):
"""do something before request comming"""
#options.logger.debug(self.request)
pass
def on_finish(self):
"""do something after response to client like logging"""
#options.logger.debug("finish request.")
pass
class ChinaMapHandler(WebSocketHandler):
handlers = 0
def open(self):
ChinaMapHandler.handlers += 1
options.logger.info("The numbers of chinamaps sockets is %d" %
ChinaMapHandler.handlers)
self.callback = None
message = []
self.write_message(json.dumps(message))
def on_message(self, message):
options.logger.info('recieved message from chinamap')
message = escape.json_decode(message)
self.check(message)
def check(self, message):
china_map = workers.china_map.copy()
for city in china_map:
if china_map[city]['score'] > 0 and china_map[city]['score'] < 5:
china_map[city]['stateInitColor'] = 5
elif china_map[city]['score'] >= 5 and china_map[city]['score'] < 10:
china_map[city]['stateInitColor'] = 4
elif china_map[city]['score'] >= 10 and china_map[city]['score'] < 50:
china_map[city]['stateInitColor'] = 3
elif china_map[city]['score'] >= 50 and china_map[city]['score'] < 100:
china_map[city]['stateInitColor'] = 2
elif china_map[city]['score'] >= 100 and china_map[city]['score'] < 200:
china_map[city]['stateInitColor'] = 1
elif china_map[city]['score'] >= 200:
china_map[city]['stateInitColor'] = 0
if message == china_map:
self.callback = tornado.ioloop.IOLoop.instance().add_timeout(
datetime.timedelta(milliseconds=500),
lambda: self.check(message))
else:
options.logger.info("send message to chinamap...")
self.write_message(json.dumps(china_map))
def on_close(self):
ChinaMapHandler.handlers -= 1
if self.callback:
options.logger.warning("remove chinamap callback")
tornado.ioloop.IOLoop.instance().remove_timeout(self.callback)
class WorldMapHandler(WebSocketHandler):
handlers = 0
def open(self):
WorldMapHandler.handlers += 1
options.logger.info("The numbers of worldmaps sockets is %d " %
WorldMapHandler.handlers)
self.callback = None
message = []
self.write_message(json.dumps(message))
def on_message(self, message):
options.logger.info("recieved message from worldmap")
message = escape.json_decode(message)
self.check(message)
def check(self, message):
world_map = workers.world_map.copy()
for country_code in world_map:
if world_map[country_code]["score"] > 0 and world_map[country_code]["score"] < 5:
world_map[country_code]["stateInitColor"] = 5
elif world_map[country_code]["score"] >= 5 and world_map[country_code]["score"] < 10:
world_map[country_code]["stateInitColor"] = 4
elif world_map[country_code]["score"] >= 10 and world_map[country_code]["score"] < 50:
world_map[country_code]["stateInitColor"] = 3
elif world_map[country_code]["score"] >= 50 and world_map[country_code]["score"] < 100:
world_map[country_code]["stateInitColor"] = 2
elif world_map[country_code]["score"] >= 100 and world_map[country_code]["score"] < 200:
world_map[country_code]["stateInitColor"] = 1
elif world_map[country_code]["score"] >= 200:
world_map[country_code]["stateInitColor"] = 0
if message == world_map:
self.callback = tornado.ioloop.IOLoop.instance().add_timeout(
datetime.timedelta(milliseconds=500),
lambda: self.check(message))
else:
options.logger.info("send message to worldmap")
self.write_message(json.dumps(world_map))
def on_close(self):
WorldMapHandler.handlers -= 1
if self.callback:
options.logger.warning("remove worldmap timeout...")
tornado.ioloop.IOLoop.instance().remove_timeout(self.callback)
class GithubChinaHandler(ApiHandler):
@asynchronous
@gen.coroutine
def post(self):
self.write(json.dumps(workers.github_china, indent=4, separators=(',', ': ')))
self.finish()
class GithubWorldHandler(ApiHandler):
@asynchronous
@gen.coroutine
def post(self):
self.write(json.dumps(workers.github_world, indent=4, separators=(',', ': ')))
self.finish()
class MainHandler(web.RequestHandler):
@asynchronous
def get(self):
self.render("index.html")
class AboutHandler(web.RequestHandler):
@asynchronous
def get(self):
self.render("about.html")
class ChinaSocketbHandler(WebSocketHandler):
handlers = 0
def open(self):
ChinaSocketbHandler.handlers += 1
options.logger.info(" The numbers of china sockets is %d" %
ChinaSocketbHandler.handlers)
self.callback = None
self.write_message(json.dumps(workers.github_china))
def on_message(self, message):
options.logger.info('recieved message from china')
message = escape.json_decode(message)
self.check(message)
def check(self, message):
if message == workers.github_china:
self.callback = tornado.ioloop.IOLoop.instance().add_timeout(
datetime.timedelta(milliseconds=500),
lambda: self.check(message))
else:
options.logger.info("send message to china...")
self.write_message(json.dumps(workers.github_china))
def on_close(self):
ChinaSocketbHandler.handlers -= 1
if self.callback:
options.logger.warning("remove china users callback")
tornado.ioloop.IOLoop.instance().remove_timeout(self.callback)
class WorldSocketbHandler(WebSocketHandler):
handlers = 0
def open(self):
WorldSocketbHandler.handlers += 1
options.logger.info("The numbers of world sockets is %d" %
WorldSocketbHandler.handlers)
self.callback = None
self.write_message(json.dumps(workers.github_world))
def on_message(self, message):
options.logger.info("recieved message from world")
message = escape.json_decode(message)
self.check(message)
def check(self, message):
if message == workers.github_world:
self.callback = tornado.ioloop.IOLoop.instance().add_timeout(
datetime.timedelta(milliseconds=500),
lambda: self.check(message))
else:
options.logger.info("send message to world...")
self.write_message(json.dumps(workers.github_world))
def on_close(self):
WorldSocketbHandler.handlers -= 1
if self.callback:
options.logger.warning("remove world users callback")
tornado.ioloop.IOLoop.instance().remove_timeout(self.callback)
class WorldMapAjaxHandler(ApiHandler):
@asynchronous
def post(self):
world_map = workers.world_map.copy()
for country_code in world_map:
if world_map[country_code]["score"] > 0 and world_map[country_code]["score"] < 5:
world_map[country_code]["stateInitColor"] = 5
elif world_map[country_code]["score"] >= 5 and world_map[country_code]["score"] < 10:
world_map[country_code]["stateInitColor"] = 4
elif world_map[country_code]["score"] >= 10 and world_map[country_code]["score"] < 50:
world_map[country_code]["stateInitColor"] = 3
elif world_map[country_code]["score"] >= 50 and world_map[country_code]["score"] < 100:
world_map[country_code]["stateInitColor"] = 2
elif world_map[country_code]["score"] >= 100 and world_map[country_code]["score"] < 200:
world_map[country_code]["stateInitColor"] = 1
elif world_map[country_code]["score"] >= 200:
world_map[country_code]["stateInitColor"] = 0
self.write(json.dumps(world_map, indent=4, separators=(',', ': ')))
self.finish()
class ChinaMapAjaxHandler(ApiHandler):
@asynchronous
def post(self):
china_map = workers.china_map.copy()
for city in china_map:
if china_map[city]['score'] > 0 and china_map[city]['score'] < 5:
china_map[city]['stateInitColor'] = 5
elif china_map[city]['score'] >= 5 and china_map[city]['score'] < 10:
china_map[city]['stateInitColor'] = 4
elif china_map[city]['score'] >= 10 and china_map[city]['score'] < 50:
china_map[city]['stateInitColor'] = 3
elif china_map[city]['score'] >= 50 and china_map[city]['score'] < 100:
china_map[city]['stateInitColor'] = 2
elif china_map[city]['score'] >= 100 and china_map[city]['score'] < 200:
china_map[city]['stateInitColor'] = 1
elif china_map[city]['score'] >= 200:
china_map[city]['stateInitColor'] = 0
self.write(json.dumps(china_map, indent=4, separators=(',', ': ')))
self.finish()
settings = {
"static_path": os.path.join(os.path.dirname(__file__), 'static'),
'template_path': os.path.join(os.path.dirname(__file__), 'template'),
"debug": options.debug
}
handlers = [
(r"/", MainHandler),
(r"/socketchina", ChinaSocketbHandler),
(r"/socketworld", WorldSocketbHandler),
(r"/githubchina", GithubChinaHandler),
(r"/githubworld", GithubWorldHandler),
(r"/chinamap", ChinaMapHandler),
(r"/worldmap", WorldMapHandler),
(r"/worldmapajax", WorldMapAjaxHandler),
(r"/chinamapajax", ChinaMapAjaxHandler),
(r"/about", AboutHandler),
(r"/favicon.ico", web.StaticFileHandler, dict(path=settings["static_path"])),
]
app = web.Application(handlers, **settings)
workers.update_china_user()
workers.update_world_user()
workers.update_china_location()
workers.update_world_location()
if __name__ == "__main__":
parse_command_line()
app.listen(options.port)
tornado.ioloop.IOLoop.instance().start()