2014-03-27 08:56:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2016-05-10 19:26:47 +00:00
|
|
|
|
using System.Net;
|
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;
|
2020-12-30 05:40:42 +00:00
|
|
|
|
using System.Text.Json;
|
2020-12-30 11:09:52 +00:00
|
|
|
|
using System.IO;
|
2014-03-27 08:56:50 +00:00
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
|
2014-03-27 08:56:50 +00:00
|
|
|
|
{
|
2016-05-05 15:08:44 +00:00
|
|
|
|
public class Google : SuggestionSource
|
2014-03-27 08:56:50 +00:00
|
|
|
|
{
|
2022-01-12 21:21:21 +00:00
|
|
|
|
public override async Task<List<string>> SuggestionsAsync(string query, CancellationToken token)
|
2014-03-27 08:56:50 +00:00
|
|
|
|
{
|
2016-05-10 19:26:47 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
const string api = "https://www.google.com/complete/search?output=chrome&q=";
|
2021-01-17 08:05:28 +00:00
|
|
|
|
|
2022-10-30 19:23:04 +00:00
|
|
|
|
await using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeDataString(query), token: token).ConfigureAwait(false);
|
2021-02-24 08:20:00 +00:00
|
|
|
|
|
2021-02-25 08:04:22 +00:00
|
|
|
|
using var json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token);
|
2021-02-24 08:20:00 +00:00
|
|
|
|
|
|
|
|
|
|
var results = json.RootElement.EnumerateArray().ElementAt(1);
|
|
|
|
|
|
|
|
|
|
|
|
return results.EnumerateArray().Select(o => o.GetString()).ToList();
|
2021-01-07 13:54:09 +00:00
|
|
|
|
|
2016-05-10 19:26:47 +00:00
|
|
|
|
}
|
2022-01-12 21:21:21 +00:00
|
|
|
|
catch (Exception e) when (e is HttpRequestException or {InnerException: 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);
|
|
|
|
|
|
return null;
|
2021-01-07 13:38:21 +00:00
|
|
|
|
}
|
2021-01-07 13:48:55 +00:00
|
|
|
|
catch (JsonException e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Exception("|Google.Suggestions|can't parse suggestions", e);
|
|
|
|
|
|
return new List<string>();
|
|
|
|
|
|
}
|
2014-03-27 08:56:50 +00:00
|
|
|
|
}
|
2016-06-20 23:14:32 +00:00
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return "Google";
|
|
|
|
|
|
}
|
2014-03-27 08:56:50 +00:00
|
|
|
|
}
|
2022-08-08 04:31:38 +00:00
|
|
|
|
}
|