-
Notifications
You must be signed in to change notification settings - Fork 4
/
searchapi.py
72 lines (56 loc) · 1.94 KB
/
searchapi.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
#coding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os.path
import tornado.auth
import tornado.escape
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.autoreload
from CrawlerDriver import CrawlerDriver
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
define("tencent_username", help="腾讯微博用户名",
default=None)
define("tencent_password", help="腾讯微博密码",
default=None)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/test', TestHandler),
(r'/weibos', WeibosHandler),
]
settings = dict(
xsrf_cookies=True,
)
tornado.web.Application.__init__(self, handlers, **settings)
self.crawl_driver = CrawlerDriver()
class BaseHandler(tornado.web.RequestHandler):
@property
def crawl_driver(self):
return self.application.crawl_driver
class TestHandler(BaseHandler):
def get(self):
self.write('Hello World')
class WeibosHandler(BaseHandler):
def get(self):
weibo_type = self.get_argument('site', '新浪')
page = self.get_argument('page', 1)
keyword = self.get_argument('kw', '比特币')
count = self.get_argument('limit', 10)
self.crawl_driver.setWeiboCrawler(weibo_type.encode('utf8'), options.tencent_username, options.tencent_password)
result = self.crawl_driver.getWeibos(keyword, int(page), int(count))
self.write(tornado.escape.json_encode(result))
def main():
tornado.options.parse_command_line()
application = Application()
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
io_loop = tornado.ioloop.IOLoop.instance()
tornado.autoreload.start(io_loop)
io_loop.start()
if __name__ == '__main__':
main()