-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf56e1c
commit 9ab6d55
Showing
10 changed files
with
223 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+1.04 KB
Community.PowerToys.Run.Plugin.WebSearchShortcut/Images/Suggestion.dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.08 KB
Community.PowerToys.Run.Plugin.WebSearchShortcut/Images/Suggestion.light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
Community.PowerToys.Run.Plugin.WebSearchShortcut/Suggestion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace Community.PowerToys.Run.Plugin.WebSearchShortcut.Suggestion | ||
{ | ||
public interface IWebSearchShortcutSuggestionsProvider | ||
{ | ||
Task<List<string>> QuerySuggestionsAsync(string query); | ||
} | ||
|
||
public class SuggestionsItem(string title, string? description) | ||
{ | ||
public string Title { get; } = title; | ||
public string? Description { get; } = description; | ||
} | ||
|
||
public class Suggestions | ||
{ | ||
public Task<List<string>> QuerySuggestionsAsync(string name, string query) | ||
{ | ||
var provider = SuggestionProviders[name]; | ||
if (provider != null) | ||
{ | ||
return provider.QuerySuggestionsAsync(query); | ||
} | ||
return Task.FromResult(new List<string>()); | ||
} | ||
public Dictionary<string, IWebSearchShortcutSuggestionsProvider> SuggestionProviders { get; set; } = new(){ | ||
{ Google.Name, new Google() }, | ||
{ Bing.Name, new Bing() }, | ||
}; | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
Community.PowerToys.Run.Plugin.WebSearchShortcut/Suggestions.cs
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
Community.PowerToys.Run.Plugin.WebSearchShortcut/SuggestionsProvider/Bing.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
using Wox.Plugin.Logger; | ||
|
||
namespace Community.PowerToys.Run.Plugin.WebSearchShortcut.Suggestion | ||
{ | ||
public class Bing : IWebSearchShortcutSuggestionsProvider | ||
{ | ||
static public string Name => "Bing"; | ||
|
||
private HttpClient Http { get; } = new HttpClient(); | ||
|
||
public async Task<List<string>> QuerySuggestionsAsync(string query) | ||
{ | ||
try | ||
{ | ||
const string api = "https://api.bing.com/qsonhs.aspx?q="; | ||
|
||
await using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeDataString(query)).ConfigureAwait(false); | ||
|
||
using var json = await JsonDocument.ParseAsync(resultStream); | ||
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())) | ||
.Where(s => s != null) | ||
.Select(s => s!) | ||
.ToList(); | ||
|
||
} | ||
catch (Exception e) when (e is HttpRequestException or { InnerException: TimeoutException }) | ||
{ | ||
Log.Error($"{e.Message}", typeof(Bing)); | ||
return []; | ||
} | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return "Bing"; | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Community.PowerToys.Run.Plugin.WebSearchShortcut/SuggestionsProvider/Google.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
using Wox.Plugin.Logger; | ||
|
||
namespace Community.PowerToys.Run.Plugin.WebSearchShortcut.Suggestion | ||
{ | ||
public class Google : IWebSearchShortcutSuggestionsProvider | ||
{ | ||
static public string Name => "Google"; | ||
|
||
private HttpClient Http { get; } = new HttpClient(); | ||
|
||
public async Task<List<string>> QuerySuggestionsAsync(string query) | ||
{ | ||
try | ||
{ | ||
const string api = "https://www.google.com/complete/search?output=chrome&q="; | ||
|
||
await using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeDataString(query)).ConfigureAwait(false); | ||
|
||
using var json = await JsonDocument.ParseAsync(resultStream); | ||
|
||
var results = json.RootElement.EnumerateArray().ElementAt(1); | ||
|
||
return results | ||
.EnumerateArray() | ||
.Select(o => o.GetString()) | ||
.Where(s => s != null) | ||
.Select(s => s!) | ||
.ToList(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error($"{e.Message}", typeof(Google)); | ||
return []; | ||
} | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return "Google"; | ||
} | ||
} | ||
} |