automatic reputation for search engines

This commit is contained in:
partisan 2024-06-14 17:56:20 +02:00
parent dd9ed4cc53
commit e3d568f6cb
9 changed files with 198 additions and 126 deletions

View file

@ -1,4 +1,3 @@
// text-duckduckgo.go
package main
import (
@ -7,27 +6,30 @@ import (
"net/http"
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
func PerformDuckDuckGoTextSearch(query, safe, lang string, page int) ([]TextSearchResult, error) {
func PerformDuckDuckGoTextSearch(query, safe, lang string, page int) ([]TextSearchResult, time.Duration, error) {
startTime := time.Now() // Start the timer
var results []TextSearchResult
searchURL := buildDuckDuckGoSearchURL(query, page)
resp, err := http.Get(searchURL)
if err != nil {
return nil, fmt.Errorf("making request: %v", err)
return nil, 0, fmt.Errorf("making request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
return nil, 0, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("loading HTML document: %v", err)
return nil, 0, fmt.Errorf("loading HTML document: %v", err)
}
doc.Find(".result__body").Each(func(i int, s *goquery.Selection) {
@ -54,7 +56,9 @@ func PerformDuckDuckGoTextSearch(query, safe, lang string, page int) ([]TextSear
}
})
return results, nil
duration := time.Since(startTime) // Calculate the duration
return results, duration, nil
}
func buildDuckDuckGoSearchURL(query string, page int) string {
@ -63,4 +67,4 @@ func buildDuckDuckGoSearchURL(query string, page int) string {
startParam = fmt.Sprintf("&s=%d", (page-1)*10)
}
return fmt.Sprintf("https://duckduckgo.com/html/?q=%s%s", url.QueryEscape(query), startParam)
}
}