59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
|
// text-duckduckgo.go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/PuerkitoBio/goquery"
|
||
|
)
|
||
|
|
||
|
func PerformDuckDuckGoTextSearch(query, safe, lang string) ([]TextSearchResult, error) {
|
||
|
var results []TextSearchResult
|
||
|
searchURL := fmt.Sprintf("https://duckduckgo.com/html/?q=%s", url.QueryEscape(query))
|
||
|
|
||
|
resp, err := http.Get(searchURL)
|
||
|
if err != nil {
|
||
|
return nil, 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)
|
||
|
}
|
||
|
|
||
|
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("loading HTML document: %v", err)
|
||
|
}
|
||
|
|
||
|
doc.Find(".result__body").Each(func(i int, s *goquery.Selection) {
|
||
|
header := s.Find(".result__a").Text()
|
||
|
description := s.Find(".result__snippet").Text()
|
||
|
rawURL, exists := s.Find(".result__a").Attr("href")
|
||
|
if exists {
|
||
|
parsedURL, err := url.Parse(rawURL)
|
||
|
if err == nil {
|
||
|
queryParams := parsedURL.Query()
|
||
|
uddg := queryParams.Get("uddg")
|
||
|
if uddg != "" {
|
||
|
result := TextSearchResult{
|
||
|
URL: uddg,
|
||
|
Header: strings.TrimSpace(header),
|
||
|
Description: strings.TrimSpace(description),
|
||
|
}
|
||
|
results = append(results, result)
|
||
|
if debugMode {
|
||
|
log.Printf("Processed DuckDuckGo result: %+v\n", result)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
return results, nil
|
||
|
}
|