typo fix lol
This commit is contained in:
parent
84a88d6fe6
commit
96dae70aa9
4 changed files with 132 additions and 70 deletions
71
imageresults.go
Normal file
71
imageresults.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue