-
Notifications
You must be signed in to change notification settings - Fork 2
/
result_handler.py
115 lines (83 loc) · 3.96 KB
/
result_handler.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tornado.web
import tornado_mysql
from sphinx_client import *
from setting import *
sql_cmd = 'SELECT ' \
'post.tid, post.title, post.fid, post.uid, post.user, post.post_time, post_content.content ' \
'FROM post LEFT JOIN post_content ON post.tid = post_content.tid WHERE post.tid IN (%s)' \
'ORDER BY FIND_IN_SET(post.tid, "%s")'
fid_str_nu = {'新作资源': '50', '游戏音乐': '4', '动画音乐': '5', '同人音乐': '42',
'综合音乐': '8', '广播剧': '25', 'Hi-Res自购资源交流': '52', 'Hi-Res&Hi-Fi讨论': '53',
'EX咖喱版': '49'}
fid_param_to_str = {'new': NEW_PRODUCTION, 'game': GAME_MUSIC, 'anime': ANIME_MUSIC, 'doujin': DOUJIN_MUSIC,
'comp': COMPOSITE_MUSIC, 'radio': RADIO, 'hi1': HI_RES_1, 'hi2': HI_RES_2,
'ex': EX}
excerpt_opts = {'before_match': '<em>', 'after_match': '</em>', 'limit_words': 300}
class ResultHandler(tornado.web.RequestHandler):
"""
处理搜索请求,查询sphinx
"""
def initialize(self):
self.__re_html = re.compile(r'<[^>]+>', re.S)
@tornado.gen.coroutine
def get(self):
key_word = self.get_argument('key', '').encode('utf-8')
pn = self._check_argument_pn(self.get_argument('pn', 1))
ex = self.get_argument('ex', '')
fid = self.__check_fid(self.get_argument('fid', 'all').encode('utf-8'))
if len(key_word) < 2:
self.write('关键字长度必须大于两个字符!!!')
return
# sphinx client
cl = AstostSphinxClient()
cl.set_filter_fid(ALL_MUSIC)
if ex != 'on':
cl.open_ex(False)
if fid != 'all':
cl.set_filter_fid(fid_param_to_str.get(fid, 0x00))
res = yield cl.search(key_word, (pn-1)*10)
if not res:
self.write('Something maybe wrong, Please wait some seconds!')
return
# result need
result = {'key_word': key_word, 'pn': pn, 'fid': fid, 'count': res['total_found'], 'time': res['time']}
result_items = None
if 'matches' in res.keys() and res['total'] > 0:
tids = [str(match['id']) for match in res['matches']] if 'matches' in res.keys() else None
result_items = []
if len(tids) > 0:
tid_sql_str = ','.join(tids)
conn = yield tornado_mysql.connect(host=SQL_HOST, port=SQL_PORT,
user=SQL_USER, passwd=SQL_PWD,
db=DB, charset='utf8')
cur = conn.cursor()
yield cur.execute(sql_cmd % (tid_sql_str, tid_sql_str))
for row in cur:
row = [row_item.encode('utf-8') if isinstance(row_item, unicode) else row_item for row_item in row]
row_excerpts = [row[1], row[6]]
row_excerpts[1] = self.__re_html.sub('', row_excerpts[1])
row_excerpts = cl.build_excerpts(row_excerpts, 'astost', key_word, excerpt_opts)
result_item = {'tid': row[0], 'title': row_excerpts[0] + '_' + row[2],
'time': row[5], 'abstract': row_excerpts[1],
'fid_str': row[2], 'fid_num': fid_str_nu.get(row[2], ''),
'uid': row[3], 'user': row[4]}
result_items.append(result_item)
cur.close()
conn.close()
self.render('result.html', result=result, result_items=result_items)
@staticmethod
def _check_argument_pn(pn):
try:
pn_p = int(pn)
except ValueError:
pn_p = 1
pn_p = pn_p if pn_p > 0 else 1
pn_p = pn_p if pn_p <= 100 else 100
return pn_p
@staticmethod
def __check_fid(fid):
if fid not in fid_param_to_str.keys():
return 'all'
return fid