added image search (website style wip)
This commit is contained in:
parent
6f25a28d5c
commit
7ae56f4e21
5 changed files with 207 additions and 114 deletions
116
images.go
Normal file
116
images.go
Normal file
|
@ -0,0 +1,116 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ImageSearchResult represents a single image result
|
||||
type ImageSearchResult struct {
|
||||
Thumbnail string
|
||||
Title string
|
||||
Media string
|
||||
Width int
|
||||
Height int
|
||||
Source string
|
||||
ThumbProxy string
|
||||
}
|
||||
|
||||
// QwantAPIResponse represents the JSON response structure from Qwant API
|
||||
type QwantAPIResponse struct {
|
||||
Data struct {
|
||||
Result struct {
|
||||
Items []struct {
|
||||
Media string `json:"media"`
|
||||
Thumbnail string `json:"thumbnail"`
|
||||
Title string `json:"title"`
|
||||
Url string `json:"url"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
} `json:"items"`
|
||||
} `json:"result"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// FetchImageResults contacts the image search API and returns a slice of ImageSearchResult
|
||||
func fetchImageResults(query string) ([]ImageSearchResult, error) {
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
// Update this URL to the actual API endpoint you intend to use
|
||||
apiURL := 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", apiURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating request: %v", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; YourBot/1.0; +http://yourbot.com/bot.html)")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("making request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var apiResp QwantAPIResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
|
||||
return nil, fmt.Errorf("decoding response: %v", err)
|
||||
}
|
||||
|
||||
var results []ImageSearchResult
|
||||
for _, item := range apiResp.Data.Result.Items {
|
||||
results = append(results, ImageSearchResult{
|
||||
Thumbnail: item.Thumbnail, // Thumbnail URL
|
||||
Title: item.Title, // Image title
|
||||
Media: item.Media, // Direct link to the image - Ensure this field is used appropriately in your template
|
||||
Source: item.Media, // Using item.Media here ensures the direct image link is used
|
||||
ThumbProxy: "/img_proxy?url=" + url.QueryEscape(item.Thumbnail), // Proxy URL for the thumbnail, if needed
|
||||
Width: item.Width,
|
||||
Height: item.Height,
|
||||
})
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// HandleImageSearch is the HTTP handler for image search requests
|
||||
func handleImageSearch(w http.ResponseWriter, r *http.Request, query string) {
|
||||
results, err := fetchImageResults(query)
|
||||
if err != nil {
|
||||
log.Printf("Error performing image search: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles("templates/images.html") // Ensure path is correct
|
||||
if err != nil {
|
||||
log.Printf("Error parsing template: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data := struct {
|
||||
Results []ImageSearchResult
|
||||
Query string
|
||||
Fetched string
|
||||
}{
|
||||
Results: results,
|
||||
Query: query,
|
||||
Fetched: fmt.Sprintf("%.2f seconds", time.Since(time.Now()).Seconds()),
|
||||
}
|
||||
|
||||
err = tmpl.Execute(w, data)
|
||||
if err != nil {
|
||||
log.Printf("Error executing template: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue