PowerToys/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs

48 lines
1.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2014-12-21 22:03:03 +08:00
using Wox.Infrastructure.Http;
namespace Wox.Plugin.WebSearch.SuggestionSources
{
public class Baidu : AbstractSuggestionSource
{
Regex reg = new Regex("window.baidu.sug\\((.*)\\)");
public override List<string> GetSuggestions(string query)
{
var result = HttpRequest.Get("http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), Proxy, "GB2312");
2014-12-21 22:03:03 +08:00
if (string.IsNullOrEmpty(result)) return new List<string>();
Match match = reg.Match(result);
if (match.Success)
{
2014-12-21 22:03:03 +08:00
JContainer json = null;
try
{
json = JsonConvert.DeserializeObject(match.Groups[1].Value) as JContainer;
2014-12-21 22:03:03 +08:00
}
catch { }
2014-12-21 22:03:03 +08:00
if (json != null)
{
2014-12-21 22:03:03 +08:00
var results = json["s"] as JArray;
if (results != null)
{
2014-12-21 22:03:03 +08:00
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
}
}
}
2014-12-21 22:03:03 +08:00
return new List<string>();
}
public Baidu(IHttpProxy httpProxy) : base(httpProxy)
{
}
}
}