2024-08-12 12:56:42 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-21 12:30:03 +02:00
|
|
|
"encoding/json"
|
2024-08-12 12:56:42 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2024-09-12 22:11:39 +02:00
|
|
|
// PerformBingImageSearch performs a Bing image search and returns the results.
|
2024-08-12 12:56:42 +02:00
|
|
|
func PerformBingImageSearch(query, safe, lang string, page int) ([]ImageSearchResult, time.Duration, error) {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
// Build the search URL
|
|
|
|
searchURL := buildBingSearchURL(query, page)
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
resp, err := http.Get(searchURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("making request: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, 0, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the HTML document
|
|
|
|
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("loading HTML document: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract data using goquery
|
|
|
|
var results []ImageSearchResult
|
2024-08-21 12:30:03 +02:00
|
|
|
doc.Find(".iusc").Each(func(i int, s *goquery.Selection) {
|
|
|
|
// Extract image source
|
2024-08-12 12:56:42 +02:00
|
|
|
imgTag := s.Find("img")
|
|
|
|
imgSrc, exists := imgTag.Attr("src")
|
|
|
|
if !exists {
|
2024-08-21 12:30:03 +02:00
|
|
|
imgSrc, exists = imgTag.Attr("data-src")
|
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
2024-08-12 12:56:42 +02:00
|
|
|
}
|
|
|
|
|
2024-08-21 12:30:03 +02:00
|
|
|
// Extract the image title from `alt` attribute
|
|
|
|
title := imgTag.AttrOr("alt", "")
|
2024-08-12 12:56:42 +02:00
|
|
|
|
|
|
|
// Extract width and height if available
|
|
|
|
width, _ := strconv.Atoi(imgTag.AttrOr("width", "0"))
|
|
|
|
height, _ := strconv.Atoi(imgTag.AttrOr("height", "0"))
|
|
|
|
|
2024-08-21 12:30:03 +02:00
|
|
|
// Extract the m parameter (JSON-encoded image metadata)
|
|
|
|
metadata, exists := s.Attr("m")
|
|
|
|
if !exists {
|
|
|
|
return
|
2024-08-12 12:56:42 +02:00
|
|
|
}
|
|
|
|
|
2024-08-21 12:30:03 +02:00
|
|
|
// 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 {
|
2024-09-12 22:11:39 +02:00
|
|
|
// Apply the image proxy
|
|
|
|
proxiedURL := "/imgproxy?url=" + mediaURL
|
2024-08-21 12:30:03 +02:00
|
|
|
results = append(results, ImageSearchResult{
|
|
|
|
Thumbnail: imgSrc,
|
|
|
|
Title: strings.TrimSpace(title),
|
|
|
|
Media: mediaURL,
|
2024-09-12 22:11:39 +02:00
|
|
|
Source: mediaURL,
|
|
|
|
ThumbProxy: proxiedURL, // Use the proxied URL
|
2024-08-21 12:30:03 +02:00
|
|
|
Width: width,
|
|
|
|
Height: height,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 12:56:42 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
duration := time.Since(startTime)
|
|
|
|
|
|
|
|
// Check if the number of results is one or less
|
2024-08-21 12:30:03 +02:00
|
|
|
if len(results) == 0 {
|
2024-08-12 12:56:42 +02:00
|
|
|
return nil, duration, fmt.Errorf("no images found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, duration, nil
|
|
|
|
}
|
|
|
|
|
2024-09-12 22:11:39 +02:00
|
|
|
// buildBingSearchURL constructs the search URL for Bing Image Search
|
2024-08-12 12:56:42 +02:00
|
|
|
func buildBingSearchURL(query string, page int) string {
|
|
|
|
baseURL := "https://www.bing.com/images/search"
|
|
|
|
params := url.Values{}
|
|
|
|
params.Add("q", query)
|
|
|
|
params.Add("first", fmt.Sprintf("%d", (page-1)*35+1)) // Pagination, but increasing it doesn't seem to make a difference
|
|
|
|
params.Add("count", "35")
|
|
|
|
params.Add("form", "HDRSC2")
|
|
|
|
return baseURL + "?" + params.Encode()
|
|
|
|
}
|
|
|
|
|
2024-09-12 22:11:39 +02:00
|
|
|
// Example usage in main (commented out for clarity)
|
2024-08-12 12:56:42 +02:00
|
|
|
// func main() {
|
|
|
|
// results, duration, err := PerformBingImageSearch("kittens", "false", "en", 1)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("Error:", err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fmt.Printf("Search took: %v\n", duration)
|
|
|
|
// fmt.Printf("Total results: %d\n", len(results))
|
|
|
|
// for _, result := range results {
|
|
|
|
// fmt.Printf("Title: %s\nThumbnail: %s\nWidth: %d\nHeight: %d\nThumbProxy: %s\nSource (Original Image URL): %s\n\n",
|
|
|
|
// result.Title, result.Thumbnail, result.Width, result.Height, result.ThumbProxy, result.Source)
|
|
|
|
// }
|
|
|
|
// }
|