removed results loging + added logs for no results returned where missing + improved image fetching
This commit is contained in:
parent
a138928d63
commit
87ac6f05f9
5 changed files with 19 additions and 14 deletions
14
images.go
14
images.go
|
@ -97,15 +97,11 @@ func getImageResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string
|
||||||
|
|
||||||
func fetchImageResults(query, safe, lang string, page int) []ImageSearchResult {
|
func fetchImageResults(query, safe, lang string, page int) []ImageSearchResult {
|
||||||
var results []ImageSearchResult
|
var results []ImageSearchResult
|
||||||
var err error
|
|
||||||
var duration time.Duration
|
|
||||||
|
|
||||||
for attempts := 0; attempts < len(imageSearchEngines); attempts++ {
|
for _, engine := range imageSearchEngines {
|
||||||
engine := selectSearchEngine(imageSearchEngines)
|
|
||||||
log.Printf("Using image search engine: %s", engine.Name)
|
log.Printf("Using image search engine: %s", engine.Name)
|
||||||
|
|
||||||
var searchResults []SearchResult
|
searchResults, duration, err := engine.Func(query, safe, lang, page)
|
||||||
searchResults, duration, err = engine.Func(query, safe, lang, page)
|
|
||||||
updateEngineMetrics(&engine, duration, err == nil)
|
updateEngineMetrics(&engine, duration, err == nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error performing image search with %s: %v", engine.Name, err)
|
log.Printf("Error performing image search with %s: %v", engine.Name, err)
|
||||||
|
@ -116,11 +112,17 @@ func fetchImageResults(query, safe, lang string, page int) []ImageSearchResult {
|
||||||
results = append(results, result.(ImageSearchResult))
|
results = append(results, result.(ImageSearchResult))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If results are found, break out of the loop
|
||||||
if len(results) > 0 {
|
if len(results) > 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If no results found after trying all engines
|
||||||
|
if len(results) == 0 {
|
||||||
|
log.Printf("No image results found for query: %s", query)
|
||||||
|
}
|
||||||
|
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,5 +69,10 @@ func PerformBraveTextSearch(query, safe, lang string, offset int) ([]TextSearchR
|
||||||
|
|
||||||
duration := time.Since(startTime) // Calculate the duration
|
duration := time.Since(startTime) // Calculate the duration
|
||||||
|
|
||||||
|
// Return an error if no results are found
|
||||||
|
if len(results) == 0 {
|
||||||
|
return nil, duration, fmt.Errorf("no results found")
|
||||||
|
}
|
||||||
|
|
||||||
return results, duration, nil
|
return results, duration, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,10 +31,6 @@ func PerformGoogleTextSearch(query, safe, lang string, page int) ([]TextSearchRe
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if debugMode {
|
|
||||||
fmt.Println("Generated User Agent (text):", TextUserAgent)
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("User-Agent", TextUserAgent)
|
req.Header.Set("User-Agent", TextUserAgent)
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
|
|
|
@ -34,10 +34,6 @@ func PerformLibreXTextSearch(query, safe, lang string, page int) ([]TextSearchRe
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if debugMode {
|
|
||||||
log.Println("Generated User Agent (text):", userAgent)
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", searchURL, nil)
|
req, err := http.NewRequest("GET", searchURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
|
@ -74,9 +70,14 @@ func PerformLibreXTextSearch(query, safe, lang string, page int) ([]TextSearchRe
|
||||||
|
|
||||||
duration := time.Since(startTime) // Calculate the duration
|
duration := time.Since(startTime) // Calculate the duration
|
||||||
|
|
||||||
|
if len(results) == 0 {
|
||||||
|
return nil, duration, fmt.Errorf("no results found")
|
||||||
|
}
|
||||||
|
|
||||||
return results, duration, nil
|
return results, duration, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is just stupid it will probbably lead to printing error twice
|
||||||
func logError(message string, err error) error {
|
func logError(message string, err error) error {
|
||||||
log.Printf("%s: %v", message, err)
|
log.Printf("%s: %v", message, err)
|
||||||
return fmt.Errorf("%s: %w", message, err)
|
return fmt.Errorf("%s: %w", message, err)
|
||||||
|
|
1
text.go
1
text.go
|
@ -105,6 +105,7 @@ func fetchTextResults(query, safe, lang string, page int) []TextSearchResult {
|
||||||
results = append(results, result.(TextSearchResult))
|
results = append(results, result.(TextSearchResult))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If results are found, break out of the loop
|
||||||
if len(results) > 0 {
|
if len(results) > 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue