retry anouther search engine when 0 results
This commit is contained in:
parent
cc824b5820
commit
6e158529f9
2 changed files with 45 additions and 17 deletions
29
images.go
29
images.go
|
@ -90,7 +90,9 @@ func getImageResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string
|
||||||
case results := <-cacheChan:
|
case results := <-cacheChan:
|
||||||
if results == nil {
|
if results == nil {
|
||||||
combinedResults = fetchImageResults(query, safe, lang, page)
|
combinedResults = fetchImageResults(query, safe, lang, page)
|
||||||
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
if len(combinedResults) > 0 {
|
||||||
|
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
_, _, imageResults := convertToSpecificResults(results)
|
_, _, imageResults := convertToSpecificResults(results)
|
||||||
combinedResults = imageResults
|
combinedResults = imageResults
|
||||||
|
@ -98,20 +100,31 @@ func getImageResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string
|
||||||
case <-time.After(2 * time.Second):
|
case <-time.After(2 * time.Second):
|
||||||
log.Println("Cache check timeout")
|
log.Println("Cache check timeout")
|
||||||
combinedResults = fetchImageResults(query, safe, lang, page)
|
combinedResults = fetchImageResults(query, safe, lang, page)
|
||||||
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
if len(combinedResults) > 0 {
|
||||||
|
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return combinedResults
|
return combinedResults
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchImageResults(query, safe, lang string, page int) []ImageSearchResult {
|
func fetchImageResults(query, safe, lang string, page int) []ImageSearchResult {
|
||||||
engine := selectImageEngine()
|
var results []ImageSearchResult
|
||||||
log.Printf("Using image search engine: %s", engine.Name)
|
var err error
|
||||||
|
|
||||||
results, err := engine.Func(query, safe, lang, page)
|
for attempts := 0; attempts < len(imageEngines); attempts++ {
|
||||||
if err != nil {
|
engine := selectImageEngine()
|
||||||
log.Printf("Error performing image search with %s: %v", engine.Name, err)
|
log.Printf("Using image search engine: %s", engine.Name)
|
||||||
return nil
|
|
||||||
|
results, err = engine.Func(query, safe, lang, page)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error performing image search with %s: %v", engine.Name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(results) > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
33
text.go
33
text.go
|
@ -69,7 +69,9 @@ func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string,
|
||||||
case results := <-cacheChan:
|
case results := <-cacheChan:
|
||||||
if results == nil {
|
if results == nil {
|
||||||
combinedResults = fetchTextResults(query, safe, lang, page)
|
combinedResults = fetchTextResults(query, safe, lang, page)
|
||||||
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
if len(combinedResults) > 0 {
|
||||||
|
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
textResults, _, _ := convertToSpecificResults(results)
|
textResults, _, _ := convertToSpecificResults(results)
|
||||||
combinedResults = textResults
|
combinedResults = textResults
|
||||||
|
@ -77,7 +79,9 @@ func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string,
|
||||||
case <-time.After(2 * time.Second):
|
case <-time.After(2 * time.Second):
|
||||||
log.Println("Cache check timeout")
|
log.Println("Cache check timeout")
|
||||||
combinedResults = fetchTextResults(query, safe, lang, page)
|
combinedResults = fetchTextResults(query, safe, lang, page)
|
||||||
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
if len(combinedResults) > 0 {
|
||||||
|
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return combinedResults
|
return combinedResults
|
||||||
|
@ -88,20 +92,31 @@ func prefetchPage(query, safe, lang string, page int) {
|
||||||
if _, exists := resultsCache.Get(cacheKey); !exists {
|
if _, exists := resultsCache.Get(cacheKey); !exists {
|
||||||
log.Printf("Page %d not cached, caching now...", page)
|
log.Printf("Page %d not cached, caching now...", page)
|
||||||
pageResults := fetchTextResults(query, safe, lang, page)
|
pageResults := fetchTextResults(query, safe, lang, page)
|
||||||
resultsCache.Set(cacheKey, convertToSearchResults(pageResults))
|
if len(pageResults) > 0 {
|
||||||
|
resultsCache.Set(cacheKey, convertToSearchResults(pageResults))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Page %d already cached", page)
|
log.Printf("Page %d already cached", page)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchTextResults(query, safe, lang string, page int) []TextSearchResult {
|
func fetchTextResults(query, safe, lang string, page int) []TextSearchResult {
|
||||||
engine := selectSearchEngine()
|
var results []TextSearchResult
|
||||||
log.Printf("Using search engine: %s", engine.Name)
|
var err error
|
||||||
|
|
||||||
results, err := engine.Func(query, safe, lang, page)
|
for attempts := 0; attempts < len(searchEngines); attempts++ {
|
||||||
if err != nil {
|
engine := selectSearchEngine()
|
||||||
log.Printf("Error performing search with %s: %v", engine.Name, err)
|
log.Printf("Using search engine: %s", engine.Name)
|
||||||
return nil
|
|
||||||
|
results, err = engine.Func(query, safe, lang, page)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error performing search with %s: %v", engine.Name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(results) > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
Loading…
Add table
Reference in a new issue