2014-03-27 16:56:50 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2016-05-11 03:26:47 +08:00
|
|
|
|
using System.Net;
|
2016-05-09 09:32:47 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2014-03-27 16:56:50 +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-03-27 16:56:50 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
namespace Wox.Plugin.WebSearch.SuggestionSources
|
2014-03-27 16:56:50 +08:00
|
|
|
|
{
|
2016-05-05 23:08:44 +08:00
|
|
|
|
public class Google : SuggestionSource
|
2014-03-27 16:56:50 +08:00
|
|
|
|
{
|
2016-05-05 23:08:44 +08:00
|
|
|
|
public override string Domain { get; set; } = "www.google.com";
|
2016-05-09 09:32:47 +08:00
|
|
|
|
public override async Task<List<string>> GetSuggestions(string query)
|
2014-03-27 16:56:50 +08:00
|
|
|
|
{
|
2016-05-11 03:26:47 +08:00
|
|
|
|
string result;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
const string api = "https://www.google.com/complete/search?output=chrome&q=";
|
|
|
|
|
result = await Http.Get(api + Uri.EscapeUriString(query), Proxy);
|
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn("Can't get suggestion from google");
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(e);
|
2016-05-11 03:26:47 +08:00
|
|
|
|
return new List<string>(); ;
|
|
|
|
|
}
|
2014-12-21 22:03:03 +08:00
|
|
|
|
if (string.IsNullOrEmpty(result)) return new List<string>();
|
2016-05-09 09:32:47 +08:00
|
|
|
|
JContainer json;
|
2014-03-27 21:14:41 +08:00
|
|
|
|
try
|
2014-03-27 16:56:50 +08:00
|
|
|
|
{
|
2016-05-09 09:32:47 +08:00
|
|
|
|
json = JsonConvert.DeserializeObject(result) as JContainer;
|
|
|
|
|
}
|
|
|
|
|
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>();
|
|
|
|
|
}
|
|
|
|
|
if (json != null)
|
|
|
|
|
{
|
|
|
|
|
var results = json[1] as JContainer;
|
|
|
|
|
if (results != null)
|
2014-03-27 16:56:50 +08:00
|
|
|
|
{
|
2016-05-09 09:32:47 +08:00
|
|
|
|
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
|
2014-03-27 16:56:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-21 22:03:03 +08:00
|
|
|
|
return new List<string>();
|
2014-03-27 16:56:50 +08:00
|
|
|
|
}
|
2015-02-20 21:14:15 +08:00
|
|
|
|
|
|
|
|
|
public Google(IHttpProxy httpProxy) : base(httpProxy)
|
|
|
|
|
{
|
|
|
|
|
}
|
2014-03-27 16:56:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|