Skip to content

Commit 47bae47

Browse files
committed
Merge remote-tracking branch 'upstream/dev' into InformUserWhenUpdateFail
2 parents 4a2e6db + fd32d36 commit 47bae47

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

Plugins/Flow.Launcher.Plugin.WebSearch/Settings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ public Settings()
196196
[JsonIgnore]
197197
public SuggestionSource[] Suggestions { get; set; } = {
198198
new Google(),
199-
new Baidu()
199+
new Baidu(),
200+
new Bing()
200201
};
201202

202203
[JsonIgnore]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Flow.Launcher.Infrastructure.Http;
2+
using Flow.Launcher.Infrastructure.Logger;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Net.Http;
7+
using System.Text;
8+
using System.Text.RegularExpressions;
9+
using System.Threading.Tasks;
10+
using System.Text.Json;
11+
using System.Linq;
12+
13+
namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
14+
{
15+
class Bing : SuggestionSource
16+
{
17+
public override async Task<List<string>> Suggestions(string query)
18+
{
19+
Stream resultStream;
20+
21+
try
22+
{
23+
const string api = "https://api.bing.com/qsonhs.aspx?q=";
24+
resultStream = await Http.GetStreamAsync(api + Uri.EscapeUriString(query)).ConfigureAwait(false);
25+
}
26+
catch (HttpRequestException e)
27+
{
28+
Log.Exception("|Bing.Suggestions|Can't get suggestion from Bing", e);
29+
return new List<string>();
30+
}
31+
32+
if (resultStream.Length == 0) return new List<string>();
33+
34+
JsonElement json;
35+
try
36+
{
37+
json = (await JsonDocument.ParseAsync(resultStream)).RootElement.GetProperty("AS");
38+
}
39+
catch (JsonException e)
40+
{
41+
Log.Exception("|Bing.Suggestions|can't parse suggestions", e);
42+
return new List<string>();
43+
}
44+
45+
if (json.GetProperty("FullResults").GetInt32() == 0)
46+
return new List<string>();
47+
48+
return json.GetProperty("Results")
49+
.EnumerateArray()
50+
.SelectMany(r => r.GetProperty("Suggests")
51+
.EnumerateArray()
52+
.Select(s => s.GetProperty("Txt").GetString()))
53+
.ToList();
54+
55+
}
56+
57+
public override string ToString()
58+
{
59+
return "Bing";
60+
}
61+
}
62+
}

Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"Name": "Web Searches",
2626
"Description": "Provide the web search ability",
2727
"Author": "qianlifeng",
28-
"Version": "1.1.2",
28+
"Version": "1.2.0",
2929
"Language": "csharp",
3030
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
3131
"ExecuteFileName": "Flow.Launcher.Plugin.WebSearch.dll",

0 commit comments

Comments
 (0)