Skip to content

Commit

Permalink
Merge pull request #6 from liszd/phonetic
Browse files Browse the repository at this point in the history
update pronunciation
  • Loading branch information
whyliam committed Dec 16, 2015
2 parents f25376e + 17cc48b commit bb1293e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 59 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# whyliam.workflows.youdao

有道翻译 workflow v1.2.0
有道翻译 workflow v1.2.1

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

Expand All @@ -13,11 +13,10 @@
*功能*

1.`回车` 复制
2.`Ctrl+回车` 打开有道翻译页面
2.`Control+回车` 打开有道翻译页面
3.`Command+回车` 直接在打出翻译结果
4.`Shift+回车` 直接发音


[下载](https://github.com/liszd/whyliam.workflows.youdao/blob/master/whyliam.workflows.youdao.alfredworkflow?raw=true)

## Demo
Expand All @@ -34,7 +33,7 @@

![](http://ww2.sinaimg.cn/large/48910e01gw1erucrvb9a8g213p0kbqhn.gif)

#### 打开有道翻译页面 - 按`Ctrl+回车`
#### 打开有道翻译页面 - 按`Control+回车`

![](http://ww4.sinaimg.cn/large/48910e01gw1erucsmvtkgg213l0kaqq2.gif)

Expand Down
67 changes: 31 additions & 36 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@
# get default browser name see: https://discussions.apple.com/thread/4570295?start=0&tstart=0
set browserName to do shell script "export VERSIONER_PERL_PREFER_32_BIT=yes; perl -MMac::InternetConfig -le 'print +(GetICHelper \"http\")[1]'"
set AppleScript's text item delimiters to {","}
set AppleScript's text item delimiters to {"$"}
set urlD to text item 1 of q
set urlD to "http://dict.youdao.com/search?q="&urlD
if ((contents of browserName) is equal to "Google Chrome") then
tell application browserName
Expand Down Expand Up @@ -128,16 +129,9 @@ end alfred_script</string>
<key>concurrently</key>
<false/>
<key>escaping</key>
<integer>103</integer>
<integer>33</integer>
<key>script</key>
<string>string="{query}"
array=(${string//,/ })
if ([[ ${array[1]} =~ ^[[:alnum:]]*$ ]])
then
say ${array[1]}
else
say --voice="Ting-Ting" ${array[1]}
fi</string>
<string>/usr/bin/python splitargs.py "{query}" 2</string>
<key>type</key>
<integer>0</integer>
</dict>
Expand All @@ -154,7 +148,7 @@ fi</string>
<key>argumenttype</key>
<integer>0</integer>
<key>escaping</key>
<integer>7</integer>
<integer>32</integer>
<key>keyword</key>
<string>yd</string>
<key>queuedelaycustom</key>
Expand All @@ -164,7 +158,7 @@ fi</string>
<key>queuedelaymode</key>
<integer>0</integer>
<key>queuemode</key>
<integer>1</integer>
<integer>2</integer>
<key>runningsubtext</key>
<string>正在获取中...</string>
<key>script</key>
Expand All @@ -188,34 +182,34 @@ fi</string>
<dict>
<key>config</key>
<dict>
<key>concurrently</key>
<key>autopaste</key>
<false/>
<key>escaping</key>
<integer>68</integer>
<key>script</key>
<string>/usr/bin/python splitargs.py "{query}" 1</string>
<key>type</key>
<integer>0</integer>
<key>clipboardtext</key>
<string>{query}</string>
</dict>
<key>type</key>
<string>alfred.workflow.action.script</string>
<string>alfred.workflow.output.clipboard</string>
<key>uid</key>
<string>27E60581-8105-41DD-8E29-4FE811179098</string>
<string>DBA62127-3B78-4B80-B82B-1C6AEC393003</string>
<key>version</key>
<integer>0</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>autopaste</key>
<key>concurrently</key>
<false/>
<key>clipboardtext</key>
<string>{query}</string>
<key>escaping</key>
<integer>34</integer>
<key>script</key>
<string>/usr/bin/python splitargs.py "{query}" 1</string>
<key>type</key>
<integer>0</integer>
</dict>
<key>type</key>
<string>alfred.workflow.output.clipboard</string>
<string>alfred.workflow.action.script</string>
<key>uid</key>
<string>DBA62127-3B78-4B80-B82B-1C6AEC393003</string>
<string>27E60581-8105-41DD-8E29-4FE811179098</string>
<key>version</key>
<integer>0</integer>
</dict>
Expand All @@ -225,7 +219,7 @@ fi</string>
<key>concurrently</key>
<false/>
<key>escaping</key>
<integer>68</integer>
<integer>34</integer>
<key>script</key>
<string>/usr/bin/python splitargs.py "{query}" 1</string>
<key>type</key>
Expand Down Expand Up @@ -255,9 +249,9 @@ fi</string>
</dict>
</array>
<key>readme</key>
<string>有道翻译 workflow v1.1.0
<string>有道翻译 workflow v1.2.1
默认快捷键 "cd",查看翻译结果。
默认快捷键 "yd",查看翻译结果。
1. 英译中
2. 中译英
Expand All @@ -268,19 +262,20 @@ fi</string>
*功能*
1. 按`回车` 复制
2. 按`Ctrl+回车` 打开有道翻译页面
3. 按`Command+回车` 直接在打出翻译结果</string>
2. 按`Control+回车` 打开有道翻译页面
3. 按`Command+回车` 直接在打出翻译结果
4. 按`Shift+回车` 直接发音</string>
<key>uidata</key>
<dict>
<key>0907BEF4-816F-48FF-B157-03F5C2AACEAB</key>
<dict>
<key>ypos</key>
<real>370</real>
<real>390</real>
</dict>
<key>27E60581-8105-41DD-8E29-4FE811179098</key>
<dict>
<key>ypos</key>
<real>190</real>
<real>240</real>
</dict>
<key>4473C9D3-7A15-4D31-84F6-A096A7CFF46C</key>
<dict>
Expand All @@ -290,7 +285,7 @@ fi</string>
<key>7C1ABC41-3B36-401F-96C7-30BCB39181FF</key>
<dict>
<key>ypos</key>
<real>340</real>
<real>390</real>
</dict>
<key>91C343E7-50D8-4B0D-9034-1C16C20DA8D4</key>
<dict>
Expand All @@ -300,12 +295,12 @@ fi</string>
<key>9D65478B-1DB1-4198-B977-D538BCA74A4A</key>
<dict>
<key>ypos</key>
<real>30</real>
<real>10</real>
</dict>
<key>DBA62127-3B78-4B80-B82B-1C6AEC393003</key>
<dict>
<key>ypos</key>
<real>250</real>
<real>240</real>
</dict>
</dict>
<key>webaddress</key>
Expand Down
19 changes: 14 additions & 5 deletions splitargs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import sys
import os
from workflow import Workflow

reload(sys)
Expand All @@ -8,12 +9,20 @@

def getargs(wf):
query = sys.argv[1]
query = query.split(',')
part = sys.argv[2]
part = int(part)
print query[part]
query = query.split('$')
part = int(sys.argv[2])

if part == 1:
print query[1]
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()
sys.exit(wf.run(getargs))
sys.exit(wf.run(getargs))
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0
1.2.1
Binary file modified whyliam.workflows.youdao.alfredworkflow
Binary file not shown.
45 changes: 32 additions & 13 deletions youdao.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def get_web_data(query):


def main(wf):

query = wf.args[0].strip().replace("\\", "")

if not query:
Expand All @@ -35,14 +34,21 @@ def main(wf):

s = get_web_data(query)

# '翻译结果'
title = s["translation"]
title = ''.join(title).encode("UTF-8")
url = u'http://dict.youdao.com/search?q=' + query

if 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 = url + "," + title

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_DEFAULT)

Expand All @@ -56,32 +62,45 @@ def main(wf):
title += (" [英: " + s["basic"]["uk-phonetic"] + "]")
title = title if title else "[" + s["basic"]["phonetic"] + "]"
subtitle = '有道发音'
arg = url + "," + query
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 = url + "," + title
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).encode("UTF-8")
title = ', '.join(title)
subtitle = '网络翻译: ' + s["web"][w]["key"]
arg = url + "," + title

if tran == 'EtC':
key = ''.join(s["web"][w]["key"])
arg = [query, title, key, ' ']
else:
value = ' '.join(s["web"][w]["value"])
arg = [query, title, value, ' ']

arg = '$'.join(arg)
wf.add_item(
title=title, subtitle=subtitle, arg=arg, valid=True, icon=ICON_WEB)

else:
title = '有道也翻译不出来了'
subtitle = '尝试一下去网站搜索'
arg = url + "," + title
arg = [query, ' ', ' ', ' ']
arg = '$'.join(arg)
wf.add_item(
title=title, subtitle=subtitle, arg=arg, valid=True, icon=ICON_DEFAULT)

Expand Down

0 comments on commit bb1293e

Please sign in to comment.