map improvements + added forums search

This commit is contained in:
partisan 2024-05-21 21:22:36 +02:00
parent c848c72aea
commit 8da387f8e9
12 changed files with 424 additions and 65 deletions

View file

@ -6,7 +6,6 @@ import (
"log"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
@ -17,20 +16,7 @@ func PerformGoogleTextSearch(query, safe, lang string, page int) ([]TextSearchRe
var results []TextSearchResult
client := &http.Client{}
safeParam := "&safe=off"
if safe == "active" {
safeParam = "&safe=active"
}
langParam := ""
if lang != "" {
langParam = "&lr=" + lang
}
// Calculate the start index based on the page number
startIndex := (page - 1) * resultsPerPage
searchURL := "https://www.google.com/search?q=" + url.QueryEscape(query) + safeParam + langParam + "&udm=14&start=" + strconv.Itoa(startIndex)
searchURL := buildSearchURL(query, safe, lang, page, resultsPerPage)
req, err := http.NewRequest("GET", searchURL, nil)
if err != nil {
@ -54,6 +40,35 @@ func PerformGoogleTextSearch(query, safe, lang string, page int) ([]TextSearchRe
return nil, fmt.Errorf("loading HTML document: %v", err)
}
results = parseResults(doc)
if len(results) == 0 {
if debugMode {
log.Println("No results found from Google")
}
}
return results, nil
}
func buildSearchURL(query, safe, lang string, page, resultsPerPage int) string {
safeParam := "&safe=off"
if safe == "active" {
safeParam = "&safe=active"
}
langParam := ""
if lang != "" {
langParam = "&lr=" + lang
}
startIndex := (page - 1) * resultsPerPage
return fmt.Sprintf("https://www.google.com/search?q=%s%s%s&udm=14&start=%d", url.QueryEscape(query), safeParam, langParam, startIndex)
}
func parseResults(doc *goquery.Document) []TextSearchResult {
var results []TextSearchResult
doc.Find(".yuRUbf").Each(func(i int, s *goquery.Selection) {
link := s.Find("a")
href, exists := link.Attr("href")
@ -67,8 +82,8 @@ func PerformGoogleTextSearch(query, safe, lang string, page int) ([]TextSearchRe
header := link.Find("h3").Text()
header = strings.TrimSpace(strings.TrimSuffix(header, ""))
descSelection := doc.Find(".VwiC3b").Eq(i)
description := ""
descSelection := doc.Find(".VwiC3b").Eq(i)
if descSelection.Length() > 0 {
description = descSelection.Text()
}
@ -84,11 +99,5 @@ func PerformGoogleTextSearch(query, safe, lang string, page int) ([]TextSearchRe
}
})
if len(results) == 0 {
if debugMode {
log.Println("No results found from Google")
}
}
return results, nil
return results
}