From 2a03bba6668026dc4d6e751925504b788cf2b2e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Wed, 24 Feb 2021 16:20:00 +0800 Subject: [PATCH] add using for JsonDocument in Google and Bing --- .../SuggestionSources/Bing.cs | 29 ++++++++++--------- .../SuggestionSources/Google.cs | 18 ++++++------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs index e505d78cf..1a1f10d0c 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs @@ -17,7 +17,6 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources { public override async Task> 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(); + + 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(); - } - - if (json.GetProperty("FullResults").GetInt32() == 0) - return new List(); - - return json.GetProperty("Results") - .EnumerateArray() - .SelectMany(r => r.GetProperty("Suggests") - .EnumerateArray() - .Select(s => s.GetProperty("Txt").GetString())) - .ToList(); - + } } public override string ToString() diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs index c075f4d5d..4ca9b2ede 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs @@ -16,15 +16,20 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources { public override async Task> 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(); + + 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(); } - - var results = json?.RootElement.EnumerateArray().ElementAt(1); - - return results?.EnumerateArray().Select(o => o.GetString()).ToList() ?? new List(); - } public override string ToString()