Files
PowerToys/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs
bao-qian 6ac33b0568 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
2016-05-09 02:32:47 +01:00

56 lines
1.7 KiB
C#

using System;
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
{
public class Baidu : SuggestionSource
{
public override string Domain { get; set; } = "www.baidu.com";
Regex reg = new Regex("window.baidu.sug\\((.*)\\)");
public override async Task<List<string>> GetSuggestions(string query)
{
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;
try
{
json = JsonConvert.DeserializeObject(match.Groups[1].Value) as JContainer;
}
catch (JsonSerializationException e)
{
Log.Error(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 Baidu(IHttpProxy httpProxy) : base(httpProxy)
{
}
}
}