i need better way to cache images

This commit is contained in:
partisan 2024-09-12 22:11:39 +02:00
parent 61af6fc453
commit d107d41d72
8 changed files with 59 additions and 33 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/PuerkitoBio/goquery"
)
// PerformBingImageSearch performs a Bing image search and returns the results.
func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchResult, time.Duration, error) {
startTime := time.Now()
@ -66,14 +67,16 @@ func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchRe
if err := json.Unmarshal([]byte(metadata), &data); err == nil {
mediaURL, ok := data["murl"].(string)
if ok {
// Apply the image proxy
proxiedURL := "/imgproxy?url=" + mediaURL
results = append(results, ImageSearchResult{
Thumbnail: imgSrc,
Title: strings.TrimSpace(title),
Media: mediaURL,
Source: mediaURL,
ThumbProxy: proxiedURL, // Use the proxied URL
Width: width,
Height: height,
Source: mediaURL,
ThumbProxy: imgSrc,
})
}
}
@ -89,6 +92,7 @@ func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchRe
return results, duration, nil
}
// buildBingSearchURL constructs the search URL for Bing Image Search
func buildBingSearchURL(query string, page int) string {
baseURL := "https://www.bing.com/images/search"
params := url.Values{}
@ -99,6 +103,7 @@ func buildBingSearchURL(query string, page int) string {
return baseURL + "?" + params.Encode()
}
// Example usage in main (commented out for clarity)
// func main() {
// results, duration, err := PerformBingImageSearch("kittens", "false", "en", 1)
// if err != nil {