code cleanup & fixed compatibility for non-JS users & fixed fullscreen images being in low resolution

This commit is contained in:
partisan 2024-11-19 10:36:33 +01:00
parent 0d083f53e7
commit db89f9c781
13 changed files with 474 additions and 300 deletions

View file

@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
@ -39,27 +38,13 @@ func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchRe
// Extract data using goquery
var results []ImageSearchResult
doc.Find(".iusc").Each(func(i int, s *goquery.Selection) {
// Extract image source
imgTag := s.Find("img")
imgSrc, exists := imgTag.Attr("src")
if !exists {
imgSrc, exists = imgTag.Attr("data-src")
if !exists {
return
}
}
// Extract width and height if available
width, _ := strconv.Atoi(imgTag.AttrOr("width", "0"))
height, _ := strconv.Atoi(imgTag.AttrOr("height", "0"))
// Extract the m parameter (JSON-encoded image metadata)
metadata, exists := s.Attr("m")
if !exists {
return
}
// Parse the metadata to get the media URL and title
// Parse the metadata to get the direct image URL and title
var data map[string]interface{}
if err := json.Unmarshal([]byte(metadata), &data); err == nil {
mediaURL, ok := data["murl"].(string)
@ -67,21 +52,45 @@ func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchRe
return
}
imgURL, ok := data["imgurl"].(string)
if !ok {
imgURL = mediaURL // Fallback to mediaURL if imgurl is not available
}
// Use imgURL as the direct image URL
directImageURL := imgURL
// Extract title from the metadata
title, _ := data["t"].(string)
// Apply the image proxy
proxiedFullURL := "/imgproxy?url=" + mediaURL
proxiedThumbURL := "/imgproxy?url=" + imgSrc
// Extract dimensions if available
width := 0
height := 0
if ow, ok := data["ow"].(float64); ok {
width = int(ow)
}
if oh, ok := data["oh"].(float64); ok {
height = int(oh)
}
// Extract thumbnail URL from the 'turl' field
thumbURL, _ := data["turl"].(string)
if thumbURL == "" {
// As a fallback, try to get it from the 'src' or 'data-src' attributes
imgTag := s.Find("img")
thumbURL, exists = imgTag.Attr("src")
if !exists {
thumbURL, _ = imgTag.Attr("data-src")
}
}
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,
Thumb: thumbURL,
Title: strings.TrimSpace(title),
Full: directImageURL,
Source: mediaURL,
Width: width,
Height: height,
})
}
})