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

40 lines
1.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
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 Google : SuggestionSource
{
public override string Domain { get; set; } = "www.google.com";
public override List<string> GetSuggestions(string query)
{
var result = HttpRequest.Get("https://www.google.com/complete/search?output=chrome&q=" + Uri.EscapeUriString(query), Proxy);
2014-12-21 22:03:03 +08:00
if (string.IsNullOrEmpty(result)) return new List<string>();
try
{
2014-12-21 22:03:03 +08:00
JContainer json = JsonConvert.DeserializeObject(result) as JContainer;
if (json != null)
{
2014-12-21 22:03:03 +08:00
var results = json[1] as JContainer;
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
catch { }
return new List<string>();
}
public Google(IHttpProxy httpProxy) : base(httpProxy)
{
}
}
}