2014-07-21 22:27:57 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2016-05-11 03:26:47 +08:00
|
|
|
|
using System.Net;
|
2014-07-21 22:27:57 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
2016-05-09 09:32:47 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2014-07-21 22:27:57 +08:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2014-12-21 22:03:03 +08:00
|
|
|
|
using Wox.Infrastructure.Http;
|
2016-05-09 09:32:47 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2014-07-21 22:27:57 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
namespace Wox.Plugin.WebSearch.SuggestionSources
|
2014-07-21 22:27:57 +08:00
|
|
|
|
{
|
2016-05-05 23:08:44 +08:00
|
|
|
|
public class Baidu : SuggestionSource
|
2014-07-21 22:27:57 +08:00
|
|
|
|
{
|
2016-05-05 23:08:44 +08:00
|
|
|
|
public override string Domain { get; set; } = "www.baidu.com";
|
|
|
|
|
|
2014-07-21 22:27:57 +08:00
|
|
|
|
Regex reg = new Regex("window.baidu.sug\\((.*)\\)");
|
|
|
|
|
|
2016-05-09 09:32:47 +08:00
|
|
|
|
public override async Task<List<string>> GetSuggestions(string query)
|
2014-07-21 22:27:57 +08:00
|
|
|
|
{
|
2016-05-11 03:26:47 +08:00
|
|
|
|
string result;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
const string api = "http://suggestion.baidu.com/su?json=1&wd=";
|
|
|
|
|
result = await Http.Get(api + Uri.EscapeUriString(query), Proxy, "GB2312");
|
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn("Can't get suggestion from baidu");
|
|
|
|
|
Log.Error(e);
|
|
|
|
|
return new List<string>(); ;
|
|
|
|
|
}
|
|
|
|
|
|
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-07-21 22:27:57 +08:00
|
|
|
|
{
|
2016-05-09 09:32:47 +08:00
|
|
|
|
JContainer json;
|
2014-12-21 22:03:03 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-01-05 22:41:17 +08:00
|
|
|
|
json = JsonConvert.DeserializeObject(match.Groups[1].Value) as JContainer;
|
2014-12-21 22:03:03 +08:00
|
|
|
|
}
|
2016-05-09 09:32:47 +08:00
|
|
|
|
catch (JsonSerializationException e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(e);
|
|
|
|
|
return new List<string>();
|
|
|
|
|
}
|
2014-07-21 22:27:57 +08:00
|
|
|
|
|
2014-12-21 22:03:03 +08:00
|
|
|
|
if (json != null)
|
2014-07-21 22:27:57 +08:00
|
|
|
|
{
|
2014-12-21 22:03:03 +08:00
|
|
|
|
var results = json["s"] as JArray;
|
|
|
|
|
if (results != null)
|
2014-07-21 22:27:57 +08:00
|
|
|
|
{
|
2014-12-21 22:03:03 +08:00
|
|
|
|
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
|
2014-07-21 22:27:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-21 22:03:03 +08:00
|
|
|
|
return new List<string>();
|
2014-07-21 22:27:57 +08:00
|
|
|
|
}
|
2015-02-20 21:14:15 +08:00
|
|
|
|
|
|
|
|
|
public Baidu(IHttpProxy httpProxy) : base(httpProxy)
|
|
|
|
|
{
|
|
|
|
|
}
|
2014-07-21 22:27:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|