-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathsplitargs.py
executable file
·69 lines (53 loc) · 1.62 KB
/
splitargs.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
# -*- coding: utf-8 -*-
import sys
import os
import re
from workflow import Workflow3
def escape_quote(result):
return result.replace("\'", "\\\'").replace("\"", "\\\"")
def get_args(wf):
query = sys.argv[1]
arg_array = query.split('$%')
command = str(sys.argv[2])
try:
version, query, result, query_language, pronounce, operation = arg_array
# 是否有错误
if operation == 'error':
import webbrowser
url = "https://blog.naaln.com/2017/04/alfred-youdao-intro/"
webbrowser.open(url)
return
# 查询的单词
if command == 'search':
sys.stdout.write(query)
# 翻译的结果
elif command == 'copy':
sys.stdout.write(result)
# 发音
elif command == "pronounce":
language = detect_language(query)
bashCommand = f"say --voice='{get_voice(language)}' {escape_quote(query)}"
os.system(bashCommand)
except Exception:
return
def detect_language(query):
if re.match(r'^[a-zA-Z]+$', query):
return 'EN'
elif re.match(r'^[\u4e00-\u9fa5]+$', query):
return 'zh-CHS'
elif re.match(r'^[\uac00-\ud7af]+$', query):
return 'KO'
elif re.match(r'^[\u3040-\u309F\u30A0-\u30FF]+$', query):
return 'JA'
return 'UNKNOWN'
def get_voice(data_form):
voices = {
'EN': 'Samantha',
'KO': 'Yuna',
'zh-CHS': 'Ting-Ting',
'JA': 'Kyoko'
}
return voices.get(data_form, 'Ting-Ting')
if __name__ == '__main__':
wf = Workflow3()
sys.exit(wf.run(get_args))