fixed the issue where Bing image titles were incorrect
This commit is contained in:
parent
e7430e85bc
commit
3861fdc81c
1 changed files with 21 additions and 19 deletions
|
@ -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
|
||||
width, _ := strconv.Atoi(imgTag.AttrOr("width", "0"))
|
||||
height, _ := strconv.Atoi(imgTag.AttrOr("height", "0"))
|
||||
|
@ -62,31 +59,36 @@ func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchRe
|
|||
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{}
|
||||
if err := json.Unmarshal([]byte(metadata), &data); err == nil {
|
||||
mediaURL, ok := data["murl"].(string)
|
||||
if ok {
|
||||
// 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,
|
||||
})
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
// Check if the number of results is one or less
|
||||
// Check if the number of results is zero
|
||||
if len(results) == 0 {
|
||||
return nil, duration, fmt.Errorf("no images found")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue