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,31 +59,36 @@ 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 {
// Apply the image proxy return
proxiedFullURL := "/imgproxy?url=" + mediaURL
proxiedThumbURL := "/imgproxy?url=" + imgSrc
results = append(results, ImageSearchResult{
Thumb: imgSrc,
Title: strings.TrimSpace(title),
Full: imgSrc,
Source: mediaURL,
ProxyFull: proxiedFullURL, // Proxied full-size image URL
ProxyThumb: proxiedThumbURL, // Proxied thumbnail URL
Width: width,
Height: height,
})
} }
// Extract title from the metadata
title, _ := data["t"].(string)
// Apply the image proxy
proxiedFullURL := "/imgproxy?url=" + mediaURL
proxiedThumbURL := "/imgproxy?url=" + imgSrc
results = append(results, ImageSearchResult{
Thumb: imgSrc,
Title: strings.TrimSpace(title),
Full: imgSrc,
Source: mediaURL,
ProxyFull: proxiedFullURL, // Proxied full-size image URL
ProxyThumb: proxiedThumbURL, // Proxied thumbnail URL
Width: width,
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")
} }