mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 20:57:22 +02:00
Add Google suggestions for web searches
Fix issues with push results
This commit is contained in:
137
Wox.Infrastructure/HttpRequest.cs
Normal file
137
Wox.Infrastructure/HttpRequest.cs
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Security;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
//From:http://blog.csdn.net/zhoufoxcn/article/details/6404236
|
||||||
|
namespace Wox.Infrastructure
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 有关HTTP请求的辅助类
|
||||||
|
/// </summary>
|
||||||
|
public class HttpRequest
|
||||||
|
{
|
||||||
|
private static readonly string DefaultUserAgent = "Wox/" + Assembly.GetEntryAssembly().GetName().Version.ToString() + " (+https://github.com/qianlifeng/Wox)";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建GET方式的HTTP请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">请求的URL</param>
|
||||||
|
/// <param name="timeout">请求的超时时间</param>
|
||||||
|
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
|
||||||
|
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(url))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("url");
|
||||||
|
}
|
||||||
|
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
||||||
|
request.Method = "GET";
|
||||||
|
request.UserAgent = DefaultUserAgent;
|
||||||
|
if (!string.IsNullOrEmpty(userAgent))
|
||||||
|
{
|
||||||
|
request.UserAgent = userAgent;
|
||||||
|
}
|
||||||
|
if (timeout.HasValue)
|
||||||
|
{
|
||||||
|
request.Timeout = timeout.Value;
|
||||||
|
}
|
||||||
|
if (cookies != null)
|
||||||
|
{
|
||||||
|
request.CookieContainer = new CookieContainer();
|
||||||
|
request.CookieContainer.Add(cookies);
|
||||||
|
}
|
||||||
|
return request.GetResponse() as HttpWebResponse;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 创建POST方式的HTTP请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">请求的URL</param>
|
||||||
|
/// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
|
||||||
|
/// <param name="timeout">请求的超时时间</param>
|
||||||
|
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
|
||||||
|
/// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
|
||||||
|
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(url))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("url");
|
||||||
|
}
|
||||||
|
if (requestEncoding == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("requestEncoding");
|
||||||
|
}
|
||||||
|
HttpWebRequest request = null;
|
||||||
|
//如果是发送HTTPS请求
|
||||||
|
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
||||||
|
request = WebRequest.Create(url) as HttpWebRequest;
|
||||||
|
request.ProtocolVersion = HttpVersion.Version10;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
request = WebRequest.Create(url) as HttpWebRequest;
|
||||||
|
}
|
||||||
|
request.Method = "POST";
|
||||||
|
request.ContentType = "application/x-www-form-urlencoded";
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(userAgent))
|
||||||
|
{
|
||||||
|
request.UserAgent = userAgent;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
request.UserAgent = DefaultUserAgent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timeout.HasValue)
|
||||||
|
{
|
||||||
|
request.Timeout = timeout.Value;
|
||||||
|
}
|
||||||
|
if (cookies != null)
|
||||||
|
{
|
||||||
|
request.CookieContainer = new CookieContainer();
|
||||||
|
request.CookieContainer.Add(cookies);
|
||||||
|
}
|
||||||
|
//如果需要POST数据
|
||||||
|
if (!(parameters == null || parameters.Count == 0))
|
||||||
|
{
|
||||||
|
StringBuilder buffer = new StringBuilder();
|
||||||
|
int i = 0;
|
||||||
|
foreach (string key in parameters.Keys)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buffer.AppendFormat("{0}={1}", key, parameters[key]);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
byte[] data = requestEncoding.GetBytes(buffer.ToString());
|
||||||
|
using (Stream stream = request.GetRequestStream())
|
||||||
|
{
|
||||||
|
stream.Write(data, 0, data.Length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return request.GetResponse() as HttpWebResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
||||||
|
{
|
||||||
|
return true; //总是接受
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -49,6 +49,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="ChineseToPinYin.cs" />
|
<Compile Include="ChineseToPinYin.cs" />
|
||||||
|
<Compile Include="HttpRequest.cs" />
|
||||||
<Compile Include="Storage\BaseStorage.cs" />
|
<Compile Include="Storage\BaseStorage.cs" />
|
||||||
<Compile Include="FuzzyMatcher.cs" />
|
<Compile Include="FuzzyMatcher.cs" />
|
||||||
<Compile Include="GlobalHotkey.cs" />
|
<Compile Include="GlobalHotkey.cs" />
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ namespace Wox.Plugin.System.CMD
|
|||||||
}
|
}
|
||||||
catch (Exception) { }
|
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))
|
IEnumerable<Result> history = CMDStorage.Instance.CMDHistory.Where(o => o.Key.Contains(cmd))
|
||||||
.OrderByDescending(o => o.Value)
|
.OrderByDescending(o => o.Value)
|
||||||
@@ -92,7 +92,7 @@ namespace Wox.Plugin.System.CMD
|
|||||||
return ret;
|
return ret;
|
||||||
}).Where(o => o != null).Take(4);
|
}).Where(o => o != null).Take(4);
|
||||||
|
|
||||||
context.PushResults(history.ToList());
|
context.PushResults(query, history.ToList());
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
42
Wox.Plugin.System/SuggestionSources/Google.cs
Normal file
42
Wox.Plugin.System/SuggestionSources/Google.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
Wox.Plugin.System/SuggestionSources/ISuggestionSource.cs
Normal file
18
Wox.Plugin.System/SuggestionSources/ISuggestionSource.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,11 +7,14 @@ using Newtonsoft.Json;
|
|||||||
using Wox.Infrastructure;
|
using Wox.Infrastructure;
|
||||||
using Wox.Infrastructure.Storage;
|
using Wox.Infrastructure.Storage;
|
||||||
using Wox.Infrastructure.Storage.UserSettings;
|
using Wox.Infrastructure.Storage.UserSettings;
|
||||||
|
using Wox.Plugin.System.SuggestionSources;
|
||||||
|
|
||||||
namespace Wox.Plugin.System
|
namespace Wox.Plugin.System
|
||||||
{
|
{
|
||||||
public class WebSearchPlugin : BaseSystemPlugin
|
public class WebSearchPlugin : BaseSystemPlugin
|
||||||
{
|
{
|
||||||
|
private PluginInitContext context;
|
||||||
|
|
||||||
protected override List<Result> QueryInternal(Query query)
|
protected override List<Result> QueryInternal(Query query)
|
||||||
{
|
{
|
||||||
List<Result> results = new List<Result>();
|
List<Result> results = new List<Result>();
|
||||||
@@ -23,22 +26,49 @@ namespace Wox.Plugin.System
|
|||||||
if (webSearch != null)
|
if (webSearch != null)
|
||||||
{
|
{
|
||||||
string keyword = query.ActionParameters.Count > 0 ? query.GetAllRemainingParameter() : "";
|
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))
|
if (string.IsNullOrEmpty(keyword))
|
||||||
{
|
{
|
||||||
title = "Search " + webSearch.Title;
|
title = subtitle;
|
||||||
|
subtitle = null;
|
||||||
}
|
}
|
||||||
results.Add(new Result()
|
context.PushResults(query, new List<Result>()
|
||||||
{
|
{
|
||||||
Title = title,
|
new Result()
|
||||||
Score = 6,
|
|
||||||
IcoPath = webSearch.IconPath,
|
|
||||||
Action = (c) =>
|
|
||||||
{
|
{
|
||||||
Process.Start(webSearch.Url.Replace("{q}", keyword));
|
Title = title,
|
||||||
return true;
|
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;
|
return results;
|
||||||
@@ -46,6 +76,7 @@ namespace Wox.Plugin.System
|
|||||||
|
|
||||||
protected override void InitInternal(PluginInitContext context)
|
protected override void InitInternal(PluginInitContext context)
|
||||||
{
|
{
|
||||||
|
this.context = context;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,8 @@
|
|||||||
<Compile Include="Setting.cs" />
|
<Compile Include="Setting.cs" />
|
||||||
<Compile Include="Sys.cs" />
|
<Compile Include="Sys.cs" />
|
||||||
<Compile Include="ThirdpartyPluginIndicator.cs" />
|
<Compile Include="ThirdpartyPluginIndicator.cs" />
|
||||||
|
<Compile Include="SuggestionSources\Google.cs" />
|
||||||
|
<Compile Include="SuggestionSources\ISuggestionSource.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Wox.Infrastructure\Wox.Infrastructure.csproj">
|
<ProjectReference Include="..\Wox.Infrastructure\Wox.Infrastructure.csproj">
|
||||||
|
|||||||
@@ -32,6 +32,6 @@ namespace Wox.Plugin
|
|||||||
|
|
||||||
public Func<string, bool> ShellRun { get; set; }
|
public Func<string, bool> ShellRun { get; set; }
|
||||||
|
|
||||||
public Action<List<Result>> PushResults { get; set; }
|
public Action<Query, List<Result>> PushResults { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,17 +27,17 @@ namespace Wox.Commands
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
thirdPlugin.InitContext.PushResults = r =>
|
thirdPlugin.InitContext.PushResults = (qu, r) =>
|
||||||
{
|
{
|
||||||
r.ForEach(o =>
|
r.ForEach(o =>
|
||||||
{
|
{
|
||||||
o.PluginDirectory = thirdPlugin.Metadata.PluginDirecotry;
|
o.PluginDirectory = thirdPlugin.Metadata.PluginDirecotry;
|
||||||
o.OriginQuery = q;
|
o.OriginQuery = qu;
|
||||||
});
|
});
|
||||||
UpdateResultView(r);
|
UpdateResultView(r);
|
||||||
};
|
};
|
||||||
List<Result> results = thirdPlugin.Plugin.Query(q) ?? new List<Result>();
|
List<Result> results = thirdPlugin.Plugin.Query(q) ?? new List<Result>();
|
||||||
thirdPlugin.InitContext.PushResults(results);
|
thirdPlugin.InitContext.PushResults(q, results);
|
||||||
}
|
}
|
||||||
catch (Exception queryException)
|
catch (Exception queryException)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,20 +17,20 @@ namespace Wox.Commands
|
|||||||
PluginPair pair1 = pair;
|
PluginPair pair1 = pair;
|
||||||
ThreadPool.QueueUserWorkItem(state =>
|
ThreadPool.QueueUserWorkItem(state =>
|
||||||
{
|
{
|
||||||
pair1.InitContext.PushResults = r =>
|
pair1.InitContext.PushResults = (q, r) =>
|
||||||
{
|
{
|
||||||
if (r == null || r.Count == 0) return;
|
if (r == null || r.Count == 0) return;
|
||||||
foreach (Result result in r)
|
foreach (Result result in r)
|
||||||
{
|
{
|
||||||
result.PluginDirectory = pair1.Metadata.PluginDirecotry;
|
result.PluginDirectory = pair1.Metadata.PluginDirecotry;
|
||||||
result.OriginQuery = query;
|
result.OriginQuery = q;
|
||||||
result.AutoAjustScore = true;
|
result.AutoAjustScore = true;
|
||||||
}
|
}
|
||||||
UpdateResultView(r);
|
UpdateResultView(r);
|
||||||
};
|
};
|
||||||
|
|
||||||
List<Result> results = pair1.Plugin.Query(query);
|
List<Result> results = pair1.Plugin.Query(query);
|
||||||
pair1.InitContext.PushResults(results);
|
pair1.InitContext.PushResults(query, results);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user