improved duckduckgo
This commit is contained in:
parent
305474936b
commit
c848c72aea
1 changed files with 43 additions and 2 deletions
|
@ -12,12 +12,52 @@ import (
|
|||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
const (
|
||||
resultsPerPage = 10
|
||||
)
|
||||
|
||||
func getVQD(query string) (string, error) {
|
||||
queryURL := fmt.Sprintf("https://duckduckgo.com/?q=%s", url.QueryEscape(query))
|
||||
resp, err := http.Get(queryURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to fetch vqd: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("loading HTML document: %v", err)
|
||||
}
|
||||
|
||||
var vqd string
|
||||
doc.Find("script").Each(func(i int, s *goquery.Selection) {
|
||||
text := s.Text()
|
||||
if strings.Contains(text, "vqd=\"") {
|
||||
start := strings.Index(text, "vqd=\"") + 5
|
||||
end := strings.Index(text[start:], "\"")
|
||||
vqd = text[start : start+end]
|
||||
}
|
||||
})
|
||||
|
||||
if vqd == "" {
|
||||
return "", fmt.Errorf("vqd not found")
|
||||
}
|
||||
|
||||
return vqd, nil
|
||||
}
|
||||
|
||||
func PerformDuckDuckGoTextSearch(query, safe, lang string, page int) ([]TextSearchResult, error) {
|
||||
const resultsPerPage = 10
|
||||
var results []TextSearchResult
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
searchURL := fmt.Sprintf("https://duckduckgo.com/html/?q=%s&s=%d", url.QueryEscape(query), (page-1)*resultsPerPage)
|
||||
|
||||
vqd, err := getVQD(query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get vqd: %v", err)
|
||||
}
|
||||
|
||||
searchURL := fmt.Sprintf("https://duckduckgo.com/html/?q=%s&kl=%s&safe=%s&s=%d&vqd=%s",
|
||||
url.QueryEscape(query), lang, safe, (page-1)*resultsPerPage, vqd)
|
||||
|
||||
req, err := http.NewRequest("GET", searchURL, nil)
|
||||
if err != nil {
|
||||
|
@ -55,6 +95,7 @@ func PerformDuckDuckGoTextSearch(query, safe, lang string, page int) ([]TextSear
|
|||
URL: uddg,
|
||||
Header: strings.TrimSpace(header),
|
||||
Description: strings.TrimSpace(description),
|
||||
Source: "DuckDuckGo",
|
||||
}
|
||||
results = append(results, result)
|
||||
if debugMode {
|
||||
|
|
Loading…
Add table
Reference in a new issue