Search/text-google.go

114 lines
2.5 KiB
Go
Raw Normal View History

2024-04-15 06:35:17 +00:00
package main
import (
"fmt"
2024-06-14 07:07:07 +00:00
"log"
"net/http"
2024-04-15 06:35:17 +00:00
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
)
2024-06-09 19:44:49 +00:00
func PerformGoogleTextSearch(query, safe, lang string, page int) ([]TextSearchResult, error) {
2024-06-14 07:07:07 +00:00
const resultsPerPage = 10
2024-06-09 19:44:49 +00:00
var results []TextSearchResult
2024-04-15 06:35:17 +00:00
2024-06-14 07:07:07 +00:00
client := &http.Client{}
searchURL := buildSearchURL(query, safe, lang, page, resultsPerPage)
req, err := http.NewRequest("GET", searchURL, nil)
2024-04-15 06:35:17 +00:00
if err != nil {
2024-06-14 07:07:07 +00:00
return nil, fmt.Errorf("failed to create request: %v", err)
2024-04-15 06:35:17 +00:00
}
2024-06-14 07:07:07 +00:00
// User Agent generation
TextUserAgent, err := GetUserAgent("Text-Search")
2024-06-09 19:44:49 +00:00
if err != nil {
2024-06-14 07:07:07 +00:00
fmt.Println("Error:", err)
return nil, err
}
if debugMode {
fmt.Println("Generated User Agent (text):", TextUserAgent)
}
req.Header.Set("User-Agent", TextUserAgent)
resp, err := client.Do(req)
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)
}
results = parseResults(doc)
if len(results) == 0 {
if debugMode {
log.Println("No results found from Google")
}
2024-05-21 19:22:36 +00:00
}
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
}
2024-06-14 07:07:07 +00:00
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)
2024-05-21 19:22:36 +00:00
}
2024-06-14 07:07:07 +00:00
func parseResults(doc *goquery.Document) []TextSearchResult {
2024-05-21 19:22:36 +00:00
var results []TextSearchResult
2024-04-15 06:35:17 +00:00
doc.Find(".yuRUbf").Each(func(i int, s *goquery.Selection) {
link := s.Find("a")
href, exists := link.Attr("href")
if !exists {
2024-06-14 07:07:07 +00:00
if debugMode {
log.Printf("No href attribute found for result %d\n", i)
}
return
}
2024-04-15 06:35:17 +00:00
header := link.Find("h3").Text()
header = strings.TrimSpace(strings.TrimSuffix(header, ""))
description := ""
2024-05-21 19:22:36 +00:00
descSelection := doc.Find(".VwiC3b").Eq(i)
2024-04-15 06:35:17 +00:00
if descSelection.Length() > 0 {
description = descSelection.Text()
}
2024-05-17 23:59:29 +00:00
result := TextSearchResult{
2024-04-15 06:35:17 +00:00
URL: href,
Header: header,
Description: description,
2024-05-17 23:59:29 +00:00
}
results = append(results, result)
2024-06-14 07:07:07 +00:00
if debugMode {
log.Printf("Google result: %+v\n", result)
}
2024-04-15 06:35:17 +00:00
})
2024-06-14 07:07:07 +00:00
return results
2024-04-15 06:35:17 +00:00
}