mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
Refactoring. Move system plugins to seperate DLLs.
This commit is contained in:
43
Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs
Normal file
43
Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Wox.Infrastructure.Http;
|
||||
|
||||
namespace Wox.Plugin.WebSearch.SuggestionSources
|
||||
{
|
||||
public class Baidu : AbstractSuggestionSource
|
||||
{
|
||||
Regex reg = new Regex("window.baidu.sug\\((.*)\\)");
|
||||
|
||||
public override List<string> GetSuggestions(string query)
|
||||
{
|
||||
var result = HttpRequest.Get("http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), "GB2312");
|
||||
if (string.IsNullOrEmpty(result)) return new List<string>();
|
||||
|
||||
Match match = reg.Match(result);
|
||||
if (match.Success)
|
||||
{
|
||||
JContainer json = null;
|
||||
try
|
||||
{
|
||||
json =JsonConvert.DeserializeObject(match.Groups[1].Value) as JContainer;
|
||||
}
|
||||
catch{}
|
||||
|
||||
if (json != null)
|
||||
{
|
||||
var results = json["s"] as JArray;
|
||||
if (results != null)
|
||||
{
|
||||
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new List<string>();
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs
Normal file
34
Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Wox.Infrastructure.Http;
|
||||
|
||||
namespace Wox.Plugin.WebSearch.SuggestionSources
|
||||
{
|
||||
public class Google : AbstractSuggestionSource
|
||||
{
|
||||
public override List<string> GetSuggestions(string query)
|
||||
{
|
||||
var result = HttpRequest.Get("https://www.google.com/complete/search?output=chrome&q=" + Uri.EscapeUriString(query));
|
||||
if (string.IsNullOrEmpty(result)) return new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
JContainer json = JsonConvert.DeserializeObject(result) as JContainer;
|
||||
if (json != null)
|
||||
{
|
||||
var results = json[1] as JContainer;
|
||||
if (results != null)
|
||||
{
|
||||
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return new List<string>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Wox.Plugin.WebSearch.SuggestionSources
|
||||
{
|
||||
public interface ISuggestionSource
|
||||
{
|
||||
List<string> GetSuggestions(string query);
|
||||
}
|
||||
|
||||
public abstract class AbstractSuggestionSource : ISuggestionSource
|
||||
{
|
||||
public abstract List<string> GetSuggestions(string query);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace Wox.Plugin.WebSearch.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