2024-06-09 12:43:46 +02:00
|
|
|
// text-duckduckgo.go
|
2024-05-18 01:59:29 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2024-06-09 12:43:46 +02:00
|
|
|
func PerformDuckDuckGoTextSearch(query, safe, lang string) ([]TextSearchResult, error) {
|
2024-05-18 01:59:29 +02:00
|
|
|
var results []TextSearchResult
|
2024-06-09 12:43:46 +02:00
|
|
|
searchURL := fmt.Sprintf("https://duckduckgo.com/html/?q=%s", url.QueryEscape(query))
|
2024-05-18 01:59:29 +02:00
|
|
|
|
2024-06-09 12:43:46 +02:00
|
|
|
resp, err := http.Get(searchURL)
|
2024-05-18 01:59:29 +02:00
|
|
|
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
|
|
|
|
}
|