mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-14 03:37:10 +08:00
6e13440f1f
1. #486 2. fix #778 #763 #742 3. MVVM refactoring 4. remove IMultipleActionKeywords interface, use PluginManager directly
68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
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
|
|
{
|
|
public class Baidu : SuggestionSource
|
|
{
|
|
private readonly Regex _reg = new Regex("window.baidu.sug\\((.*)\\)");
|
|
|
|
public override async Task<List<string>> Suggestions(string query)
|
|
{
|
|
string result;
|
|
|
|
try
|
|
{
|
|
const string api = "http://suggestion.baidu.com/su?json=1&wd=";
|
|
result = await Http.Get(api + Uri.EscapeUriString(query), "GB2312");
|
|
}
|
|
catch (WebException e)
|
|
{
|
|
Log.Warn("Can't get suggestion from baidu");
|
|
Log.Exception(e);
|
|
return new List<string>();
|
|
;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(result)) return new List<string>();
|
|
Match match = _reg.Match(result);
|
|
if (match.Success)
|
|
{
|
|
JContainer json;
|
|
try
|
|
{
|
|
json = JsonConvert.DeserializeObject(match.Groups[1].Value) as JContainer;
|
|
}
|
|
catch (JsonSerializationException e)
|
|
{
|
|
Log.Exception(e);
|
|
return new List<string>();
|
|
}
|
|
|
|
if (json != null)
|
|
{
|
|
var results = json["s"] as JArray;
|
|
if (results != null)
|
|
{
|
|
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
|
|
}
|
|
}
|
|
}
|
|
|
|
return new List<string>();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return "Baidu";
|
|
}
|
|
}
|
|
} |