Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: switch to consts instead of runtime structs and comments #333

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/defaults_cat_general.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var generalRequiredByOriginEngines = []engines.Name{
}

var generalPreferredEngines = []engines.Name{
engines.ETOOLS,
engines.ETOOLS, // Not in ByOrigin because it only gives 10 results across a lot of engines that it scrapes from.
}

var generalPreferredByOriginEngines = []engines.Name{
Expand Down
12 changes: 12 additions & 0 deletions src/search/engines/bing/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package bing

import (
"github.com/hearchco/agent/src/search/engines"
)

const (
seName = engines.BING
searchURL = "https://www.bing.com/search"
)

var origins = [...]engines.Name{seName}
20 changes: 0 additions & 20 deletions src/search/engines/bing/infoparams.go

This file was deleted.

9 changes: 8 additions & 1 deletion src/search/engines/bing/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import (
"github.com/hearchco/agent/src/search/engines/options"
)

const (
paramKeyPage = "first"
paramKeyLocale = "setlang" // Should be first 2 characters of Locale.
paramKeyLocaleSec = "cc" // Should be last 2 characters of Locale.
paramKeySafeSearch = "" // Always enabled.
)

func localeParamString(locale options.Locale) string {
spl := strings.SplitN(strings.ToLower(locale.String()), "_", 2)
return fmt.Sprintf("%v=%v&%v=%v", params.Locale, spl[0], params.LocaleSec, spl[1])
return fmt.Sprintf("%v=%v&%v=%v", paramKeyLocale, spl[0], paramKeyLocaleSec, spl[1])
}
20 changes: 10 additions & 10 deletions src/search/engines/bing/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ type Engine struct {
scraper.EngineBase
}

func New() *Engine {
return &Engine{EngineBase: scraper.EngineBase{
Name: info.Name,
Origins: info.Origins,
func New() scraper.Enginer {
return &Engine{scraper.EngineBase{
Name: seName,
Origins: origins[:],
}}
}

Expand Down Expand Up @@ -84,23 +84,23 @@ func (se Engine) Search(query string, opts options.Options, resChan chan result.
})

// Static params.
localeParam := localeParamString(opts.Locale)
paramLocale := localeParamString(opts.Locale)

for i := range opts.Pages.Max {
pageNum0 := i + opts.Pages.Start
ctx := colly.NewContext()
ctx.Put("page", strconv.Itoa(i))

// Dynamic params.
pageParam := ""
paramPage := ""
if pageNum0 > 0 {
pageParam = fmt.Sprintf("%v=%v", params.Page, pageNum0*10+1)
paramPage = fmt.Sprintf("%v=%v", paramKeyPage, pageNum0*10+1)
}

combinedParams := morestrings.JoinNonEmpty([]string{pageParam, localeParam}, "&", "&")
combinedParams := morestrings.JoinNonEmpty([]string{paramPage, paramLocale}, "&", "&")

urll := fmt.Sprintf("%v?q=%v%v", info.URL, query, combinedParams)
anonUrll := fmt.Sprintf("%v?q=%v%v", info.URL, anonymize.String(query), combinedParams)
urll := fmt.Sprintf("%v?q=%v%v", searchURL, query, combinedParams)
anonUrll := fmt.Sprintf("%v?q=%v%v", searchURL, anonymize.String(query), combinedParams)

if err := se.Get(ctx, urll, anonUrll); err != nil {
retErrors = append(retErrors, err)
Expand Down
7 changes: 2 additions & 5 deletions src/search/engines/bing/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import (
)

func TestSearch(t *testing.T) {
// Search engine name
seName := info.Name

// testing options
// Testing options.
conf := _engines_test.NewConfig(seName)
opt := _engines_test.NewOpts()

// test cases
// Test cases.
tchar := []_engines_test.TestCaseHasAnyResults{{
Query: "ping",
Options: opt,
Expand Down
12 changes: 12 additions & 0 deletions src/search/engines/bingimages/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package bingimages

import (
"github.com/hearchco/agent/src/search/engines"
)

const (
seName = engines.BINGIMAGES
searchURL = "https://www.bing.com/images/async"
)

var origins = [...]engines.Name{seName}
23 changes: 0 additions & 23 deletions src/search/engines/bingimages/infoparams.go

This file was deleted.

12 changes: 11 additions & 1 deletion src/search/engines/bingimages/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ import (
"github.com/hearchco/agent/src/search/engines/options"
)

const (
paramKeyPage = "first"
paramKeyLocale = "setlang" // Should be first 2 characters of Locale.
paramKeyLocaleSec = "cc" // Should be last 2 characters of Locale.
paramKeySafeSearch = "" // Always enabled.

paramAsync = "async=1"
paramCount = "count=35"
)

func localeParamString(locale options.Locale) string {
spl := strings.SplitN(strings.ToLower(locale.String()), "_", 2)
return fmt.Sprintf("%v=%v&%v=%v", params.Locale, spl[0], params.LocaleSec, spl[1])
return fmt.Sprintf("%v=%v&%v=%v", paramKeyLocale, spl[0], paramKeyLocaleSec, spl[1])
}
18 changes: 9 additions & 9 deletions src/search/engines/bingimages/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ type Engine struct {
scraper.EngineBase
}

func New() *Engine {
return &Engine{EngineBase: scraper.EngineBase{
Name: info.Name,
Origins: info.Origins,
func New() scraper.Enginer {
return &Engine{scraper.EngineBase{
Name: seName,
Origins: origins[:],
}}
}

Expand Down Expand Up @@ -222,20 +222,20 @@ func (se Engine) Search(query string, opts options.Options, resChan chan result.
})

// Static params.
localeParam := localeParamString(opts.Locale)
paramLocale := localeParamString(opts.Locale)

for i := range opts.Pages.Max {
pageNum0 := i + opts.Pages.Start
ctx := colly.NewContext()
ctx.Put("page", strconv.Itoa(i))

// Dynamic params.
pageParam := fmt.Sprintf("%v=%v", params.Page, pageNum0*35+1)
paramPage := fmt.Sprintf("%v=%v", paramKeyPage, pageNum0*35+1)

combinedParams := morestrings.JoinNonEmpty([]string{asyncParam, pageParam, countParam, localeParam}, "&", "&")
combinedParams := morestrings.JoinNonEmpty([]string{paramAsync, paramPage, paramCount, paramLocale}, "&", "&")

urll := fmt.Sprintf("%v?q=%v%v", info.URL, query, combinedParams)
anonUrll := fmt.Sprintf("%v?q=%v%v", info.URL, anonymize.String(query), combinedParams)
urll := fmt.Sprintf("%v?q=%v%v", searchURL, query, combinedParams)
anonUrll := fmt.Sprintf("%v?q=%v%v", searchURL, anonymize.String(query), combinedParams)

if err := se.Get(ctx, urll, anonUrll); err != nil {
retErrors = append(retErrors, err)
Expand Down
7 changes: 2 additions & 5 deletions src/search/engines/bingimages/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import (
)

func TestSearch(t *testing.T) {
// Search engine name
seName := info.Name

// testing options
// Testing options.
conf := _engines_test.NewConfig(seName)
opt := _engines_test.NewOpts()

// test cases
// Test cases.
tchar := []_engines_test.TestCaseHasAnyResults{{
Query: "ping",
Options: opt,
Expand Down
21 changes: 0 additions & 21 deletions src/search/engines/brave/cookies.go

This file was deleted.

12 changes: 12 additions & 0 deletions src/search/engines/brave/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package brave

import (
"github.com/hearchco/agent/src/search/engines"
)

const (
seName = engines.BRAVE
searchURL = "https://search.brave.com/search"
)

var origins = [...]engines.Name{seName, engines.GOOGLE}
22 changes: 0 additions & 22 deletions src/search/engines/brave/infoparams.go

This file was deleted.

30 changes: 30 additions & 0 deletions src/search/engines/brave/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package brave

import (
"fmt"
"strings"

"github.com/hearchco/agent/src/search/engines/options"
)

const (
paramKeyPage = "offset"
paramKeyLocale = "country" // Should be last 2 characters of Locale.
paramKeySafeSearch = "safesearch" // Can be "off" or "strict".

paramSource = "source=web"
paramSpellcheck = "spellcheck=0"
)

func localeCookieString(locale options.Locale) string {
region := strings.SplitN(strings.ToLower(locale.String()), "_", 2)[1]
return fmt.Sprintf("%v=%v", paramKeyLocale, region)
}

func safeSearchCookieString(safesearch bool) string {
if safesearch {
return fmt.Sprintf("%v=%v", paramKeySafeSearch, "strict")
} else {
return fmt.Sprintf("%v=%v", paramKeySafeSearch, "off")
}
}
20 changes: 10 additions & 10 deletions src/search/engines/brave/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ type Engine struct {
scraper.EngineBase
}

func New() *Engine {
return &Engine{EngineBase: scraper.EngineBase{
Name: info.Name,
Origins: info.Origins,
func New() scraper.Enginer {
return &Engine{scraper.EngineBase{
Name: seName,
Origins: origins[:],
}}
}

Expand Down Expand Up @@ -80,15 +80,15 @@ func (se Engine) Search(query string, opts options.Options, resChan chan result.
ctx.Put("page", strconv.Itoa(i))

// Dynamic params.
pageParam := ""
combinedParams := morestrings.JoinNonEmpty([]string{sourceParam, pageParam}, "&", "&")
paramPage := ""
combinedParams := morestrings.JoinNonEmpty([]string{paramSource, paramPage}, "&", "&")
if pageNum0 > 0 {
pageParam = fmt.Sprintf("%v=%v", params.Page, pageNum0)
combinedParams = morestrings.JoinNonEmpty([]string{spellcheckParam, pageParam}, "&", "&")
paramPage = fmt.Sprintf("%v=%v", paramKeyPage, pageNum0)
combinedParams = morestrings.JoinNonEmpty([]string{paramSpellcheck, paramPage}, "&", "&")
}

urll := fmt.Sprintf("%v?q=%v%v", info.URL, query, combinedParams)
anonUrll := fmt.Sprintf("%v?q=%v%v", info.URL, anonymize.String(query), combinedParams)
urll := fmt.Sprintf("%v?q=%v%v", searchURL, query, combinedParams)
anonUrll := fmt.Sprintf("%v?q=%v%v", searchURL, anonymize.String(query), combinedParams)

if err := se.Get(ctx, urll, anonUrll); err != nil {
retErrors = append(retErrors, err)
Expand Down
7 changes: 2 additions & 5 deletions src/search/engines/brave/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import (
)

func TestSearch(t *testing.T) {
// Search engine name
seName := info.Name

// testing options
// Testing options.
conf := _engines_test.NewConfig(seName)
opt := _engines_test.NewOpts()

// test cases
// Test cases.
tchar := []_engines_test.TestCaseHasAnyResults{{
Query: "ping",
Options: opt,
Expand Down
13 changes: 0 additions & 13 deletions src/search/engines/duckduckgo/cookies.go

This file was deleted.

Loading
Loading