From 83e199a0de8d2be8acedfdfde02f8e23c71cfd8a Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Mon, 21 Jul 2014 22:27:57 +0800 Subject: [PATCH] #129 Make web search suggestion optional and add baidu suggeestion source. --- Wox.Infrastructure/HttpRequest.cs | 27 +-------- .../UserSettings/UserSettingStorage.cs | 6 ++ .../SuggestionSources/Baidu.cs | 54 ++++++++++++++++++ .../SuggestionSourceFactory.cs | 25 +++++++++ .../WebSearch/WebSearchPlugin.cs | 37 ++++++------ .../WebSearch/WebSearchesSetting.xaml | 12 +++- .../WebSearch/WebSearchesSetting.xaml.cs | 41 +++++++++++++- .../Wox.Plugin.SystemPlugins.csproj | 2 + Wox/MainWindow.xaml.cs | 56 ++++++++++++++----- Wox/ResultPanel.xaml.cs | 2 +- 10 files changed, 203 insertions(+), 59 deletions(-) create mode 100644 Wox.Plugin.SystemPlugins/SuggestionSources/Baidu.cs create mode 100644 Wox.Plugin.SystemPlugins/SuggestionSources/SuggestionSourceFactory.cs diff --git a/Wox.Infrastructure/HttpRequest.cs b/Wox.Infrastructure/HttpRequest.cs index 1e53db6d0b..e082d07525 100644 --- a/Wox.Infrastructure/HttpRequest.cs +++ b/Wox.Infrastructure/HttpRequest.cs @@ -11,21 +11,11 @@ using System.Text; //From:http://blog.csdn.net/zhoufoxcn/article/details/6404236 namespace Wox.Infrastructure { - /// - /// 有关HTTP请求的辅助类 - /// public class HttpRequest { private static readonly string DefaultUserAgent = "Wox/" + Assembly.GetEntryAssembly().GetName().Version.ToString() + " (+https://github.com/qianlifeng/Wox)"; - /// - /// 创建GET方式的HTTP请求 - /// - /// 请求的URL - /// 请求的超时时间 - /// 请求的客户端浏览器信息,可以为空 - /// 随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空 - /// + public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies) { if (string.IsNullOrEmpty(url)) @@ -50,16 +40,7 @@ namespace Wox.Infrastructure } return request.GetResponse() as HttpWebResponse; } - /// - /// 创建POST方式的HTTP请求 - /// - /// 请求的URL - /// 随同请求POST的参数名称及参数值字典 - /// 请求的超时时间 - /// 请求的客户端浏览器信息,可以为空 - /// 发送HTTP请求时所用的编码 - /// 随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空 - /// + public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies) { if (string.IsNullOrEmpty(url)) @@ -71,7 +52,6 @@ namespace Wox.Infrastructure throw new ArgumentNullException("requestEncoding"); } HttpWebRequest request = null; - //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); @@ -103,7 +83,6 @@ namespace Wox.Infrastructure request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } - //如果需要POST数据 if (!(parameters == null || parameters.Count == 0)) { StringBuilder buffer = new StringBuilder(); @@ -131,7 +110,7 @@ namespace Wox.Infrastructure private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { - return true; //总是接受 + return true; } } } diff --git a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs index 18d1962995..8350fce8a6 100644 --- a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs +++ b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs @@ -70,6 +70,12 @@ namespace Wox.Infrastructure.Storage.UserSettings [JsonProperty] public OpacityMode OpacityMode { get; set; } + [JsonProperty] + public bool EnableWebSearchSuggestion { get; set; } + + [JsonProperty] + public string WebSearchSuggestionSource { get; set; } + [JsonProperty] public bool LeaveCmdOpen { get; set; } diff --git a/Wox.Plugin.SystemPlugins/SuggestionSources/Baidu.cs b/Wox.Plugin.SystemPlugins/SuggestionSources/Baidu.cs new file mode 100644 index 0000000000..2b4c03dfc1 --- /dev/null +++ b/Wox.Plugin.SystemPlugins/SuggestionSources/Baidu.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Xml; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; +using Wox.Infrastructure; +using YAMP.Numerics; + +namespace Wox.Plugin.SystemPlugins.SuggestionSources +{ + public class Baidu : AbstractSuggestionSource + { + Regex reg = new Regex("window.baidu.sug\\((.*)\\)"); + + public override List GetSuggestions(string query) + { + try + { + var response = + HttpRequest.CreateGetHttpResponse( + "http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), null, + null, null); + var stream = response.GetResponseStream(); + + if (stream != null) + { + var body = new StreamReader(stream, Encoding.GetEncoding("GB2312")).ReadToEnd(); + Match m = reg.Match(body); + if (m.Success) + { + var json = JsonConvert.DeserializeObject(m.Groups[1].Value) as JContainer; + if (json != null) + { + var results = json["s"] as JArray; + if (results != null) + { + return results.OfType().Select(o => o.Value).OfType().ToList(); + } + } + } + } + } + catch + { } + + return null; + } + } +} diff --git a/Wox.Plugin.SystemPlugins/SuggestionSources/SuggestionSourceFactory.cs b/Wox.Plugin.SystemPlugins/SuggestionSources/SuggestionSourceFactory.cs new file mode 100644 index 0000000000..3262320b94 --- /dev/null +++ b/Wox.Plugin.SystemPlugins/SuggestionSources/SuggestionSourceFactory.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Wox.Plugin.SystemPlugins.SuggestionSources +{ + public class SuggestionSourceFactory + { + public static ISuggestionSource GetSuggestionSource(string name) + { + switch (name.ToLower()) + { + case "google": + return new Google(); + + case "baidu": + return new Baidu(); + + default: + return null; + } + } + } +} diff --git a/Wox.Plugin.SystemPlugins/WebSearch/WebSearchPlugin.cs b/Wox.Plugin.SystemPlugins/WebSearch/WebSearchPlugin.cs index b7afa08dd2..97fe4e505e 100644 --- a/Wox.Plugin.SystemPlugins/WebSearch/WebSearchPlugin.cs +++ b/Wox.Plugin.SystemPlugins/WebSearch/WebSearchPlugin.cs @@ -32,7 +32,7 @@ namespace Wox.Plugin.SystemPlugins title = subtitle; subtitle = null; } - context.API.PushResults(query,context.CurrentPluginMetadata, new List() + context.API.PushResults(query, context.CurrentPluginMetadata, new List() { new Result() { @@ -48,24 +48,29 @@ namespace Wox.Plugin.SystemPlugins } }); - if (!string.IsNullOrEmpty(keyword)) + if (UserSettingStorage.Instance.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword)) { - ISuggestionSource sugg = new Google(); - var result = sugg.GetSuggestions(keyword); - if (result != null) + ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource( + UserSettingStorage.Instance.WebSearchSuggestionSource); + if (sugg != null) { - context.API.PushResults(query,context.CurrentPluginMetadata, result.Select(o => new Result() + var result = sugg.GetSuggestions(keyword); + if (result != null) { - Title = o, - SubTitle = subtitle, - Score = 5, - IcoPath = webSearch.IconPath, - Action = (c) => - { - Process.Start(webSearch.Url.Replace("{q}", o)); - return true; - } - }).ToList()); + context.API.PushResults(query, context.CurrentPluginMetadata, + result.Select(o => new Result() + { + Title = o, + SubTitle = subtitle, + Score = 5, + IcoPath = webSearch.IconPath, + Action = (c) => + { + Process.Start(webSearch.Url.Replace("{q}", o)); + return true; + } + }).ToList()); + } } } } diff --git a/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml b/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml index 419d350076..8e24a5993e 100644 --- a/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml +++ b/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml @@ -7,10 +7,16 @@ d:DesignHeight="300" d:DesignWidth="300"> - + + - + + Enable search suggestions + + + + @@ -30,7 +36,7 @@ - +