From 82a0681e9ffe7d1e147d1f511c53f28066bce782 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Thu, 16 Feb 2017 09:10:56 +0800 Subject: [PATCH] Clean up and fix --- commands/natural_language.py | 19 ++++++++++--------- commands/networktools.py | 13 ++++++------- msg_src_adapters/coolq_http_api.py | 5 ++--- nl_processors/translate.py | 2 +- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/commands/natural_language.py b/commands/natural_language.py index 684fcc78..74ddbd7c 100644 --- a/commands/natural_language.py +++ b/commands/natural_language.py @@ -29,12 +29,13 @@ def process(args_text, ctx_msg): ctx_msg['parsed_data'] = most_possible_cmd[3] cmdhub.call(most_possible_cmd[1], most_possible_cmd[2], ctx_msg) else: - if ctx_msg.get('from_voice'): - # Special for voice message - if _fallback_command: - core.echo('暂时无法理解你的意思,下面将使用备用命令 ' + _fallback_command + '……', ctx_msg) - cmdhub.call(_fallback_command, sentence, ctx_msg) - return - core.echo('暂时无法理解你的意思。\n' - '由于自然语言识别还非常不完善,建议使用命令来精确控制我。\n' - '如需帮助请发送「使用帮助」。', ctx_msg) + # if ctx_msg.get('from_voice'): + # Special for voice message + if _fallback_command: + core.echo('暂时无法理解你的意思,下面将使用备用命令 ' + _fallback_command + '……', ctx_msg) + cmdhub.call(_fallback_command, sentence, ctx_msg) + return + pass + # core.echo('暂时无法理解你的意思。\n' + # '由于自然语言识别还非常不完善,建议使用命令来精确控制我。\n' + # '如需帮助请发送「使用帮助」。', ctx_msg) diff --git a/commands/networktools.py b/commands/networktools.py index 21961a73..25b7ce33 100644 --- a/commands/networktools.py +++ b/commands/networktools.py @@ -28,13 +28,12 @@ def ip(args_text, ctx_msg): resp = requests.get(chinaz_url % query) if resp.status_code == 200: html = etree.HTML(resp.text) - p_elems = html.xpath('//*[@id="leftinfo"]/div[3]/div[2]/p') - if len(p_elems) > 1: - p_elems = p_elems[1:] - reply = 'ChinaZ:' + p_elems = html.xpath('//p[@class="WhwtdWrap bor-b1s col-gray03"]') + if len(p_elems) > 0: + reply = 'ChinaZ.com:' for p_elem in p_elems: span_elems = p_elem.getchildren() - reply += '\n' + span_elems[1].text.replace('.', '_') + ', ' + span_elems[3].text + reply += '\n' + span_elems[1].text + ', ' + span_elems[3].text core.echo(reply, ctx_msg) found = True @@ -44,7 +43,7 @@ def ip(args_text, ctx_msg): # Example: 'IP:123.125.114.144 来自:北京市 联通' items = resp.text.strip().split(':') if len(items) == 3: - reply = 'IP_CN:\n' + items[1].split(' ')[0].replace('.', '_') + ', ' + items[2] + reply = 'IP.cn:\n' + items[1].split(' ')[0] + ', ' + items[2] core.echo(reply, ctx_msg) found = True @@ -53,7 +52,7 @@ def ip(args_text, ctx_msg): if resp.status_code == 200 and resp.text.strip(): # Example: '["中国","江苏","常州","","教育网"]' parts = json.loads(resp.text) - reply = 'IPIP_NET\n' + query.replace('.', '_') + ' ' + ''.join(parts) + reply = 'IPIP.net\n' + query + ' ' + ''.join(parts) core.echo(reply, ctx_msg) found = True diff --git a/msg_src_adapters/coolq_http_api.py b/msg_src_adapters/coolq_http_api.py index f6e5fc3a..df7afa60 100644 --- a/msg_src_adapters/coolq_http_api.py +++ b/msg_src_adapters/coolq_http_api.py @@ -18,9 +18,8 @@ class CoolQHttpApiAdapter(Adapter): def unitize_context(self, ctx_msg: dict): # Check token - if self.token: - if flask_req.headers.get('Authorization', '') != 'token ' + self.token: - return None + if flask_req.headers.get('Authorization') != self.session.headers.get('Authorization'): + return None new_ctx = {'raw_ctx': ctx_msg, 'post_type': ctx_msg['post_type'], 'via': ctx_msg['via'], 'login_id': ctx_msg['login_id']} diff --git a/nl_processors/translate.py b/nl_processors/translate.py index d3258a0b..fbf27717 100644 --- a/nl_processors/translate.py +++ b/nl_processors/translate.py @@ -5,7 +5,7 @@ from nl_processor import as_processor _query_lang_matcher = [ re.compile('(?:(?:要|[应]?该)?怎么|怎样|如何)?[把将]?[\s,.,。]?(?P.*?)[\s,.,。]?' '(?:这[个]?(?:词[组]?|句(?:子|话)?|短语))?翻译[成为到](?P\w+?[文语])(?![\s::,,.。])'), - re.compile('(?P.+?)[\s,.,。]?(?:这[个]?(?:词[组]?|句(?:子|话)?|短语))?的(?P\w+?[文语])'), + re.compile('(?P.+?)[\s,.,。]?(?:这[个]?(?:词[组]?|句(?:子|话)?|短语))?[的用](?P\w+?[文语])'), re.compile('.*?[把将]?(?:(?:[下后][面])?(?:这[个]?|[下后][面]?)(?:词[组]?|句(?:子|话)?|短语))?' '翻译[成为到]\s*(?P\w+?[文语])[\s::,,](?P.*)'), re.compile('.*[用]?(?P\w+?[文语])\w*?(?:说|讲|表达|表示)'