Skip to content

Commit

Permalink
Merge pull request #13 from Yusyuriv/several-fixes
Browse files Browse the repository at this point in the history
Implement a few small improvements
  • Loading branch information
jjw24 authored Dec 29, 2024
2 parents 8e10d13 + 75f164f commit 55e1659
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
22 changes: 11 additions & 11 deletions GoogleSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,21 +25,22 @@ public GoogleSearch()
_client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
}

public IEnumerable<GoogleSearchResult> Search(string query, int limit)
public async Task<IEnumerable<GoogleSearchResult>> 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<GoogleSearchResult> ParseResponseWithHAP(HttpResponseMessage response)
private static async Task<IEnumerable<GoogleSearchResult>> ParseResponseWithHAP(HttpResponseMessage response, CancellationToken token)
{
var htmlDoc = new HtmlDocument();
var googleSearchResults = new List<GoogleSearchResult>();

htmlDoc.LoadHtml(response.Content.ReadAsStringAsync().Result);
htmlDoc.LoadHtml(await response.Content.ReadAsStringAsync(token));

var allElementsWithClassG = htmlDoc.QuerySelectorAll("div.g");

Expand All @@ -50,12 +53,9 @@ private static IEnumerable<GoogleSearchResult> ParseResponseWithHAP(HttpResponse
if (link == null || title == null)
continue;

title = title.Replace("&amp;", "&");

Console.WriteLine("Title: " + title);
Console.WriteLine("Link: " + link);
title = HtmlEntity.DeEntitize(title);

googleSearchResults.Add(new GoogleSearchResult()
googleSearchResults.Add(new GoogleSearchResult
{
Name = title,
Url = link
Expand Down Expand Up @@ -103,4 +103,4 @@ private static string BuildSearchUri(string query)
return url;
}
}
}
}
5 changes: 4 additions & 1 deletion GoogleSearchResult.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
33 changes: 13 additions & 20 deletions Main.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,52 @@
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<Result> Query(Query query)
{
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token) {
await Task.Delay(300, token);
var results = new List<Result>();
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)
{
return false;
}
},
ContextData = s
};
results.Add(r);
}

return results;
}

public void Init(PluginInitContext context)
public async Task InitAsync(PluginInitContext context)

Check warning on line 46 in Main.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
this.context = context;
_pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
_context = context;
_gs = new GoogleSearch();

}
}
}
}
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 55e1659

Please sign in to comment.