diff --git a/91C343E7-50D8-4B0D-9034-1C16C20DA8D4.png b/91C343E7-50D8-4B0D-9034-1C16C20DA8D4.png index 5bc60c4..02a099f 100644 Binary files a/91C343E7-50D8-4B0D-9034-1C16C20DA8D4.png and b/91C343E7-50D8-4B0D-9034-1C16C20DA8D4.png differ diff --git a/README.md b/README.md index dbb7667..8e17804 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # whyliam.workflows.youdao -## 有道翻译 workflow v1.2.5 +## 有道翻译 workflow v2.0.0 -默认快捷键 "yd",查看翻译结果。 +默认快捷键 `yd`,查看翻译结果。 1. 英译中 2. 中译英 @@ -13,45 +13,57 @@ ### 功能 1. 按`回车` 复制 -2. 按`Control+回车` 打开有道翻译页面 -3. 按`Command+回车` 直接在打出翻译结果 -4. 按`Shift+回车` 直接发音 -5. 选中文字 双击`Command`进行翻译(暂定) +2. 按`Control ⌃+回车` 打开有道翻译页面 +3. 按`Command ⌘+回车` 直接在打出翻译结果 +4. 按`Shift ⇧+回车` 直接发音 +5. 选中文字 双击`Option ⌥`进行翻译 -### 问题 - -如果出现`No JSON object could be decoded` +### 下载 -![No JSON object could be decoded](https://cloud.githubusercontent.com/assets/12092275/16805774/590a4064-4944-11e6-9b12-d8218024c032.png) +[点击下载](https://github.com/liszd/whyliam.workflows.youdao/releases/download/2.0.0/whyliam.workflows.youdao.alfredworkflow) -请删除该文件 -`/Users/用户名/Library/Application\ Support/Alfred\ 3/Workflow\ Data/whyliam.workflows.youdao` +### 安装 -### 下载 +1. [下载](https://github.com/liszd/whyliam.workflows.youdao/releases)最新版本双击安装 +2. [注册](http://fanyi.youdao.com/openapi?path=data-mode)有道翻译API +3. 在Alfred的设置中填入对应的`Key`和`Keyfrom` +![](http://ww2.sinaimg.cn/large/006tNbRwgy1feno2vevklj31a30os0u5.jpg) +4. 在Alfred的设置中设置快捷方式键 +![](http://ww2.sinaimg.cn/large/006tNbRwgy1feno6pzaxdj31a60p0jsl.jpg) -[点击下载](https://github.com/liszd/whyliam.workflows.youdao/releases/download/1.2.5/whyliam.workflows.youdao.alfredworkflow) - -## Demo +### 演示 #### 英译中 -![](http://ww2.sinaimg.cn/large/48910e01gw1erucr05z85g213p0kbqhn.gif) +![](http://ww3.sinaimg.cn/large/006tNbRwgy1fenonlxdjwg30sv0r7wkd.gif) #### 中译英 -![](http://ww2.sinaimg.cn/large/48910e01gw1erucrd5tnmg213p0kbk6q.gif) +![](http://ww1.sinaimg.cn/large/006tNbRwgy1fenonzclvfg30sw0r90zo.gif) #### 翻译短语 -![](http://ww2.sinaimg.cn/large/48910e01gw1erucrvb9a8g213p0kbqhn.gif) +![](http://ww3.sinaimg.cn/large/006tNbRwgy1fenooolrkpg30t00r47bg.gif) + +#### 发音 - 按`Shift ⇧+回车` + +![](http://ww3.sinaimg.cn/large/006tNbRwgy1fenooolrkpg30t00r47bg.gif) + +#### 打开有道翻译页面 - 按`Control ⌃+回车` + +![](http://ww2.sinaimg.cn/large/006tNbRwgy1fenopnjw9qg30tj0r5n8k.gif) + +#### 直接在打出翻译结果 - 按`Command ⌘+回车` + +![](http://ww3.sinaimg.cn/large/006tNbRwgy1fenomln8jdg30sx0r4wg2.gif) -#### 打开有道翻译页面 - 按`Control+回车` +#### 双击快速翻译 - 双击`Option ⌥` -![](http://ww4.sinaimg.cn/large/48910e01gw1erucsmvtkgg213l0kaqq2.gif) +![](http://ww1.sinaimg.cn/large/006tNbRwgy1fenosusv0bg30qn0qpq7a.gif) -#### 直接在打出翻译结果 - 按`Command+回车` +### 更多 -![](http://ww3.sinaimg.cn/large/48910e01gw1eructbvt9rg213p0jh0wi.gif) +更多问题参见[Alfred 有道翻译简介](https://blog.naaln.com/2017/04/alfred-youdao-intro/) --- diff --git a/icon.png b/icon.png index bbaeb96..3c1a383 100755 Binary files a/icon.png and b/icon.png differ diff --git a/icon_basic.png b/icon_basic.png index 8b54148..88dedcb 100755 Binary files a/icon_basic.png and b/icon_basic.png differ diff --git a/icon_error.png b/icon_error.png new file mode 100644 index 0000000..9195a07 Binary files /dev/null and b/icon_error.png differ diff --git a/icon_phonetic.png b/icon_phonetic.png index 5782193..7fac86d 100644 Binary files a/icon_phonetic.png and b/icon_phonetic.png differ diff --git a/icon_update.png b/icon_update.png new file mode 100644 index 0000000..11e37b3 Binary files /dev/null and b/icon_update.png differ diff --git a/icon_web.png b/icon_web.png index 594f622..6ab85a8 100755 Binary files a/icon_web.png and b/icon_web.png differ diff --git a/info.plist b/info.plist index 44ef41c..b9e1832 100755 --- a/info.plist +++ b/info.plist @@ -54,7 +54,7 @@ modifiersubtext 有道发音 vitoclose - + destinationuid @@ -77,6 +77,19 @@ + F99C4C55-10F5-4D62-A77D-F27058629B21 + + + destinationuid + 6A03FDC5-89AC-4F9D-9456-3762ACA751FE + modifiers + 0 + modifiersubtext + + vitoclose + + + createdby whyliam @@ -96,8 +109,7 @@ escaping 35 script - query={query} -open "http://dict.youdao.com/search?q=`echo $query | awk 'BEGIN{FS="$"} {print $1}'`" + /usr/bin/python splitargs.py "{query}" 0 scriptargtype 0 scriptfile @@ -112,13 +124,32 @@ open "http://dict.youdao.com/search?q=`echo $query | awk 'BEGIN{FS="$"} {print $ version 2 + + config + + browser + + spaces + + url + http://dict.youdao.com/search?q={query} + utf8 + + + type + alfred.workflow.action.openurl + uid + 6A03FDC5-89AC-4F9D-9456-3762ACA751FE + version + 1 + config concurrently escaping - 33 + 35 script /usr/bin/python splitargs.py "{query}" 2 scriptargtype @@ -143,13 +174,13 @@ open "http://dict.youdao.com/search?q=`echo $query | awk 'BEGIN{FS="$"} {print $ argumenttype 0 escaping - 32 + 35 keyword yd queuedelaycustom 3 queuedelayimmediatelyinitially - + queuedelaymode 1 queuemode @@ -201,7 +232,7 @@ open "http://dict.youdao.com/search?q=`echo $query | awk 'BEGIN{FS="$"} {print $ concurrently escaping - 34 + 32 script /usr/bin/python splitargs.py "{query}" 1 scriptargtype @@ -218,13 +249,46 @@ open "http://dict.youdao.com/search?q=`echo $query | awk 'BEGIN{FS="$"} {print $ version 2 + + config + + action + 1 + argument + 1 + argumenttext + yd + focusedappvariable + + focusedappvariablename + + hotkey + -1 + hotmod + 524288 + hotstring + double tap + leftcursor + + modsmode + 0 + relatedAppsMode + 0 + + type + alfred.workflow.trigger.hotkey + uid + 5751065C-52C1-4D19-8F7D-03B730BFE440 + version + 2 + config concurrently escaping - 34 + 32 script /usr/bin/python splitargs.py "{query}" 1 scriptargtype @@ -258,42 +322,11 @@ open "http://dict.youdao.com/search?q=`echo $query | awk 'BEGIN{FS="$"} {print $ version 2 - - config - - action - 1 - argument - 1 - argumenttext - yd - focusedappvariable - - focusedappvariablename - - hotkey - 0 - hotmod - 0 - leftcursor - - modsmode - 0 - relatedAppsMode - 0 - - type - alfred.workflow.trigger.hotkey - uid - 5751065C-52C1-4D19-8F7D-03B730BFE440 - version - 2 - readme - 有道翻译 Workflow v1.2.5 + 有道翻译 Workflow v2.0.0 -默认快捷键 "yd", 查看翻译结果。 +默认快捷键 yd, 查看翻译结果。 1. 英译中 2. 中译英 @@ -301,74 +334,98 @@ open "http://dict.youdao.com/search?q=`echo $query | awk 'BEGIN{FS="$"} {print $ 4. 打开有道翻译页面,查看详细内容 5. 直接在打出翻译结果 -*功能* +功能 1. 按`回车`复制 2. 按`Control+回车`打开有道翻译页面 3. 按`Comman+回车`直接在打出翻译结果 4. 按`Shift+回车`直接发音 -5. 选中文字 双击`Command`进行翻译(暂定) +5. 选中文字 双击`Command`进行翻译 + +更多 +参见 https://blog.naaln.com/2017/04/alfred-youdao-intro/ uidata 0907BEF4-816F-48FF-B157-03F5C2AACEAB xpos - 700 + 830 ypos - 390 + 440 27E60581-8105-41DD-8E29-4FE811179098 xpos 500 ypos - 240 + 290 4473C9D3-7A15-4D31-84F6-A096A7CFF46C xpos - 700 + 830 ypos - 120 + 150 5751065C-52C1-4D19-8F7D-03B730BFE440 + + note + 双击设置快捷方式 + xpos + 270 + ypos + 430 + + 6A03FDC5-89AC-4F9D-9456-3762ACA751FE xpos - 310 + 830 ypos - 480 + 40 7C1ABC41-3B36-401F-96C7-30BCB39181FF xpos 500 ypos - 390 + 440 91C343E7-50D8-4B0D-9034-1C16C20DA8D4 xpos - 300 + 270 ypos - 120 + 250 DBA62127-3B78-4B80-B82B-1C6AEC393003 xpos - 700 + 830 ypos - 240 + 290 F99C4C55-10F5-4D62-A77D-F27058629B21 xpos 500 ypos - 10 + 40 + variables + + youdao_key + 1947745091 + youdao_keyfrom + whyliam-wf-12 + + variablesdontexport + + youdao_keyfrom + youdao_key + version - 1.2.5 + 2.0.0 webaddress https://github.com/liszd/whyliam.workflows.youdao diff --git a/splitargs.py b/splitargs.py index 9b7e290..15461ad 100644 --- a/splitargs.py +++ b/splitargs.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import sys import os -from workflow import Workflow +from workflow import Workflow3 reload(sys) sys.setdefaultencoding('utf8') @@ -9,20 +9,32 @@ def getargs(wf): query = sys.argv[1] - query = query.split('$') + query = query.split('$%') part = int(sys.argv[2]) - if part == 1: + if query[4]: + import webbrowser + new = 2 + url = "https://blog.naaln.com/2017/04/alfred-youdao-intro/" + webbrowser.open(url, new=new) + return 0 + + if part == 0: + # 查询的单词 + sys.stdout.write(query[0].strip()) + elif part == 1: + # 翻过的结果 sys.stdout.write(query[1].strip()) elif part == 2: + # 发音 if query[2]: bashCommand = "say --voice='Samantha' " + query[2] os.system(bashCommand) if query[3]: bashCommand = "say --voice='Ting-Ting' " + query[3] os.system(bashCommand) - return 0 + if __name__ == '__main__': - wf = Workflow() + wf = Workflow3() sys.exit(wf.run(getargs)) diff --git a/version b/version index b966e81..359a5b9 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.2.4 \ No newline at end of file +2.0.0 \ No newline at end of file diff --git a/whyliam.workflows.youdao.alfredworkflow b/whyliam.workflows.youdao.alfredworkflow index 91d7389..a86d265 100644 Binary files a/whyliam.workflows.youdao.alfredworkflow and b/whyliam.workflows.youdao.alfredworkflow differ diff --git a/youdao.py b/youdao.py index 5b30810..c8ef9df 100755 --- a/youdao.py +++ b/youdao.py @@ -1,124 +1,200 @@ # -*- coding: utf-8 -*- -import re -import random -import urllib -from workflow import Workflow, ICON_WEB, web -import sys +from workflow import Workflow3, ICON_WEB, web +import sys reload(sys) sys.setdefaultencoding('utf8') -apikey = [1331254833, 2002493135, 2002493136, 2002493137, - 2002493138, 2002493139, 2002493140, 2002493141, - 2002493142, 2002493143, 1947745089, 1947745090] +YOUDAO_DEFAULT_KEYFROM = ('whyliam-wf-1', 'whyliam-wf-2', 'whyliam-wf-3', + 'whyliam-wf-4', 'whyliam-wf-5', 'whyliam-wf-6', + 'whyliam-wf-7', 'whyliam-wf-8', 'whyliam-wf-9', + 'whyliam-wf-10', 'whyliam-wf-11') -keyfrom = ['whyliam', 'whyliam-wf-1', 'whyliam-wf-2', 'whyliam-wf-3', - 'whyliam-wf-4', 'whyliam-wf-5', 'whyliam-wf-6', 'whyliam-wf-7', - 'whyliam-wf-8', 'whyliam-wf-9', 'whyliam-wf-10', 'whyliam-wf-11'] +YOUDAO_DEFAULT_KEY = (2002493135, 2002493136, 2002493137, + 2002493138, 2002493139, 2002493140, + 2002493141, 2002493142, 2002493143, + 1947745089, 1947745090) ICON_DEFAULT = 'icon.png' ICON_PHONETIC = 'icon_phonetic.png' ICON_BASIC = 'icon_basic.png' ICON_WEB = 'icon_web.png' +ICON_UPDATE = 'icon_update.png' +ICON_ERROR = 'icon_error.png' + + +def set_youdao_url(query): + # 构建有道翻译URL + import os + import random + import urllib + + youdao_keyfrom = os.getenv('youdao_keyfrom', '') + youdao_key = os.getenv('youdao_key', '') + if not youdao_keyfrom or not youdao_key: + i = random.randrange(0, 11, 1) + youdao_keyfrom = YOUDAO_DEFAULT_KEYFROM[i] + youdao_key = YOUDAO_DEFAULT_KEY[i] -def get_web_data(query): query = urllib.quote(str(query)) - i = random.randrange(0, 12, 1) - url = 'http://fanyi.youdao.com/openapi.do?keyfrom=' + keyfrom[i] + \ - '&key=' + str(apikey[i]) + \ + url = 'http://fanyi.youdao.com/openapi.do?keyfrom=' + youdao_keyfrom + \ + '&key=' + str(youdao_key) + \ '&type=data&doctype=json&version=1.1&q=' + query - return web.get(url).json() + return url + + +def get_web_data(query): + # 获取翻译数据 + url = set_youdao_url(query) + try: + rt = web.get(url).json() + return rt + except: + rt = {} + rt['errorCode'] = 500 + return rt + else: + rt = {} + rt['errorCode'] = 500 + return rt + + +def check_Update(): + # 检查更新 + if wf.update_available: + arg = ['', '', '', '', 'error'] + arg = '$%'.join(arg) + wf.add_item( + title='有新版本更新', subtitle='', arg=arg, + valid=True, icon=ICON_UPDATE) + else: + wf.add_item('有道翻译') + + +def check_English(query): + # 检查英文翻译中午 + import re + + if re.search(ur"[\u4e00-\u9fa5]+", query): + return False + return True + + +def get_translation(query, isEnglish, rt): + # 翻译结果 + subtitle = '翻译结果' + translations = rt["translation"] + for title in translations: + arg = [query, title, query, '', ''] if isEnglish else [ + query, title, title, '', ''] + arg = '$%'.join(arg) + # print arg + wf.add_item( + title=title, subtitle=subtitle, arg=arg, + valid=True, icon=ICON_DEFAULT) + + +def get_phonetic(query, isEnglish, rt): + # 发音 + if u'basic' in rt.keys(): + if rt["basic"].get("phonetic"): + title = "" + if rt["basic"].get("us-phonetic"): + title += ("[美: " + rt["basic"]["us-phonetic"] + "] ") + if rt["basic"].get("uk-phonetic"): + title += ("[英: " + rt["basic"]["uk-phonetic"] + "] ") + title = title if title else "[" + rt["basic"]["phonetic"] + "]" + subtitle = '有道发音' + arg = [query, title, query, '', ''] if isEnglish else [ + query, title, '', query, ''] + arg = '$%'.join(arg) + wf.add_item( + title=title, subtitle=subtitle, arg=arg, + valid=True, icon=ICON_PHONETIC) + + +def get_explains(query, isEnglish, rt): + # 简明释意 + if u'basic' in rt.keys(): + for i in range(len(rt["basic"]["explains"])): + title = rt["basic"]["explains"][i] + subtitle = '简明释意' + arg = [query, title, query, '', ''] if isEnglish else [ + query, title, '', title, ''] + arg = '$%'.join(arg) + wf.add_item( + title=title, subtitle=subtitle, arg=arg, + valid=True, icon=ICON_PHONETIC) + + +def get_web_translation(query, isEnglish, rt): + # 网络翻译 + if u'web' in rt.keys(): + for i in range(len(rt["web"])): + titles = rt["web"][i]["value"] + for title in titles: + subtitle = '网络翻译: ' + rt["web"][i]["key"] + + if isEnglish: + key = ''.join(rt["web"][i]["key"]) + arg = [query, title, key, '', ''] + else: + value = ' '.join(rt["web"][i]["value"]) + arg = [query, title, title, '', ''] + + arg = '$%'.join(arg) + wf.add_item( + title=title, subtitle=subtitle, + arg=arg, valid=True, icon=ICON_WEB) def main(wf): query = wf.args[0].strip().replace("\\", "") + if not isinstance(query, unicode): + query = query.decode('utf8') if not query: - wf.add_item('有道翻译') + check_Update() wf.send_feedback() - return 0 - - s = get_web_data(query) - - if s.get("errorCode") == 0: - # '翻译结果' - title = s["translation"] - title = ''.join(title) - # url = u'http://dict.youdao.com/search?q=' + query - tran = 'EtC' - if not isinstance(query, unicode): - query = query.decode('utf8') - if re.search(ur"[\u4e00-\u9fa5]+", query): - tran = 'CtE' - subtitle = '翻译结果' - - arg = [query, title, query, ' '] if tran == 'EtC' else [ - query, title, title, ' '] - arg = '$'.join(arg) + + rt = get_web_data(query) + + if rt.get("errorCode") == 500: + arg = ['', '', '', '', 'error'] + arg = '$%'.join(arg) wf.add_item( - title=title, subtitle=subtitle, arg=arg, valid=True, icon=ICON_DEFAULT) - - if u'basic' in s.keys(): - # '发音' - if s["basic"].get("phonetic"): - title = "" - if s["basic"].get("us-phonetic"): - title += (" [美: " + s["basic"]["us-phonetic"] + "]") - if s["basic"].get("uk-phonetic"): - title += (" [英: " + s["basic"]["uk-phonetic"] + "]") - title = title if title else "[" + s["basic"]["phonetic"] + "]" - subtitle = '有道发音' - arg = [query, title, query, ' '] if tran == 'EtC' else [ - query, title, ' ', query] - arg = '$'.join(arg) - wf.add_item( - title=title, subtitle=subtitle, arg=arg, valid=True, icon=ICON_PHONETIC) - - # '简明释意' - for be in range(len(s["basic"]["explains"])): - title = s["basic"]["explains"][be] - subtitle = '简明释意' - arg = [query, title, query, ' '] if tran == 'EtC' else [ - query, title, title, ' '] - arg = '$'.join(arg) - wf.add_item( - title=title, subtitle=subtitle, arg=arg, valid=True, icon=ICON_BASIC) - - # '网络翻译' - if u'web' in s.keys(): - for w in range(len(s["web"])): - title = s["web"][w]["value"] - title = ', '.join(title) - subtitle = '网络翻译: ' + s["web"][w]["key"] - - if tran == 'EtC': - key = ''.join(s["web"][w]["key"]) - arg = [query, title, key, ' '] - else: - value = ' '.join(s["web"][w]["value"]) - arg = [query, title, value, ' '] + title='有道翻译的API Key使用频率过高', subtitle='', arg=arg, + valid=True, icon=ICON_ERROR) - arg = '$'.join(arg) - wf.add_item( - title=title, subtitle=subtitle, arg=arg, valid=True, icon=ICON_WEB) + elif rt.get("errorCode") == 50: + arg = ['', '', '', '', 'error'] + arg = '$%'.join(arg) + wf.add_item( + title='有道翻译的API Key错误', subtitle='', arg=arg, + valid=True, icon=ICON_ERROR) + + elif rt.get("errorCode") == 0: + isEnglish = check_English(query) + get_translation(query, isEnglish, rt) + get_phonetic(query, isEnglish, rt) + get_explains(query, isEnglish, rt) + get_web_translation(query, isEnglish, rt) else: title = '有道也翻译不出来了' subtitle = '尝试一下去网站搜索' arg = [query, ' ', ' ', ' '] - arg = '$'.join(arg) + arg = '$%'.join(arg) wf.add_item( - title=title, subtitle=subtitle, arg=arg, valid=True, icon=ICON_DEFAULT) + title=title, subtitle=subtitle, arg=arg, + valid=True, icon=ICON_DEFAULT) wf.send_feedback() if __name__ == '__main__': - wf = Workflow(update_settings={ + wf = Workflow3(update_settings={ 'github_slug': 'liszd/whyliam.workflows.youdao', 'frequency': 7 }) - - if wf.update_available: - wf.start_update() sys.exit(wf.run(main))