cache v1 + debug mode + buttons on text results
This commit is contained in:
parent
9208104ff7
commit
d5bbfe118d
9 changed files with 228 additions and 65 deletions
|
@ -7,15 +7,26 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
func PerformDuckDuckGoTextSearch(query, safe, lang string) ([]TextSearchResult, error) {
|
||||
func PerformDuckDuckGoTextSearch(query, safe, lang string, page int) ([]TextSearchResult, error) {
|
||||
const resultsPerPage = 10
|
||||
var results []TextSearchResult
|
||||
searchURL := fmt.Sprintf("https://duckduckgo.com/html/?q=%s", url.QueryEscape(query))
|
||||
|
||||
resp, err := http.Get(searchURL)
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
searchURL := fmt.Sprintf("https://duckduckgo.com/html/?q=%s&s=%d", url.QueryEscape(query), (page-1)*resultsPerPage)
|
||||
|
||||
req, err := http.NewRequest("GET", searchURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %v", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("making request: %v", err)
|
||||
}
|
||||
|
@ -49,10 +60,28 @@ func PerformDuckDuckGoTextSearch(query, safe, lang string) ([]TextSearchResult,
|
|||
if debugMode {
|
||||
log.Printf("Processed DuckDuckGo result: %+v\n", result)
|
||||
}
|
||||
} else {
|
||||
if debugMode {
|
||||
log.Printf("Missing 'uddg' parameter in URL: %s\n", rawURL)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if debugMode {
|
||||
log.Printf("Error parsing URL: %s, error: %v\n", rawURL, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if debugMode {
|
||||
log.Printf("Missing 'href' attribute in result anchor tag\n")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if len(results) == 0 {
|
||||
if debugMode {
|
||||
log.Println("No results found from DuckDuckGo")
|
||||
}
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue