add using for JsonDocument in Google and Bing

This commit is contained in:
弘韬 张 2021-02-24 16:20:00 +08:00
parent f3a1bcc01f
commit 2a03bba666
2 changed files with 24 additions and 23 deletions

View file

@ -17,7 +17,6 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
{
JsonElement json;
try
{
@ -25,7 +24,20 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeUriString(query), token).ConfigureAwait(false);
json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token)).RootElement.GetProperty("AS");
using var json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token));
var root = json.RootElement.GetProperty("AS");
if (root.GetProperty("FullResults").GetInt32() == 0)
return new List<string>();
return root.GetProperty("Results")
.EnumerateArray()
.SelectMany(r => r.GetProperty("Suggests")
.EnumerateArray()
.Select(s => s.GetProperty("Txt").GetString()))
.ToList();
}
catch (TaskCanceledException)
@ -41,18 +53,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
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()

View file

@ -16,15 +16,20 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
{
JsonDocument json;
try
{
const string api = "https://www.google.com/complete/search?output=chrome&q=";
using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeUriString(query)).ConfigureAwait(false);
json = await JsonDocument.ParseAsync(resultStream);
using var json = await JsonDocument.ParseAsync(resultStream);
if (json == null)
return new List<string>();
var results = json.RootElement.EnumerateArray().ElementAt(1);
return results.EnumerateArray().Select(o => o.GetString()).ToList();
}
catch (TaskCanceledException)
@ -41,11 +46,6 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
Log.Exception("|Google.Suggestions|can't parse suggestions", e);
return new List<string>();
}
var results = json?.RootElement.EnumerateArray().ElementAt(1);
return results?.EnumerateArray().Select(o => o.GetString()).ToList() ?? new List<string>();
}
public override string ToString()