quant+google+duckdcuckgo results

This commit is contained in:
partisan 2024-05-18 01:59:29 +02:00
parent c8cf762222
commit a3a7c69679
6 changed files with 187 additions and 40 deletions

View file

@ -1,4 +1,3 @@
// text-quant.go
package main
import (
@ -13,22 +12,39 @@ import (
type QwantTextAPIResponse struct {
Data struct {
Result struct {
Items []struct {
Title string `json:"title"`
Url string `json:"url"`
Snippet string `json:"desc"`
Items struct {
Mainline []struct {
Items []struct {
URL string `json:"url"`
Title string `json:"title"`
Description string `json:"desc"`
} `json:"items"`
} `json:"mainline"`
} `json:"items"`
} `json:"result"`
} `json:"data"`
}
// PerformQwantTextSearch contacts the Qwant API and returns a slice of TextSearchResult
func PerformQwantTextSearch(query, safe, lang string) ([]TextSearchResult, error) {
const resultsPerPage = 10
apiURL := fmt.Sprintf("https://api.qwant.com/v3/search/web?t=web&q=%s&count=%d&locale=%s&safesearch=%s",
const offset = 0
// Ensure safe search is disabled by default if not specified
if safe == "" {
safe = "0"
}
// Default to English Canada locale if not specified
if lang == "" {
lang = "en_CA"
}
apiURL := fmt.Sprintf("https://api.qwant.com/v3/search/web?q=%s&count=%d&locale=%s&offset=%d&device=desktop",
url.QueryEscape(query),
resultsPerPage,
lang,
safe)
offset)
client := &http.Client{Timeout: 10 * time.Second}
@ -54,14 +70,30 @@ func PerformQwantTextSearch(query, safe, lang string) ([]TextSearchResult, error
return nil, fmt.Errorf("decoding response: %v", err)
}
// Extracting results from the nested JSON structure
if len(apiResp.Data.Result.Items.Mainline) == 0 {
return nil, fmt.Errorf("no search results found")
}
var results []TextSearchResult
for _, item := range apiResp.Data.Result.Items {
for _, item := range apiResp.Data.Result.Items.Mainline[0].Items {
cleanURL := cleanQwantURL(item.URL)
results = append(results, TextSearchResult{
URL: item.Url,
URL: cleanURL,
Header: item.Title,
Description: item.Snippet,
Description: item.Description,
Source: "Qwant",
})
}
return results, nil
}
// cleanQwantURL extracts the main part of the URL, removing tracking information
func cleanQwantURL(rawURL string) string {
u, err := url.Parse(rawURL)
if err != nil {
return rawURL
}
return u.Scheme + "://" + u.Host + u.Path
}