Skip to content

Commit

Permalink
Fix optional UserAgent
Browse files Browse the repository at this point in the history
Added an example
  • Loading branch information
chrisjoyce911 committed May 26, 2023
1 parent 8179baa commit bcbf8ae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
30 changes: 30 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,33 @@ func ExampleSearch() {
// 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",
}

serp, err := Search(context.Background(), "First Aid Course Australia Wide First Aid", opt)

if err != nil {
fmt.Print(err.Error())
}

for _, resault := range serp {
if strings.Contains(resault.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

}
4 changes: 3 additions & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Result struct {
}

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.
//
Expand Down Expand Up @@ -284,7 +285,8 @@ func Search(ctx context.Context, searchTerm string, opts ...SearchOptions) ([]Re
}

if opts[0].UserAgent == "" {
c.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
c.UserAgent = defaultAgent
} else {
c.UserAgent = opts[0].UserAgent
}

Expand Down

0 comments on commit bcbf8ae

Please sign in to comment.