mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
#129 Make web search suggestion optional and add baidu suggeestion source.
This commit is contained in:
54
Wox.Plugin.SystemPlugins/SuggestionSources/Baidu.cs
Normal file
54
Wox.Plugin.SystemPlugins/SuggestionSources/Baidu.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Wox.Infrastructure;
|
||||
using YAMP.Numerics;
|
||||
|
||||
namespace Wox.Plugin.SystemPlugins.SuggestionSources
|
||||
{
|
||||
public class Baidu : AbstractSuggestionSource
|
||||
{
|
||||
Regex reg = new Regex("window.baidu.sug\\((.*)\\)");
|
||||
|
||||
public override List<string> GetSuggestions(string query)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response =
|
||||
HttpRequest.CreateGetHttpResponse(
|
||||
"http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), null,
|
||||
null, null);
|
||||
var stream = response.GetResponseStream();
|
||||
|
||||
if (stream != null)
|
||||
{
|
||||
var body = new StreamReader(stream, Encoding.GetEncoding("GB2312")).ReadToEnd();
|
||||
Match m = reg.Match(body);
|
||||
if (m.Success)
|
||||
{
|
||||
var json = JsonConvert.DeserializeObject(m.Groups[1].Value) as JContainer;
|
||||
if (json != null)
|
||||
{
|
||||
var results = json["s"] as JArray;
|
||||
if (results != null)
|
||||
{
|
||||
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Plugin.SystemPlugins.SuggestionSources
|
||||
{
|
||||
public class SuggestionSourceFactory
|
||||
{
|
||||
public static ISuggestionSource GetSuggestionSource(string name)
|
||||
{
|
||||
switch (name.ToLower())
|
||||
{
|
||||
case "google":
|
||||
return new Google();
|
||||
|
||||
case "baidu":
|
||||
return new Baidu();
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user