-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathmain.go
168 lines (148 loc) · 3.34 KB
/
main.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/zgs225/alfred-youdao/alfred"
"github.com/zgs225/youdao"
)
const (
APPID = ""
APPSECRET = ""
MAX_LEN = 255
UPDATECMD = "alfred-youdao:update"
)
func init() {
log.SetPrefix("[i] ")
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func main() {
log.Println(os.Args)
items := alfred.NewResult()
appID := os.Getenv("zhiyun_id")
appKey := os.Getenv("zhiyun_key")
if appID == "" || appKey == "" {
items.Append(&alfred.ResultElement{
Valid: false,
Title: "错误: 请设置有道API",
Subtitle: "有道词典",
})
items.End()
}
client := &youdao.Client{
AppID: appID,
AppSecret: appKey,
}
agent := newAgent(client)
q, from, to, lang := parseArgs(os.Args)
if lang {
if err := agent.Client.SetFrom(from); err != nil {
items.Append(&alfred.ResultElement{
Valid: true,
Title: fmt.Sprintf("错误: 源语言不支持[%s]", from),
Subtitle: `有道词典`,
})
items.End()
}
if err := agent.Client.SetTo(to); err != nil {
items.Append(&alfred.ResultElement{
Valid: true,
Title: fmt.Sprintf("错误: 目标语言不支持[%s]", to),
Subtitle: `有道词典`,
})
items.End()
}
}
if len(q) == 0 {
items.Append(&alfred.ResultElement{
Valid: true,
Title: "有道词典",
Subtitle: `查看"..."的解释或翻译`,
})
items.End()
}
if len(q) > 255 {
items.Append(&alfred.ResultElement{
Valid: false,
Title: "错误: 最大查询字符数为255",
Subtitle: q,
})
items.End()
}
r, err := agent.Query(q)
if err != nil {
panic(err)
}
mod := map[string]*alfred.ModElement{
alfred.Mods_Shift: &alfred.ModElement{
Valid: true,
Arg: toYoudaoDictUrl(q),
Subtitle: "回车键打开词典网页",
},
}
if r.Basic != nil {
phonetic := joinPhonetic(r.Basic.Phonetic, r.Basic.UkPhonetic, r.Basic.UsPhonetic)
for _, title := range r.Basic.Explains {
mod2 := copyModElementMap(mod)
mod2[alfred.Mods_Cmd] = &alfred.ModElement{
Valid: true,
Arg: wordsToSayCmdOption(title, r),
Subtitle: "发音",
}
item := alfred.ResultElement{
Valid: true,
Title: title,
Subtitle: phonetic,
Arg: title,
Mods: mod2,
}
items.Append(&item)
}
}
if r.Translation != nil {
title := strings.Join(*r.Translation, "; ")
mod2 := copyModElementMap(mod)
mod2[alfred.Mods_Cmd] = &alfred.ModElement{
Valid: true,
Arg: wordsToSayCmdOption(title, r),
Subtitle: "发音",
}
item := alfred.ResultElement{
Valid: true,
Title: title,
Subtitle: "翻译结果",
Arg: title,
Mods: mod2,
}
items.Append(&item)
}
if r.Web != nil {
items.Append(&alfred.ResultElement{
Valid: true,
Title: "网络释义",
Subtitle: "有道词典 for Alfred",
})
for _, elem := range *r.Web {
mod2 := copyModElementMap(mod)
mod2[alfred.Mods_Cmd] = &alfred.ModElement{
Valid: true,
Arg: wordsToSayCmdOption(elem.Key, r),
Subtitle: "发音",
}
items.Append(&alfred.ResultElement{
Valid: true,
Title: elem.Key,
Subtitle: strings.Join(elem.Value, "; "),
Arg: elem.Key,
Mods: mod,
})
}
}
if agent.Dirty {
if err := agent.Cache.SaveFile(CACHE_FILE); err != nil {
log.Println(err)
}
}
items.End()
}