mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
Add manually check updates option
1. manually check updates 2. refactoring get http request to use async 3. remove some generic exception catch 4. remove unused code
This commit is contained in:
@@ -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<Result> ResultsFromSuggestions(string keyword, string subtitle, WebSearch webSearch)
|
||||
private async Task<IEnumerable<Result>> 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,
|
||||
|
||||
@@ -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<string> GetSuggestions(string query)
|
||||
public override async Task<List<string>> 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<string>();
|
||||
|
||||
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<string>();
|
||||
}
|
||||
|
||||
if (json != null)
|
||||
{
|
||||
|
||||
@@ -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<string> GetSuggestions(string query)
|
||||
public override async Task<List<string>> 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<string>();
|
||||
|
||||
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<string>();
|
||||
}
|
||||
if (json != null)
|
||||
{
|
||||
var results = json[1] as JContainer;
|
||||
if (results != null)
|
||||
{
|
||||
var results = json[1] as JContainer;
|
||||
if (results != null)
|
||||
{
|
||||
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
|
||||
}
|
||||
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
|
||||
@@ -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<string> GetSuggestions(string query);
|
||||
public abstract Task<List<string>> GetSuggestions(string query);
|
||||
|
||||
public static SuggestionSource GetSuggestionSource(string name, PluginInitContext context)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user