diff --git a/GoogleSearch.cs b/GoogleSearch.cs index d14e237..34f6396 100644 --- a/GoogleSearch.cs +++ b/GoogleSearch.cs @@ -3,6 +3,8 @@ using System.Linq; using System.Net.Http; using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; using System.Web; using HtmlAgilityPack; @@ -23,21 +25,22 @@ public GoogleSearch() _client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent); } - public IEnumerable Search(string query, int limit) + public async Task> Search(string query, int limit, CancellationToken token) { query = query.Replace(' ', '+'); - var response = _client.GetAsync(BuildSearchUri(query)); - var results = ParseResponseWithHAP(response.Result).ToList(); + + var response = await _client.GetAsync(BuildSearchUri(query), token); + var results = await ParseResponseWithHAP(response, token); return results.Take(limit); } - private static IEnumerable ParseResponseWithHAP(HttpResponseMessage response) + private static async Task> ParseResponseWithHAP(HttpResponseMessage response, CancellationToken token) { var htmlDoc = new HtmlDocument(); var googleSearchResults = new List(); - htmlDoc.LoadHtml(response.Content.ReadAsStringAsync().Result); + htmlDoc.LoadHtml(await response.Content.ReadAsStringAsync(token)); var allElementsWithClassG = htmlDoc.QuerySelectorAll("div.g"); @@ -50,12 +53,9 @@ private static IEnumerable ParseResponseWithHAP(HttpResponse if (link == null || title == null) continue; - title = title.Replace("&", "&"); - - Console.WriteLine("Title: " + title); - Console.WriteLine("Link: " + link); + title = HtmlEntity.DeEntitize(title); - googleSearchResults.Add(new GoogleSearchResult() + googleSearchResults.Add(new GoogleSearchResult { Name = title, Url = link @@ -103,4 +103,4 @@ private static string BuildSearchUri(string query) return url; } } -} \ No newline at end of file +} diff --git a/GoogleSearchResult.cs b/GoogleSearchResult.cs index e5f8549..36626db 100644 --- a/GoogleSearchResult.cs +++ b/GoogleSearchResult.cs @@ -1,8 +1,11 @@ +using System.Web; + namespace Wox.Plugin.GoogleSearch { public class GoogleSearchResult { public string Name { get; set; } public string Url { get; set; } + public string DecodedUrl => HttpUtility.UrlDecode(Url); } -} \ No newline at end of file +} diff --git a/Main.cs b/Main.cs index 3144da9..1ce4808 100644 --- a/Main.cs +++ b/Main.cs @@ -1,38 +1,34 @@ using Flow.Launcher.Plugin; -using Flow.Launcher.Plugin.SharedCommands; using System; using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; +using System.Threading; +using System.Threading.Tasks; namespace Wox.Plugin.GoogleSearch { - public class Main: IPlugin + public class Main: IAsyncPlugin { - private string _pluginDirectory; - private GoogleSearch _gs; - private PluginInitContext context; + private PluginInitContext _context; - public List Query(Query query) - { + public async Task> QueryAsync(Query query, CancellationToken token) { + await Task.Delay(300, token); var results = new List(); if (string.IsNullOrEmpty(query.Search)) return results; - var searchResults = _gs.Search(query.Search, 8); + var searchResults = await _gs.Search(query.Search, 8, token); foreach (var s in searchResults) { var r = new Result { Title = s.Name, - SubTitle = s.Url, - IcoPath = $"{_pluginDirectory}\\images\\icon.png", + SubTitle = s.DecodedUrl, + IcoPath = @"images\icon.png", Action = c => { try { - context.API.OpenUrl(s.Url); + _context.API.OpenUrl(s.Url); return true; } catch (Exception) @@ -40,7 +36,6 @@ public List Query(Query query) return false; } }, - ContextData = s }; results.Add(r); } @@ -48,12 +43,10 @@ public List Query(Query query) return results; } - public void Init(PluginInitContext context) + public async Task InitAsync(PluginInitContext context) { - this.context = context; - _pluginDirectory = context.CurrentPluginMetadata.PluginDirectory; + _context = context; _gs = new GoogleSearch(); - } } -} \ No newline at end of file +} diff --git a/plugin.json b/plugin.json index 27160fa..f82593b 100644 --- a/plugin.json +++ b/plugin.json @@ -4,7 +4,7 @@ "Name":"Google Search Plus", "Description":"Plugin for searching Google and navigating directly to the search results.", "Author":"mikemorain", - "Version":"2.0.1", + "Version":"2.0.2", "Language":"csharp", "Website":"https://github.com/jjw24/Wox.Plugin.GoogleSearch", "IcoPath": "images\\icon.png",