Compare commits

...

10 Commits

Author SHA1 Message Date
pj
30c2a23ad5
Merge pull request #23 from chrisjoyce911/useragent
Update User Agent
2023-06-18 14:32:15 +10:00
Chris Joyce
dfdb3593e1 Pull request issues resolved
* Remove contect from tests
* Resorted package dependacy for TestSearch
* Removed extra notes from README
2023-05-27 17:31:50 +10:00
Chris Joyce
bcbf8ae1b3 Fix optional UserAgent
Added an example
2023-05-26 13:06:12 +10:00
Chris Joyce
8179baaa61 Added Using a Fork to Readme 2023-05-25 20:34:28 +10:00
Chris Joyce
31583a67d0 restored module name 2023-05-25 20:07:06 +10:00
Chris Joyce
a22732eeb6 Moved example to a test 2023-05-25 20:07:06 +10:00
Chris Joyce
f72b448bec Updated go, useragent and added example 2023-05-25 20:07:06 +10:00
miguelp
42aab3d7fc cahnged package name to be able to import from fork 2023-05-25 20:06:22 +10:00
miguelp
20e5149740 changed package name 2023-05-25 20:05:25 +10:00
Kay Nelson
6b8d3ebbca Update the default User-Agent 2023-05-25 20:05:25 +10:00
5 changed files with 47 additions and 16 deletions

View File

@ -1,7 +1,6 @@
package googlesearch package googlesearch
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
) )
@ -12,14 +11,46 @@ func ExampleSearch() {
CountryCode: "au", CountryCode: "au",
} }
serp, err := Search(context.Background(), "First Aid Course Australia Wide First Aid", opt) //lint:ignore SA1012 ignore this bare essentials by passing nil for context and removing context package (despite not being idiomatic go).
serp, err := Search(nil, "First Aid Course Australia Wide First Aid", opt)
if err != nil { if err != nil {
fmt.Print(err.Error()) fmt.Print(err.Error())
} }
for _, resault := range serp { for _, result := range serp {
if strings.Contains(resault.URL, "australiawidefirstaid.com.au") { if strings.Contains(result.URL, "australiawidefirstaid.com.au") {
fmt.Println("Australia Wide First Aid (https://www.australiawidefirstaid.com.au/) found in the serp")
break
}
}
// Output: Australia Wide First Aid (https://www.australiawidefirstaid.com.au/) found in the serp
}
/*
Example of how to set the useragent
*/
func ExampleUserAgent() {
// whatismybrowser.com maintains a database of UserAgents
// https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome
opt := SearchOptions{
CountryCode: "au",
UserAgent: "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
}
//lint:ignore SA1012 ignore this bare essentials by passing nil for context and removing context package (despite not being idiomatic go).
serp, err := Search(nil, "First Aid Course Australia Wide First Aid", opt)
if err != nil {
fmt.Print(err.Error())
}
for _, result := range serp {
if strings.Contains(result.URL, "australiawidefirstaid.com.au") {
fmt.Println("Australia Wide First Aid (https://www.australiawidefirstaid.com.au/) found in the serp") fmt.Println("Australia Wide First Aid (https://www.australiawidefirstaid.com.au/) found in the serp")
break break
} }

6
go.mod
View File

@ -1,6 +1,6 @@
module github.com/rocketlaunchr/google-search module github.com/rocketlaunchr/google-search
go 1.20 go 1.16
require ( require (
github.com/gocolly/colly/v2 v2.1.0 github.com/gocolly/colly/v2 v2.1.0
@ -13,14 +13,10 @@ require (
github.com/antchfx/htmlquery v1.3.0 // indirect github.com/antchfx/htmlquery v1.3.0 // indirect
github.com/antchfx/xmlquery v1.3.15 // indirect github.com/antchfx/xmlquery v1.3.15 // indirect
github.com/antchfx/xpath v1.2.4 // indirect github.com/antchfx/xpath v1.2.4 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect github.com/golang/protobuf v1.5.3 // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
github.com/temoto/robotstxt v1.1.2 // indirect github.com/temoto/robotstxt v1.1.2 // indirect
golang.org/x/net v0.10.0 // indirect golang.org/x/net v0.10.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/appengine v1.6.7 // indirect google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect google.golang.org/protobuf v1.30.0 // indirect
) )

3
go.sum
View File

@ -28,6 +28,7 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gocolly/colly v1.2.0 h1:qRz9YAn8FIH0qzgNUw+HT9UN7wm1oF9OBAilwEWpyrI=
github.com/gocolly/colly v1.2.0/go.mod h1:Hof5T3ZswNVsOHYmba1u03W65HDWgpV5HifSuueE0EA= github.com/gocolly/colly v1.2.0/go.mod h1:Hof5T3ZswNVsOHYmba1u03W65HDWgpV5HifSuueE0EA=
github.com/gocolly/colly/v2 v2.1.0 h1:k0DuZkDoCsx51bKpRJNEmcxcp+W5N8ziuwGaSDuFoGs= github.com/gocolly/colly/v2 v2.1.0 h1:k0DuZkDoCsx51bKpRJNEmcxcp+W5N8ziuwGaSDuFoGs=
github.com/gocolly/colly/v2 v2.1.0/go.mod h1:I2MuhsLjQ+Ex+IzK3afNS8/1qP3AedHOusRPcRdC5o0= github.com/gocolly/colly/v2 v2.1.0/go.mod h1:I2MuhsLjQ+Ex+IzK3afNS8/1qP3AedHOusRPcRdC5o0=
@ -119,11 +120,13 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ= golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

View File

@ -31,6 +31,7 @@ type Result struct {
} }
const stdGoogleBase = "https://www.google." const stdGoogleBase = "https://www.google."
const defaultAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
// GoogleDomains represents localized Google homepages. The 2 letter country code is based on ISO 3166-1 alpha-2. // GoogleDomains represents localized Google homepages. The 2 letter country code is based on ISO 3166-1 alpha-2.
// //
@ -284,7 +285,7 @@ func Search(ctx context.Context, searchTerm string, opts ...SearchOptions) ([]Re
} }
if opts[0].UserAgent == "" { if opts[0].UserAgent == "" {
c.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672 Safari/537.36" c.UserAgent = defaultAgent
} else { } else {
c.UserAgent = opts[0].UserAgent c.UserAgent = opts[0].UserAgent
} }

View File

@ -1,23 +1,23 @@
// Copyright 2020-21 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved. // Copyright 2020-21 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package googlesearch package googlesearch_test
import ( import (
"context"
"testing" "testing"
)
var ctx = context.Background() googlesearch "github.com/rocketlaunchr/google-search"
)
func TestSearch(t *testing.T) { func TestSearch(t *testing.T) {
q := "Hello World" q := "Hello World"
opts := SearchOptions{ opts := googlesearch.SearchOptions{
Limit: 20, Limit: 20,
} }
returnLinks, err := Search(ctx, q, opts) //lint:ignore SA1012 ignore this bare essentials by passing nil for context and removing context package (despite not being idiomatic go).
returnLinks, err := googlesearch.Search(nil, q, opts)
if err != nil { if err != nil {
t.Errorf("something went wrong: %v", err) t.Errorf("something went wrong: %v", err)
return return