File tree Expand file tree Collapse file tree 3 files changed +65
-2
lines changed
Plugins/Flow.Launcher.Plugin.WebSearch Expand file tree Collapse file tree 3 files changed +65
-2
lines changed Original file line number Diff line number Diff 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 ]
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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" ,
You can’t perform that action at this time.
0 commit comments