PowerToys/Plugins/WinAlfred.Plugin.Fanyi/Main.cs

117 lines
3.7 KiB
C#
Raw Normal View History

2014-01-05 17:56:02 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
2014-01-07 23:27:05 +08:00
using System.Threading;
2014-01-05 17:56:02 +08:00
using System.Windows.Forms;
2014-01-07 23:27:05 +08:00
using System.Windows.Forms.VisualStyles;
2014-01-05 17:56:02 +08:00
using Newtonsoft.Json;
namespace WinAlfred.Plugin.Fanyi
{
public class TranslateResult
{
public string from { get; set; }
public string to { get; set; }
public List<SrcDst> trans_result { get; set; }
}
public class SrcDst
{
public string src { get; set; }
public string dst { get; set; }
}
public class Main : IPlugin
{
private string translateURL = "http://openapi.baidu.com/public/2.0/bmt/translate";
private string baiduKey = "SnPcDY3iH5jDbklRewkG2D2v";
static public string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
if (query.ActionParameters.Count == 0)
{
results.Add(new Result()
{
Title = "Start to translate between Chinese and English",
SubTitle = "Powered by baidu api",
IcoPath = "Images\\translate.png"
});
return results;
}
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("from", "auto");
data.Add("to", "auto");
data.Add("q", query.RawQuery.Substring(3));
data.Add("client_id", baiduKey);
HttpWebResponse response = HttpRequest.CreatePostHttpResponse(translateURL, data, null, null, Encoding.UTF8, null);
Stream s = response.GetResponseStream();
if (s != null)
{
StreamReader reader = new StreamReader(s, Encoding.UTF8);
string json = reader.ReadToEnd();
TranslateResult o = JsonConvert.DeserializeObject<TranslateResult>(json);
foreach (SrcDst srcDst in o.trans_result)
{
string dst = srcDst.dst;
results.Add(new Result()
{
Title = dst,
SubTitle = "Copy to clipboard",
IcoPath = "Images\\translate.png",
Action = () =>
{
Clipboard.SetText(dst);
context.ShowMsg("translation has been copyed to your clipboard.", "",
AssemblyDirectory + "\\Images\\translate.png");
}
});
}
}
return results;
}
public static string GetHtmlStr(string url)
{
try
{
WebRequest rGet = WebRequest.Create(url);
WebResponse rSet = rGet.GetResponse();
Stream s = rSet.GetResponseStream();
StreamReader reader = new StreamReader(s, Encoding.UTF8);
return reader.ReadToEnd();
}
catch (WebException)
{
//连接失败
return null;
}
}
public void Init(PluginInitContext context)
{
this.context = context;
}
private PluginInitContext context { get; set; }
}
}