#129 Make web search suggestion optional and add baidu suggeestion source.

This commit is contained in:
qianlifeng
2014-07-21 22:27:57 +08:00
parent 4e87211d39
commit 83e199a0de
10 changed files with 203 additions and 59 deletions

View 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;
}
}
}

View File

@@ -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;
}
}
}
}