This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtor.py
59 lines (49 loc) · 1.67 KB
/
tor.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
from datetime import datetime
import os
import os.path
from tornado import autoreload
import tornado.ioloop
import tornado.web
import json
import crawler
FROM = datetime(2000, 1, 1)
TO = datetime(2100, 1, 1)
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self, htag):
c = crawler.Crawler()
c.crawl_tweets([htag])
tweets = c.find_tweets(htag, FROM, TO)
self.write(json.dumps({'htag': htag, 'count': len(tweets), 'results': list(tweets)}))
self.finish()
class GraphHandler(tornado.web.RequestHandler):
def get(self, htag):
c = crawler.Crawler()
data = c.graph_data(htag, FROM, TO)
# data = [[161.2, 51.6], [167.5, 59.0], [159.5, 49.2], [157.0, 63.0], [155.8, 53.6]]
self.write(json.dumps(data))
class HomeHandler(tornado.web.RequestHandler):
def get(self):
c = crawler.Crawler()
htags = c.htags()
self.render('index.html', **{'htags': json.dumps(sorted(htags))})
static_path = 'static/htdocs/'
settings = {
'template_path': os.path.join(os.path.dirname(__file__), "templates"),
'xsrf_cookies': True,
'static_path': os.path.join(os.path.dirname(__file__), static_path),
}
import tornado.options
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", HomeHandler),
(r"/api/v.1/([^/]+)", MainHandler),
(r"/api/graph/([^/]+)", GraphHandler),
], **settings)
if __name__ == "__main__":
application.listen(8888)
ioloop = tornado.ioloop.IOLoop.instance()
tornado.autoreload.watch(static_path)
tornado.autoreload.watch('templates/index.html')
tornado.autoreload.start(ioloop)
ioloop.start()