45 lines
859 B
Go
45 lines
859 B
Go
package search
|
|
|
|
import (
|
|
"context"
|
|
googlesearch "github.com/rocketlaunchr/google-search"
|
|
"sort"
|
|
)
|
|
|
|
type Google struct {
|
|
}
|
|
|
|
var _ Search = Google{}
|
|
|
|
func (Google) Search(ctx context.Context, search string) ([]Result, error) {
|
|
res, err := googlesearch.Search(ctx, search, googlesearch.SearchOptions{
|
|
CountryCode: "",
|
|
LanguageCode: "",
|
|
Limit: 0,
|
|
Start: 0,
|
|
UserAgent: "",
|
|
OverLimit: false,
|
|
ProxyAddr: "",
|
|
FollowNextPage: false,
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var results []Result
|
|
|
|
// just in case, sort the res by rank, as the api does not mention it is sorted
|
|
sort.Slice(res, func(i, j int) bool {
|
|
return res[i].Rank < res[j].Rank
|
|
})
|
|
|
|
for _, r := range res {
|
|
results = append(results, Result{
|
|
Title: r.Title,
|
|
URL: r.URL,
|
|
Description: r.Description,
|
|
})
|
|
}
|
|
}
|