added image fetching using Bing
This commit is contained in:
parent
735819d072
commit
624d3137e0
2 changed files with 109 additions and 1 deletions
107
images-bing.go
Normal file
107
images-bing.go
Normal file
|
@ -0,0 +1,107 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
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
|
||||
doc.Find(".imgpt").Each(func(i int, s *goquery.Selection) {
|
||||
imgTag := s.Find("img")
|
||||
imgSrc, exists := imgTag.Attr("src")
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
title, _ := imgTag.Attr("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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
results = append(results, ImageSearchResult{
|
||||
Thumbnail: imgSrc,
|
||||
Title: strings.TrimSpace(title),
|
||||
Media: imgSrc,
|
||||
Width: width,
|
||||
Height: height,
|
||||
Source: mediaURL, // Original image URL
|
||||
ThumbProxy: imgSrc,
|
||||
})
|
||||
})
|
||||
|
||||
duration := time.Since(startTime)
|
||||
|
||||
// Check if the number of results is one or less
|
||||
if len(results) <= 1 {
|
||||
return nil, duration, fmt.Errorf("no images found")
|
||||
}
|
||||
|
||||
return results, duration, nil
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
// 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)
|
||||
// }
|
||||
// }
|
|
@ -13,7 +13,8 @@ var imageSearchEngines []SearchEngine
|
|||
func init() {
|
||||
imageSearchEngines = []SearchEngine{
|
||||
{Name: "Qwant", Func: wrapImageSearchFunc(PerformQwantImageSearch), Weight: 1},
|
||||
{Name: "Imgur", Func: wrapImageSearchFunc(PerformImgurImageSearch), Weight: 2},
|
||||
{Name: "Bing", Func: wrapImageSearchFunc(PerformBingImageSearch), Weight: 2}, // Bing sometimes returns with low amount of images, this leads to danamica page loading not working
|
||||
{Name: "Imgur", Func: wrapImageSearchFunc(PerformImgurImageSearch), Weight: 3},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue