fixed the issue where Bing image titles were incorrect

This commit is contained in:
partisan 2024-10-31 22:24:23 +01:00
parent e7430e85bc
commit 3861fdc81c

View file

@ -49,9 +49,6 @@ func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchRe
} }
} }
// Extract the image title from `alt` attribute
title := imgTag.AttrOr("alt", "")
// Extract width and height if available // Extract width and height if available
width, _ := strconv.Atoi(imgTag.AttrOr("width", "0")) width, _ := strconv.Atoi(imgTag.AttrOr("width", "0"))
height, _ := strconv.Atoi(imgTag.AttrOr("height", "0")) height, _ := strconv.Atoi(imgTag.AttrOr("height", "0"))
@ -62,11 +59,17 @@ func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchRe
return return
} }
// Parse the metadata to get the media URL (the original image source) // Parse the metadata to get the media URL and title
var data map[string]interface{} var data map[string]interface{}
if err := json.Unmarshal([]byte(metadata), &data); err == nil { if err := json.Unmarshal([]byte(metadata), &data); err == nil {
mediaURL, ok := data["murl"].(string) mediaURL, ok := data["murl"].(string)
if ok { if !ok {
return
}
// Extract title from the metadata
title, _ := data["t"].(string)
// Apply the image proxy // Apply the image proxy
proxiedFullURL := "/imgproxy?url=" + mediaURL proxiedFullURL := "/imgproxy?url=" + mediaURL
proxiedThumbURL := "/imgproxy?url=" + imgSrc proxiedThumbURL := "/imgproxy?url=" + imgSrc
@ -81,12 +84,11 @@ func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchRe
Height: height, Height: height,
}) })
} }
}
}) })
duration := time.Since(startTime) duration := time.Since(startTime)
// Check if the number of results is one or less // Check if the number of results is zero
if len(results) == 0 { if len(results) == 0 {
return nil, duration, fmt.Errorf("no images found") return nil, duration, fmt.Errorf("no images found")
} }