Add Google suggestions for web searches

Fix issues with push results
This commit is contained in:
Yeechan Lu
2014-03-27 16:56:50 +08:00
parent c8fcfcd28c
commit 9a2ffc1d7c
10 changed files with 249 additions and 18 deletions

View File

@@ -57,7 +57,7 @@ namespace Wox.Plugin.System.CMD
}
catch (Exception) { }
context.PushResults(new List<Result>() { result });
context.PushResults(query, new List<Result>() { result });
IEnumerable<Result> history = CMDStorage.Instance.CMDHistory.Where(o => o.Key.Contains(cmd))
.OrderByDescending(o => o.Value)
@@ -92,7 +92,7 @@ namespace Wox.Plugin.System.CMD
return ret;
}).Where(o => o != null).Take(4);
context.PushResults(history.ToList());
context.PushResults(query, history.ToList());
try
{

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Wox.Infrastructure;
using YAMP.Numerics;
namespace Wox.Plugin.System.SuggestionSources
{
public class Google : AbstractSuggestionSource
{
public override List<string> GetSuggestions(string query)
{
var response =
HttpRequest.CreateGetHttpResponse("https://www.google.com/complete/search?output=chrome&q=" + Uri.EscapeUriString(query), null,
null, null);
var stream = response.GetResponseStream();
if (stream != null)
{
var body = new StreamReader(stream).ReadToEnd();
var json = JsonConvert.DeserializeObject(body) as JContainer;
if (json != null)
{
var results = json[1] as JContainer;
if (results != null)
{
var j = results.OfType<JValue>().Select(o => o.Value);
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
}
}
}
return null;
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace Wox.Plugin.System.SuggestionSources
{
public interface ISuggestionSource
{
List<string> GetSuggestions(string query);
}
public abstract class AbstractSuggestionSource : ISuggestionSource
{
public abstract List<string> GetSuggestions(string query);
}
}

View File

@@ -7,11 +7,14 @@ using Newtonsoft.Json;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.Storage.UserSettings;
using Wox.Plugin.System.SuggestionSources;
namespace Wox.Plugin.System
{
public class WebSearchPlugin : BaseSystemPlugin
{
private PluginInitContext context;
protected override List<Result> QueryInternal(Query query)
{
List<Result> results = new List<Result>();
@@ -23,22 +26,49 @@ namespace Wox.Plugin.System
if (webSearch != null)
{
string keyword = query.ActionParameters.Count > 0 ? query.GetAllRemainingParameter() : "";
string title = string.Format("Search {0} for \"{1}\"", webSearch.Title, keyword);
string title = keyword;
string subtitle = "Search " + webSearch.Title;
if (string.IsNullOrEmpty(keyword))
{
title = "Search " + webSearch.Title;
title = subtitle;
subtitle = null;
}
results.Add(new Result()
context.PushResults(query, new List<Result>()
{
Title = title,
Score = 6,
IcoPath = webSearch.IconPath,
Action = (c) =>
new Result()
{
Process.Start(webSearch.Url.Replace("{q}", keyword));
return true;
Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = webSearch.IconPath,
Action = (c) =>
{
Process.Start(webSearch.Url.Replace("{q}", keyword));
return true;
}
}
});
if (!string.IsNullOrEmpty(keyword))
{
ISuggestionSource sugg = new Google();
var result = sugg.GetSuggestions(keyword);
if (result != null)
{
context.PushResults(query, result.Select(o => new Result()
{
Title = o,
SubTitle = subtitle,
Score = 5,
IcoPath = webSearch.IconPath,
Action = (c) =>
{
Process.Start(webSearch.Url.Replace("{q}", o));
return true;
}
}).ToList());
}
}
}
return results;
@@ -46,6 +76,7 @@ namespace Wox.Plugin.System
protected override void InitInternal(PluginInitContext context)
{
this.context = context;
}
}
}

View File

@@ -70,6 +70,8 @@
<Compile Include="Setting.cs" />
<Compile Include="Sys.cs" />
<Compile Include="ThirdpartyPluginIndicator.cs" />
<Compile Include="SuggestionSources\Google.cs" />
<Compile Include="SuggestionSources\ISuggestionSource.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Wox.Infrastructure\Wox.Infrastructure.csproj">