71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"math/rand"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
var userAgents := []string{
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
|
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0",
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0",
|
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15",
|
|
}
|
|
|
|
type ImageResult struct {
|
|
ThumbnailURL string `json:"thumb_proxy"`
|
|
Source string `json:"source"`
|
|
}
|
|
|
|
func FetchImageResults(query string) ([]ImageResult, error) {
|
|
var results []ImageResult
|
|
|
|
// Random user agent
|
|
randIndex := rand.Intn(len(userAgents))
|
|
userAgent := userAgents[randIndex]
|
|
|
|
client := &http.Client{}
|
|
reqURL := fmt.Sprintf("https://api.qwant.com/v3/search/images?t=images&q=%s&count=50&locale=en_CA&offset=1&device=desktop&tgp=2&safesearch=1", url.QueryEscape(query))
|
|
req, err := http.NewRequest("GET", reqURL, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("User-Agent", userAgent)
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var respData struct {
|
|
Data struct {
|
|
Result struct {
|
|
Items []struct {
|
|
Thumbnail string `json:"thumbnail"`
|
|
URL string `json:"url"`
|
|
} `json:"items"`
|
|
} `json:"result"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&respData); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, item := range respData.Data.Result.Items {
|
|
// Process each image result here
|
|
results = append(results, ImageResult{
|
|
ThumbnailURL: "/img_proxy?url=" + url.QueryEscape(item.Thumbnail),
|
|
Source: url.QueryEscape(urlparse(item.URL).Host), // Ensure you have a urlparse equivalent in Go or process URL differently
|
|
})
|
|
}
|
|
|
|
return results, nil
|
|
}
|