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-06-21 07:14:32 +08:00
|
|
|
|
private readonly Regex _reg = new Regex("window.baidu.sug\\((.*)\\)");
|
2016-05-05 23:08:44 +08:00
|
|
|
|
|
2016-06-21 07:14:32 +08:00
|
|
|
|
public override async Task<List<string>> Suggestions(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=";
|
2016-06-19 23:18:43 +08:00
|
|
|
|
result = await Http.Get(api + Uri.EscapeUriString(query), "GB2312");
|
2016-05-11 03:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn("Can't get suggestion from baidu");
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(e);
|
2016-06-21 07:14:32 +08:00
|
|
|
|
return new List<string>();
|
|
|
|
|
;
|
2016-05-11 03:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-21 22:03:03 +08:00
|
|
|
|
if (string.IsNullOrEmpty(result)) return new List<string>();
|
2016-06-21 07:14:32 +08:00
|
|
|
|
Match match = _reg.Match(result);
|
2014-12-21 22:03:03 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(e);
|
2016-05-09 09:32:47 +08:00
|
|
|
|
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
|
|
|
|
}
|
2016-06-21 07:14:32 +08:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return "Baidu";
|
|
|
|
|
}
|
2014-07-21 22:27:57 +08:00
|
|
|
|
}
|
2016-06-21 07:14:32 +08:00
|
|
|
|
}
|