-
Notifications
You must be signed in to change notification settings - Fork 19
/
Main.cs
162 lines (146 loc) · 4.91 KB
/
Main.cs
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
using System.Collections.Generic;
using System.Windows;
using Newtonsoft.Json;
using Wox.Infrastructure.Http;
namespace Wox.Plugin.Youdao
{
public class TranslateResult
{
public int errorCode { get; set; }
public List<string> translation { get; set; }
public BasicTranslation basic { get; set; }
public List<WebTranslation> web { get; set; }
}
// 有道词典-基本词典
public class BasicTranslation
{
public string phonetic { get; set; }
public List<string> explains { get; set; }
}
public class WebTranslation
{
public string key { get; set; }
public List<string> value { get; set; }
}
public class Main : IPlugin
{
private const string TranslateUrl = "http://fanyi.youdao.com/openapi.do?keyfrom=WoxLauncher&key=1247918016&type=data&doctype=json&version=1.1&q=";
private PluginInitContext _context;
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
const string ico = "Images\\youdao.ico";
if (query.Search.Length == 0)
{
results.Add(new Result
{
Title = "开始有道中英互译",
SubTitle = "基于有道网页 API",
IcoPath = ico
});
return results;
}
var json = Http.Get(TranslateUrl + query.Search).Result;
TranslateResult o = JsonConvert.DeserializeObject<TranslateResult>(json);
if (o.errorCode == 0)
{
if(o.translation != null)
{
var translation = string.Join(", ", o.translation.ToArray());
var title = translation;
if (o.basic?.phonetic != null)
{
title += " [" + o.basic.phonetic + "]";
}
results.Add(new Result
{
Title = title,
SubTitle = "翻译结果",
IcoPath = ico,
Action = this.copyToClipboardFunc(translation)
});
}
if(o.basic?.explains != null)
{
var explantion = string.Join(",", o.basic.explains.ToArray());
results.Add(new Result
{
Title = explantion,
SubTitle = "简明释义",
IcoPath = ico,
Action = this.copyToClipboardFunc(explantion)
});
}
if (o.web != null)
{
foreach (WebTranslation t in o.web)
{
var translation = string.Join(",", t.value.ToArray());
results.Add(new Result
{
Title = translation,
SubTitle = "网络释义:"+ t.key,
IcoPath = ico,
Action = this.copyToClipboardFunc(translation)
});
}
}
}
else
{
string error = string.Empty;
switch (o.errorCode)
{
case 20:
error = "要翻译的文本过长";
break;
case 30:
error = "无法进行有效的翻译";
break;
case 40:
error = "不支持的语言类型";
break;
case 50:
error = "无效的key";
break;
}
results.Add(new Result
{
Title = error,
IcoPath = ico
});
}
return results;
}
public void Init(PluginInitContext context)
{
_context = context;
}
private System.Func<ActionContext, bool> copyToClipboardFunc(string text)
{
return c =>
{
if (this.copyToClipboard(text))
{
_context.API.ShowMsg("翻译已被存入剪贴板");
}
else
{
_context.API.ShowMsg("剪贴板打开失败,请稍后再试");
}
return false;
};
}
private bool copyToClipboard(string text)
{
try
{
Clipboard.SetText(text);
}catch(System.Exception e)
{
return false;
}
return true;
}
}
}