diff --git a/images-bing.go b/images-bing.go index a9f8717..d6e37c5 100644 --- a/images-bing.go +++ b/images-bing.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "net/http" "net/url" @@ -36,45 +37,52 @@ func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchRe // Extract data using goquery var results []ImageSearchResult - doc.Find(".imgpt").Each(func(i int, s *goquery.Selection) { + doc.Find(".iusc").Each(func(i int, s *goquery.Selection) { + // Extract image source imgTag := s.Find("img") imgSrc, exists := imgTag.Attr("src") if !exists { - return + imgSrc, exists = imgTag.Attr("data-src") + if !exists { + return + } } - title, _ := imgTag.Attr("alt") + // 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")) - // Extract the original image URL from the `mediaurl` parameter in the link - pageLink, exists := s.Find("a.iusc").Attr("href") - mediaURL := "" - if exists { - if u, err := url.Parse(pageLink); err == nil { - if mediaURLParam := u.Query().Get("mediaurl"); mediaURLParam != "" { - mediaURL, _ = url.QueryUnescape(mediaURLParam) - } - } + // Extract the m parameter (JSON-encoded image metadata) + metadata, exists := s.Attr("m") + if !exists { + return } - results = append(results, ImageSearchResult{ - Thumbnail: imgSrc, - Title: strings.TrimSpace(title), - Media: imgSrc, - Width: width, - Height: height, - Source: mediaURL, // Original image URL - ThumbProxy: imgSrc, - }) + // Parse the metadata to get the media URL (the original image source) + var data map[string]interface{} + if err := json.Unmarshal([]byte(metadata), &data); err == nil { + mediaURL, ok := data["murl"].(string) + if ok { + results = append(results, ImageSearchResult{ + Thumbnail: imgSrc, + Title: strings.TrimSpace(title), + Media: mediaURL, + Width: width, + Height: height, + Source: mediaURL, + ThumbProxy: imgSrc, + }) + } + } }) duration := time.Since(startTime) // Check if the number of results is one or less - if len(results) <= 1 { + if len(results) == 0 { return nil, duration, fmt.Errorf("no images found") }