Flow.Launcher/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs
Jeremy Wu 84a806bf75
Release 1.9.3 (#1052)
## Bug fixes
- Addresses the issue where WindowsSettings plugin results appear higher than other results #1020 

- Fixed an issue where full screen mode does not disable hotkey when enabled #1037 

- Fixed some potential issues when loading plugins that use shared assembly #1036 

- Sorted out a race condition issue causing image loading on some results to fail #1040 

- Revised ttf/otf support #935 

- Resolved the issue where WebSearch plugin would crash if not connected to internet #977 

- Fixed incorrect text for "New Tab" and "New Window" buttons under Settings' default browser section #951 

- Fixed typos in plugin title and WindowsSettings name inside the context menu #1056
2022-03-03 13:59:43 +11:00

52 lines
No EOL
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Logger;
using System.Net.Http;
using System.Threading;
using System.Text.Json;
using System.IO;
namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
public class Google : SuggestionSource
{
public override async Task<List<string>> SuggestionsAsync(string query, CancellationToken token)
{
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);
using var json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token);
if (json == null)
return new List<string>();
var results = json.RootElement.EnumerateArray().ElementAt(1);
return results.EnumerateArray().Select(o => o.GetString()).ToList();
}
catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException})
{
Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e);
return null;
}
catch (JsonException e)
{
Log.Exception("|Google.Suggestions|can't parse suggestions", e);
return new List<string>();
}
}
public override string ToString()
{
return "Google";
}
}
}