added ProxyRetry to config and fixed ProxyStrict

This commit is contained in:
partisan 2025-02-22 22:36:54 +01:00
parent ab707a91e8
commit 35e657bccd
17 changed files with 224 additions and 186 deletions

View file

@ -11,56 +11,46 @@ import (
)
func PerformGoogleTextSearch(query, safe, lang string, page int) ([]TextSearchResult, time.Duration, error) {
startTime := time.Now()
const resultsPerPage = 10
var results []TextSearchResult
startTime := time.Now() // Start the timer
// Build the search URL
// 1) Build the search URL
searchURL := buildSearchURL(query, safe, lang, page, resultsPerPage)
// Create a new request
// 2) Create a new request
req, err := http.NewRequest("GET", searchURL, nil)
if err != nil {
return nil, 0, fmt.Errorf("failed to create request: %v", err)
}
// Generate and set User-Agent header
TextUserAgent, err := GetUserAgent("Google-Text-Search")
// 3) Generate and set a User-Agent header
userAgent, err := GetUserAgent("Google-Text-Search")
if err != nil {
return nil, 0, err
}
req.Header.Set("User-Agent", TextUserAgent)
// Perform the request using proxy if MetaProxy is enabled
var resp *http.Response
if config.MetaProxyEnabled && metaProxyClient != nil {
resp, err = metaProxyClient.Do(req)
} else {
client := &http.Client{}
resp, err = client.Do(req)
}
req.Header.Set("User-Agent", userAgent)
// 4) Use the meta-proxy wrapper
resp, err := DoMetaProxyRequest(req)
if err != nil {
return nil, 0, fmt.Errorf("making request: %v", err)
return nil, 0, fmt.Errorf("failed to do meta-request: %v", err)
}
defer resp.Body.Close()
// Check for HTTP status code
// 5) Check HTTP status
if resp.StatusCode != http.StatusOK {
return nil, 0, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
// Parse the HTML response
// 6) Parse the HTML response
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, 0, fmt.Errorf("loading HTML document: %v", err)
}
results := parseResults(doc)
// Extract search results
results = parseResults(doc)
duration := time.Since(startTime) // Calculate the duration
// 7) Calculate duration
duration := time.Since(startTime)
if len(results) == 0 {
printDebug("No results found from Google Search")