add bing search suggestion

This commit is contained in:
弘韬 张 2020-12-30 20:24:01 +08:00
parent 58d281db7a
commit a139a040ec
2 changed files with 64 additions and 1 deletions

View file

@ -196,7 +196,8 @@ namespace Flow.Launcher.Plugin.WebSearch
[JsonIgnore]
public SuggestionSource[] Suggestions { get; set; } = {
new Google(),
new Baidu()
new Baidu(),
new Bing()
};
[JsonIgnore]

View file

@ -0,0 +1,62 @@
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Logger;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Text.Json;
using System.Linq;
namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
class Bing : SuggestionSource
{
public override async Task<List<string>> Suggestions(string query)
{
Stream resultStream;
try
{
const string api = "https://api.bing.com/qsonhs.aspx?q=";
resultStream = await Http.GetStreamAsync(api + Uri.EscapeUriString(query)).ConfigureAwait(false);
}
catch (HttpRequestException e)
{
Log.Exception("|Bing.Suggestions|Can't get suggestion from Bing", e);
return new List<string>();
}
if (resultStream.Length == 0) return new List<string>();
JsonElement json;
try
{
json = (await JsonDocument.ParseAsync(resultStream)).RootElement.GetProperty("AS");
}
catch (JsonException e)
{
Log.Exception("|Bing.Suggestions|can't parse suggestions", e);
return new List<string>();
}
if (json.GetProperty("FullResults").GetInt32() == 0)
return new List<string>();
return json.GetProperty("Results")
.EnumerateArray()
.SelectMany(r => r.GetProperty("Suggests")
.EnumerateArray()
.Select(s => s.GetProperty("Txt").GetString()))
.ToList();
}
public override string ToString()
{
return "Bing";
}
}
}