Skip to content

Commit

Permalink
Merge pull request #48 from liszd/develop
Browse files Browse the repository at this point in the history
添加有道智云的错误提醒
  • Loading branch information
whyliam authored Aug 30, 2017
2 parents 5b8b104 + 32d5063 commit d7ac7b0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 39 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# whyliam.workflows.youdao

## 有道翻译 workflow v2.1.0
## 有道翻译 workflow v2.1.1

默认快捷键 `yd`,查看翻译结果。

Expand All @@ -16,11 +16,11 @@
2.`Control ⌃+回车` 打开有道翻译页面
3.`Command ⌘+回车` 直接在打出翻译结果
4.`Shift ⇧+回车` 直接发音
5. 选中文字 双击`Option ⌥`进行翻译
5. 选中文字 双击`Option ⌥`进行翻译(需要另行设置)

### 下载

[点击下载](https://github.com/liszd/whyliam.workflows.youdao/releases/download/2.1.0/whyliam.workflows.youdao.alfredworkflow)
[点击下载](https://github.com/liszd/whyliam.workflows.youdao/releases/download/2.1.1/whyliam.workflows.youdao.alfredworkflow)

### 安装

Expand All @@ -34,7 +34,7 @@

![](http://ww2.sinaimg.cn/large/006tNbRwgy1feno2vevklj31a30os0u5.jpg)

**新版本有道智云**
**或者 新版本有道智云**

2\. [注册](http://ai.youdao.com/appmgr.s)有道智云应用

Expand All @@ -45,6 +45,10 @@
4\. 在Alfred的设置中设置快捷方式键
![](http://ww2.sinaimg.cn/large/006tNbRwgy1feno6pzaxdj31a60p0jsl.jpg)

### 问题

如果新版本有道智云遇到问题,请参见 [错误代码列表](http://ai.youdao.com/docs/api.s#id6)

### 演示

#### 英译中
Expand Down
6 changes: 4 additions & 2 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@
<dict>
<key>alfredfiltersresults</key>
<false/>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
<integer>0</integer>
<key>escaping</key>
Expand Down Expand Up @@ -324,7 +326,7 @@
</dict>
</array>
<key>readme</key>
<string>有道翻译 Workflow v2.1.0
<string>有道翻译 Workflow v2.1.1
默认快捷键 yd, 查看翻译结果。
Expand Down Expand Up @@ -431,7 +433,7 @@
<string>zhiyun_key</string>
</array>
<key>version</key>
<string>2.1.0</string>
<string>2.1.1</string>
<key>webaddress</key>
<string>https://github.com/liszd/whyliam.workflows.youdao</string>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.0
2.1.1
Binary file modified whyliam.workflows.youdao.alfredworkflow
Binary file not shown.
66 changes: 34 additions & 32 deletions youdao.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@
2002493141, 2002493142, 2002493143,
1947745089, 1947745090)

ERRORCODE_DICT = {
"50": "有道翻译的API Key错误",
"101": "缺少必填的参数,出现这个情况还可能是et的值和实际加密方式不对应",
"102": "不支持的语言类型", "103": "翻译文本过长",
"104": "不支持的API类型", "105": "不支持的签名类型",
"106": "不支持的响应类型", "107": "不支持的传输加密类型",
"108": "appKey无效( 注意不是应用密钥)",
"109": "batchLog格式不正确",
"110": "无相关服务的有效实例",
"111": "开发者账号无效,可能是账号为欠费状态",
"201": "解密失败,可能为DES,BASE64,URLDecode的错误",
"202": "签名检验失败", "203": "访问IP地址不在可访问IP列表",
"301": "辞典查询失败", "302": "翻译查询失败",
"303": "服务端的其它异常",
"401": "账户已经欠费停",
"500": "有道翻译的API Key使用频率过高"
}

ICON_DEFAULT = 'icon.png'
ICON_PHONETIC = 'icon_phonetic.png'
ICON_BASIC = 'icon_basic.png'
Expand All @@ -26,15 +44,16 @@

def set_youdao_url(query):
# 构建有道翻译URL
import os, random
import os
import random

zhiyun_id = os.getenv('zhiyun_id', '')
zhiyun_key = os.getenv('zhiyun_key', '')
zhiyun_id = os.getenv('zhiyun_id', '').strip()
zhiyun_key = os.getenv('zhiyun_key', '').strip()
if zhiyun_id and zhiyun_key:
url = set_youdao_new_url_from(query, zhiyun_id, zhiyun_key)
else:
youdao_keyfrom = os.getenv('youdao_keyfrom', '')
youdao_key = os.getenv('youdao_key', '')
youdao_keyfrom = os.getenv('youdao_keyfrom', '').strip()
youdao_key = os.getenv('youdao_key', '').strip()
if not youdao_keyfrom or not youdao_key:
i = random.randrange(0, 11, 1)
youdao_keyfrom = YOUDAO_DEFAULT_KEYFROM[i]
Expand All @@ -55,7 +74,9 @@ def set_youdao_old_url_from(query, youdao_keyfrom, youdao_key):


def set_youdao_new_url_from(query, zhiyun_id, zhiyun_key):
import urllib, hashlib, uuid
import urllib
import hashlib
import uuid

salt = uuid.uuid4().hex
sign = hashlib.md5(zhiyun_id + query + salt + zhiyun_key).hexdigest()
Expand All @@ -77,11 +98,11 @@ def get_web_data(query):
return rt
except:
rt = {}
rt['errorCode'] = 500
rt['errorCode'] = "500"
return rt
else:
rt = {}
rt['errorCode'] = 500
rt['errorCode'] = "500"
return rt


Expand Down Expand Up @@ -184,36 +205,17 @@ def main(wf):
wf.send_feedback()

rt = get_web_data(query)
errorCode = str(rt.get("errorCode"))

if rt.get("errorCode") in (500, u'500'):
arg = ['', '', '', '', 'error']
arg = '$%'.join(arg)
wf.add_item(
title='有道翻译的API Key使用频率过高', subtitle='', arg=arg,
valid=True, icon=ICON_ERROR)

elif rt.get("errorCode") in (108, u'108'):
arg = ['', '', '', '', 'error']
arg = '$%'.join(arg)
wf.add_item(
title='appID 或者 appKey 无效', subtitle='', arg=arg,
valid=True, icon=ICON_ERROR)

elif rt.get("errorCode") in (111, u'111', 401, u'401'):
arg = ['', '', '', '', 'error']
arg = '$%'.join(arg)
wf.add_item(
title='账号为欠费状态', subtitle='', arg=arg,
valid=True, icon=ICON_ERROR)

elif rt.get("errorCode") in (50, u'50'):
if ERRORCODE_DICT.has_key(errorCode):
arg = ['', '', '', '', 'error']
arg = '$%'.join(arg)
wf.add_item(
title='有道翻译的API Key错误', subtitle='', arg=arg,
title=errorCode+" "+ERRORCODE_DICT[errorCode],
subtitle='', arg=arg,
valid=True, icon=ICON_ERROR)

elif rt.get("errorCode") in (0, u'0'):
elif errorCode == "0":
isEnglish = check_English(query)
get_translation(query, isEnglish, rt)
get_phonetic(query, isEnglish, rt)
Expand Down

0 comments on commit d7ac7b0

Please sign in to comment.