mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
2. Manually replace "#" with "%23" to solve the similar issue in Explorer plugin 3. Add GetAsync method with Uri as argument 4. Remove unused encoding argument 5. Change exception type for WebSearch Plguin 6. Update Comment
67 lines
No EOL
2 KiB
C#
67 lines
No EOL
2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using Flow.Launcher.Infrastructure.Http;
|
|
using Flow.Launcher.Infrastructure.Logger;
|
|
using System.Net.Http;
|
|
|
|
namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
|
|
{
|
|
public class Baidu : SuggestionSource
|
|
{
|
|
private readonly Regex _reg = new Regex("window.baidu.sug\\((.*)\\)");
|
|
|
|
public override async Task<List<string>> Suggestions(string query)
|
|
{
|
|
string result;
|
|
|
|
try
|
|
{
|
|
const string api = "http://suggestion.baidu.com/su?json=1&wd=";
|
|
result = await Http.GetAsync(api + Uri.EscapeUriString(query)).ConfigureAwait(false);
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e);
|
|
return new List<string>();
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(result)) return new List<string>();
|
|
Match match = _reg.Match(result);
|
|
if (match.Success)
|
|
{
|
|
JContainer json;
|
|
try
|
|
{
|
|
json = JsonConvert.DeserializeObject(match.Groups[1].Value) as JContainer;
|
|
}
|
|
catch (JsonSerializationException e)
|
|
{
|
|
Log.Exception("|Baidu.Suggestions|can't parse suggestions", e);
|
|
return new List<string>();
|
|
}
|
|
|
|
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>();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return "Baidu";
|
|
}
|
|
}
|
|
} |