diff --git a/Plugins/Wox.Plugin.WebSearch/Main.cs b/Plugins/Wox.Plugin.WebSearch/Main.cs index 5d08fa5acc..e99d615295 100644 --- a/Plugins/Wox.Plugin.WebSearch/Main.cs +++ b/Plugins/Wox.Plugin.WebSearch/Main.cs @@ -85,10 +85,10 @@ namespace Wox.Plugin.WebSearch if (_settings.EnableWebSearchSuggestion) { const int waittime = 300; - var task = Task.Run(() => + var task = Task.Run(async () => { - results.AddRange(ResultsFromSuggestions(keyword, subtitle, webSearch)); - + var suggestions = await Suggestions(keyword, subtitle, webSearch); + results.AddRange(suggestions); }, _updateToken); if (!task.Wait(waittime)) @@ -102,12 +102,12 @@ namespace Wox.Plugin.WebSearch } } - private IEnumerable ResultsFromSuggestions(string keyword, string subtitle, WebSearch webSearch) + private async Task> Suggestions(string keyword, string subtitle, WebSearch webSearch) { var source = SuggestionSource.GetSuggestionSource(_settings.WebSearchSuggestionSource, Context); - var suggestions = source?.GetSuggestions(keyword); - if (suggestions != null) + if (source != null) { + var suggestions = await source.GetSuggestions(keyword); var resultsFromSuggestion = suggestions.Select(o => new Result { Title = o, diff --git a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs index c4a472ac97..d05d7287f1 100644 --- a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs +++ b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs @@ -2,9 +2,11 @@ using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; +using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Wox.Infrastructure.Http; +using Wox.Infrastructure.Logger; namespace Wox.Plugin.WebSearch.SuggestionSources { @@ -14,20 +16,24 @@ namespace Wox.Plugin.WebSearch.SuggestionSources Regex reg = new Regex("window.baidu.sug\\((.*)\\)"); - public override List GetSuggestions(string query) + public override async Task> GetSuggestions(string query) { - var result = HttpRequest.Get("http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), Proxy, "GB2312"); + var result = await HttpRequest.Get("http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), Proxy, "GB2312"); if (string.IsNullOrEmpty(result)) return new List(); Match match = reg.Match(result); if (match.Success) { - JContainer json = null; + JContainer json; try { json = JsonConvert.DeserializeObject(match.Groups[1].Value) as JContainer; } - catch { } + catch (JsonSerializationException e) + { + Log.Error(e); + return new List(); + } if (json != null) { diff --git a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs index fd732375f5..ab569219c3 100644 --- a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs @@ -1,34 +1,39 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Wox.Infrastructure.Http; +using Wox.Infrastructure.Logger; namespace Wox.Plugin.WebSearch.SuggestionSources { public class Google : SuggestionSource { public override string Domain { get; set; } = "www.google.com"; - public override List GetSuggestions(string query) + public override async Task> GetSuggestions(string query) { - var result = HttpRequest.Get("https://www.google.com/complete/search?output=chrome&q=" + Uri.EscapeUriString(query), Proxy); + var result = await HttpRequest.Get("https://www.google.com/complete/search?output=chrome&q=" + Uri.EscapeUriString(query), Proxy); if (string.IsNullOrEmpty(result)) return new List(); - + JContainer json; try { - JContainer json = JsonConvert.DeserializeObject(result) as JContainer; - if (json != null) + json = JsonConvert.DeserializeObject(result) as JContainer; + } + catch (JsonSerializationException e) + { + Log.Error(e); + return new List(); + } + if (json != null) + { + var results = json[1] as JContainer; + if (results != null) { - var results = json[1] as JContainer; - if (results != null) - { - return results.OfType().Select(o => o.Value).OfType().ToList(); - } + return results.OfType().Select(o => o.Value).OfType().ToList(); } } - catch { } - return new List(); } diff --git a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/ISuggestionSource.cs b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/ISuggestionSource.cs index 75556602ee..8c654ecd44 100644 --- a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/ISuggestionSource.cs +++ b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/ISuggestionSource.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading.Tasks; namespace Wox.Plugin.WebSearch.SuggestionSources { @@ -12,7 +13,7 @@ namespace Wox.Plugin.WebSearch.SuggestionSources Proxy = httpProxy; } - public abstract List GetSuggestions(string query); + public abstract Task> GetSuggestions(string query); public static SuggestionSource GetSuggestionSource(string name, PluginInitContext context) { diff --git a/Wox.Core/APIServer.cs b/Wox.Core/APIServer.cs deleted file mode 100644 index 7830d091ae..0000000000 --- a/Wox.Core/APIServer.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Wox.Core -{ - public static class APIServer - { - private static string BaseAPIURL = "http://api.getwox.com"; - public static string ErrorReportURL = BaseAPIURL + "/error/"; - public static string LastestReleaseURL = BaseAPIURL + "/release/latest/"; - } -} diff --git a/Wox.Core/Wox.Core.csproj b/Wox.Core/Wox.Core.csproj index 099a5bf468..92b58b27c1 100644 --- a/Wox.Core/Wox.Core.csproj +++ b/Wox.Core/Wox.Core.csproj @@ -64,7 +64,6 @@ Properties\SolutionAssemblyInfo.cs - diff --git a/Wox.Infrastructure/Http/HttpRequest.cs b/Wox.Infrastructure/Http/HttpRequest.cs index 1142f217fb..6dd7030afc 100644 --- a/Wox.Infrastructure/Http/HttpRequest.cs +++ b/Wox.Infrastructure/Http/HttpRequest.cs @@ -1,120 +1,79 @@ -using System.IO; +using System; +using System.IO; using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; using System.Text; +using System.Threading.Tasks; +using JetBrains.Annotations; using Wox.Infrastructure.Logger; using Wox.Plugin; namespace Wox.Infrastructure.Http { - public class HttpRequest + public static class HttpRequest { - public static string Get(string url, IHttpProxy proxy, string encoding = "UTF-8") - { - return Get(url, encoding, proxy); - } - - public static WebProxy GetWebProxy(IHttpProxy proxy) + private static WebProxy GetWebProxy(IHttpProxy proxy) { if (proxy != null && proxy.Enabled && !string.IsNullOrEmpty(proxy.Server)) { if (string.IsNullOrEmpty(proxy.UserName) || string.IsNullOrEmpty(proxy.Password)) { - return new WebProxy(proxy.Server, proxy.Port); - } - - return new WebProxy(proxy.Server, proxy.Port) - { - Credentials = new NetworkCredential(proxy.UserName, proxy.Password) - }; - } - - return null; - } - - private static string Get(string url, string encoding, IHttpProxy proxy) - { - if (string.IsNullOrEmpty(url)) return string.Empty; - - HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; - request.Method = "GET"; - request.Timeout = 10 * 1000; - request.Proxy = GetWebProxy(proxy); - - try - { - HttpWebResponse response = request.GetResponse() as HttpWebResponse; - if (response != null) - { - Stream stream = response.GetResponseStream(); - if (stream != null) - { - using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding))) - { - return reader.ReadToEnd(); - } - } - } - } - catch (System.Exception e) - { - Log.Error(e); - return string.Empty; - } - - return string.Empty; - } - - public static string Post(string url, string jsonData, IHttpProxy proxy) - { - if (string.IsNullOrEmpty(url)) return string.Empty; - - HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; - request.Method = "POST"; - request.ContentType = "text/json"; - request.Timeout = 10 * 1000; - if (proxy != null && proxy.Enabled && !string.IsNullOrEmpty(proxy.Server)) - { - if (string.IsNullOrEmpty(proxy.UserName) || string.IsNullOrEmpty(proxy.Password)) - { - request.Proxy = new WebProxy(proxy.Server, proxy.Port); + var webProxy = new WebProxy(proxy.Server, proxy.Port); + return webProxy; } else { - request.Proxy = new WebProxy(proxy.Server, proxy.Port) + var webProxy = new WebProxy(proxy.Server, proxy.Port) { Credentials = new NetworkCredential(proxy.UserName, proxy.Password) }; + return webProxy; } } - using (var streamWriter = new StreamWriter(request.GetRequestStream())) + else { - streamWriter.Write(jsonData); - streamWriter.Flush(); - streamWriter.Close(); + return null; } + } + public static async Task Get([NotNull] string url, IHttpProxy proxy, string encoding = "UTF-8") + { + + HttpWebRequest request = WebRequest.CreateHttp(url); + request.Method = "GET"; + request.Timeout = 10 * 1000; + request.Proxy = GetWebProxy(proxy); + request.UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko"; + HttpWebResponse response; try { - HttpWebResponse response = request.GetResponse() as HttpWebResponse; - if (response != null) - { - Stream stream = response.GetResponseStream(); - if (stream != null) - { - using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8"))) - { - return reader.ReadToEnd(); - } - } - } + response = await request.GetResponseAsync() as HttpWebResponse; } - catch (System.Exception e) + catch (WebException e) { Log.Error(e); return string.Empty; } - - return string.Empty; + if (response != null) + { + var stream = response.GetResponseStream(); + if (stream != null) + { + using (var reader = new StreamReader(stream, Encoding.GetEncoding(encoding))) + { + return await reader.ReadToEndAsync(); + } + } + else + { + return string.Empty; + } + } + else + { + return string.Empty; + } } } } \ No newline at end of file diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index dcdb0e2be5..77653ce8a9 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -57,6 +57,7 @@ + diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 842bd91a40..3bd70a046e 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -64,7 +64,15 @@ namespace Wox } catch (Exception exception) { - Log.Error(exception); + const string info = "Update.exe not found, not a Squirrel-installed app?"; + if (exception.Message == info) + { + Log.Warn("info"); + } + else + { + throw; + } } } diff --git a/Wox/Languages/en.xaml b/Wox/Languages/en.xaml index 71065ba6b6..35d0a80db1 100644 --- a/Wox/Languages/en.xaml +++ b/Wox/Languages/en.xaml @@ -78,6 +78,9 @@ About Website Version + Check Updates + New Version {0} avaiable, please restart + Release Notes: You have activated Wox {0} times diff --git a/Wox/SettingWindow.xaml b/Wox/SettingWindow.xaml index 9fd385ecfe..45bd9442f8 100644 --- a/Wox/SettingWindow.xaml +++ b/Wox/SettingWindow.xaml @@ -340,27 +340,42 @@ - - + + + + - - - - - - - - - + + + + + https://github.com/Wox-launcher/Wox + + + + + + + + + + + https://github.com/Wox-launcher/Wox/releases/latest + + + +