Skip to content

Commit

Permalink
SearchTimeout error handling and proper tabs close
Browse files Browse the repository at this point in the history
  • Loading branch information
karust committed Jun 27, 2023
1 parent c175d0c commit 72bddf7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
8 changes: 5 additions & 3 deletions baidu/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func (baid *Baidu) Search(query core.Query) ([]core.SearchResult, error) {

results, err := page.Timeout(baid.Timeout).Search("div.c-container.new-pmd")
if err != nil {
defer page.Close()
logrus.Errorf("Cannot parse search results: %s", err)
return nil, core.ErrSearchTimeout
}

// Check why no results, maybe captcha?
Expand All @@ -63,12 +65,12 @@ func (baid *Baidu) Search(query core.Query) ([]core.SearchResult, error) {

if baid.isCaptcha(page) {
logrus.Errorf("Baidu captcha occurred during: %s", url)
return searchResults, core.ErrCaptcha
return nil, core.ErrCaptcha
} else if baid.isTimeout(page) {
logrus.Errorf("Baidu timeout occurred during: %s", url)
return searchResults, core.ErrCaptcha
return nil, core.ErrCaptcha
}
return searchResults, nil
return nil, nil
}

resultElements, err := results.All()
Expand Down
1 change: 1 addition & 0 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

var ErrCaptcha = errors.New("Captcha detected")
var ErrSearchTimeout = errors.New("Timeout. Cannot find element on page")

type SearchResult struct {
Rank int `json:"rank"`
Expand Down
7 changes: 3 additions & 4 deletions core/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ func NewServer(host string, port int, searchEngines ...SearchEngine) *Server {
switch err {
case ErrCaptcha:
err = errors.New(fmt.Sprintf("Captcha found, please stop sending requests for a while\n%s", err))
c.Status(503)
default:
c.Status(500)
case ErrSearchTimeout:
err = errors.New(fmt.Sprintf("Error: %s\nProbably need to update CSS selector", err))
}

logrus.Errorf("Error during %s search: %s", locEngine.Name(), err)
return err
return fiber.NewError(fiber.StatusServiceUnavailable, err.Error())
}

return c.JSON(res)
Expand Down
6 changes: 4 additions & 2 deletions google/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ func (gogl *Google) Search(query core.Query) ([]core.SearchResult, error) {

results, err := page.Timeout(gogl.Timeout).Search("div[data-hveid][data-ved][lang], div[data-surl][jsaction]")
if err != nil {
defer page.Close()
logrus.Errorf("Cannot parse search results: %s", err)
return nil, core.ErrSearchTimeout
}

// Check why no results, maybe captcha?
Expand All @@ -87,9 +89,9 @@ func (gogl *Google) Search(query core.Query) ([]core.SearchResult, error) {

if gogl.isCaptcha(page) {
logrus.Errorf("Google captcha occurred during: %s", url)
return searchResults, core.ErrCaptcha
return nil, core.ErrCaptcha
}
return searchResults, nil
return nil, err
}

totalResults, err := gogl.FindTotalResults(page)
Expand Down
4 changes: 3 additions & 1 deletion yandex/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ func (yand *Yandex) Search(query core.Query) ([]core.SearchResult, error) {
// Get all search results in page
searchRes, err := page.Timeout(yand.Timeout).Search("li.serp-item")
if err != nil {
defer page.Close()
logrus.Errorf("Cannot parse search results: %s", err)
return nil, core.ErrSearchTimeout
}

// Check why no results, maybe captcha?
Expand All @@ -121,7 +123,7 @@ func (yand *Yandex) Search(query core.Query) ([]core.SearchResult, error) {
logrus.Errorf("No results found")
} else if yand.isCaptcha(page) {
logrus.Errorf("Yandex captcha occurred during: %s", url)
return allResults, core.ErrCaptcha
return nil, core.ErrCaptcha
}
break
}
Expand Down

0 comments on commit 72bddf7

Please sign in to comment.