2014-07-21 14:27:57 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2016-05-10 19:26:47 +00:00
|
|
|
|
using System.Net;
|
2020-12-30 05:40:42 +00:00
|
|
|
|
using System.Text.Json;
|
2014-07-21 14:27:57 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2016-05-09 01:32:47 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Http;
|
|
|
|
|
|
using Flow.Launcher.Infrastructure.Logger;
|
2020-12-29 09:50:56 +00:00
|
|
|
|
using System.Net.Http;
|
2021-01-07 03:09:08 +00:00
|
|
|
|
using System.Threading;
|
2014-07-21 14:27:57 +00:00
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
|
2014-07-21 14:27:57 +00:00
|
|
|
|
{
|
2016-05-05 15:08:44 +00:00
|
|
|
|
public class Baidu : SuggestionSource
|
2014-07-21 14:27:57 +00:00
|
|
|
|
{
|
2016-06-20 23:14:32 +00:00
|
|
|
|
private readonly Regex _reg = new Regex("window.baidu.sug\\((.*)\\)");
|
2016-05-05 15:08:44 +00:00
|
|
|
|
|
2021-01-07 03:09:08 +00:00
|
|
|
|
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
|
2014-07-21 14:27:57 +00:00
|
|
|
|
{
|
2016-05-10 19:26:47 +00:00
|
|
|
|
string result;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
const string api = "http://suggestion.baidu.com/su?json=1&wd=";
|
2021-01-07 03:09:08 +00:00
|
|
|
|
result = await Http.GetAsync(api + Uri.EscapeUriString(query), token).ConfigureAwait(false);
|
2016-05-10 19:26:47 +00:00
|
|
|
|
}
|
2021-02-24 07:55:03 +00:00
|
|
|
|
catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException)
|
2021-01-07 13:38:21 +00:00
|
|
|
|
{
|
2021-02-24 07:55:03 +00:00
|
|
|
|
Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e);
|
2021-01-07 13:38:21 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2021-02-24 07:55:03 +00:00
|
|
|
|
|
2014-12-21 14:03:03 +00:00
|
|
|
|
if (string.IsNullOrEmpty(result)) return new List<string>();
|
2016-06-20 23:14:32 +00:00
|
|
|
|
Match match = _reg.Match(result);
|
2014-12-21 14:03:03 +00:00
|
|
|
|
if (match.Success)
|
2014-07-21 14:27:57 +00:00
|
|
|
|
{
|
2020-12-30 05:40:42 +00:00
|
|
|
|
JsonDocument json;
|
2014-12-21 14:03:03 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-12-30 05:40:42 +00:00
|
|
|
|
json = JsonDocument.Parse(match.Groups[1].Value);
|
2014-12-21 14:03:03 +00:00
|
|
|
|
}
|
2021-02-24 07:55:03 +00:00
|
|
|
|
catch (JsonException e)
|
2016-05-09 01:32:47 +00:00
|
|
|
|
{
|
2017-01-24 00:24:20 +00:00
|
|
|
|
Log.Exception("|Baidu.Suggestions|can't parse suggestions", e);
|
2016-05-09 01:32:47 +00:00
|
|
|
|
return new List<string>();
|
|
|
|
|
|
}
|
2014-07-21 14:27:57 +00:00
|
|
|
|
|
2020-12-30 05:40:42 +00:00
|
|
|
|
var results = json?.RootElement.GetProperty("s");
|
|
|
|
|
|
|
|
|
|
|
|
return results?.EnumerateArray().Select(o => o.GetString()).ToList() ?? new List<string>();
|
2014-07-21 14:27:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-12-21 14:03:03 +00:00
|
|
|
|
return new List<string>();
|
2014-07-21 14:27:57 +00:00
|
|
|
|
}
|
2016-06-20 23:14:32 +00:00
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return "Baidu";
|
|
|
|
|
|
}
|
2014-07-21 14:27:57 +00:00
|
|
|
|
}
|
2016-06-20 23:14:32 +00:00
|
|
|
|
}
|