automatic reputation for search engines

This commit is contained in:
partisan 2024-06-14 17:56:20 +02:00
parent dd9ed4cc53
commit e3d568f6cb
9 changed files with 198 additions and 126 deletions

View file

@ -25,7 +25,9 @@ type QwantAPIResponse struct {
}
// PerformQwantImageSearch performs an image search on Qwant and returns the results.
func PerformQwantImageSearch(query, safe, lang string, page int) ([]ImageSearchResult, error) {
func PerformQwantImageSearch(query, safe, lang string, page int) ([]ImageSearchResult, time.Duration, error) {
startTime := time.Now() // Start the timer
const resultsPerPage = 50
var offset int
if page <= 1 {
@ -53,29 +55,29 @@ func PerformQwantImageSearch(query, safe, lang string, page int) ([]ImageSearchR
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return nil, fmt.Errorf("creating request: %v", err)
return nil, 0, fmt.Errorf("creating request: %v", err)
}
ImageUserAgent, err := GetUserAgent("Image-Search")
if err != nil {
return nil, err
return nil, 0, err
}
req.Header.Set("User-Agent", ImageUserAgent)
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("making request: %v", err)
return nil, 0, 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)
return nil, 0, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var apiResp QwantAPIResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
return nil, fmt.Errorf("decoding response: %v", err)
return nil, 0, fmt.Errorf("decoding response: %v", err)
}
var results []ImageSearchResult
@ -91,5 +93,7 @@ func PerformQwantImageSearch(query, safe, lang string, page int) ([]ImageSearchR
})
}
return results, nil
duration := time.Since(startTime) // Calculate the duration
return results, duration, nil
}