2024-04-05 12:44:11 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
2024-04-07 15:48:25 +02:00
|
|
|
var funcs = template.FuncMap{
|
|
|
|
"sub": func(a, b int) int {
|
|
|
|
return a - b
|
|
|
|
},
|
|
|
|
"add": func(a, b int) int {
|
|
|
|
return a + b
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-04-05 12:44:11 +02:00
|
|
|
// FetchImageResults contacts the image search API and returns a slice of ImageSearchResult
|
2024-04-07 15:48:25 +02:00
|
|
|
func fetchImageResults(query string, safe, lang string, page int) ([]ImageSearchResult, error) {
|
|
|
|
const resultsPerPage = 50
|
|
|
|
var offset int
|
|
|
|
if page <= 1 {
|
2024-05-23 10:56:26 +02:00
|
|
|
offset = 0
|
2024-04-07 15:48:25 +02:00
|
|
|
} else {
|
|
|
|
offset = (page - 1) * resultsPerPage
|
|
|
|
}
|
|
|
|
|
2024-05-19 22:57:23 +02:00
|
|
|
// Ensuring safe search is disabled by default if not specified
|
2024-04-07 15:48:25 +02:00
|
|
|
if safe == "" {
|
2024-05-19 22:57:23 +02:00
|
|
|
safe = "0"
|
2024-04-07 15:48:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Defaulting to English Canada locale if not specified
|
|
|
|
if lang == "" {
|
|
|
|
lang = "en_CA"
|
|
|
|
}
|
2024-04-05 12:44:11 +02:00
|
|
|
|
2024-05-19 22:57:23 +02:00
|
|
|
// Format &lang=lang_de is incorrect, implement fix !
|
2024-04-07 15:48:25 +02:00
|
|
|
apiURL := fmt.Sprintf("https://api.qwant.com/v3/search/images?t=images&q=%s&count=%d&locale=%s&offset=%d&device=desktop&tgp=2&safesearch=%s",
|
|
|
|
url.QueryEscape(query),
|
|
|
|
resultsPerPage,
|
|
|
|
lang,
|
|
|
|
offset,
|
|
|
|
safe)
|
|
|
|
|
|
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
2024-04-05 12:44:11 +02:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", apiURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("creating request: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-04-07 15:48:25 +02:00
|
|
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
|
2024-04-05 12:44:11 +02:00
|
|
|
|
|
|
|
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{
|
2024-04-17 15:02:52 +02:00
|
|
|
Thumbnail: item.Thumbnail, // Thumbnail URL
|
|
|
|
Title: item.Title, // Image title
|
|
|
|
Media: item.Media, // Direct link to the image
|
|
|
|
Source: item.Url,
|
|
|
|
ThumbProxy: "/img_proxy?url=" + url.QueryEscape(item.Media),
|
2024-04-05 12:44:11 +02:00
|
|
|
Width: item.Width,
|
|
|
|
Height: item.Height,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleImageSearch is the HTTP handler for image search requests
|
2024-04-07 15:48:25 +02:00
|
|
|
func handleImageSearch(w http.ResponseWriter, query, safe, lang string, page int) {
|
2024-05-24 14:07:16 +02:00
|
|
|
startTime := time.Now()
|
2024-04-05 12:44:11 +02:00
|
|
|
|
2024-05-24 14:07:16 +02:00
|
|
|
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "image"}
|
|
|
|
combinedResults := getImageResultsFromCacheOrFetch(cacheKey, query, safe, lang, page)
|
|
|
|
|
|
|
|
elapsedTime := time.Since(startTime)
|
2024-04-07 15:48:25 +02:00
|
|
|
tmpl, err := template.New("images.html").Funcs(funcs).ParseFiles("templates/images.html")
|
2024-04-05 12:44:11 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error parsing template: %v", err)
|
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := struct {
|
2024-04-08 13:15:23 +02:00
|
|
|
Results []ImageSearchResult
|
|
|
|
Query string
|
|
|
|
Page int
|
|
|
|
Fetched string
|
|
|
|
LanguageOptions []LanguageOption
|
|
|
|
CurrentLang string
|
|
|
|
HasPrevPage bool
|
|
|
|
HasNextPage bool
|
2024-04-05 12:44:11 +02:00
|
|
|
}{
|
2024-05-24 14:07:16 +02:00
|
|
|
Results: combinedResults,
|
2024-04-08 13:15:23 +02:00
|
|
|
Query: query,
|
|
|
|
Page: page,
|
2024-05-24 14:07:16 +02:00
|
|
|
Fetched: fmt.Sprintf("%.2f seconds", elapsedTime.Seconds()),
|
2024-04-08 13:15:23 +02:00
|
|
|
LanguageOptions: languageOptions,
|
|
|
|
CurrentLang: lang,
|
|
|
|
HasPrevPage: page > 1,
|
2024-05-24 14:07:16 +02:00
|
|
|
HasNextPage: len(combinedResults) >= 50,
|
2024-04-05 12:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = tmpl.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error executing template: %v", err)
|
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2024-05-24 14:07:16 +02:00
|
|
|
|
|
|
|
func getImageResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page int) []ImageSearchResult {
|
|
|
|
cacheChan := make(chan []SearchResult)
|
|
|
|
var combinedResults []ImageSearchResult
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
results, exists := resultsCache.Get(cacheKey)
|
|
|
|
if exists {
|
|
|
|
log.Println("Cache hit")
|
|
|
|
cacheChan <- results
|
|
|
|
} else {
|
|
|
|
log.Println("Cache miss")
|
|
|
|
cacheChan <- nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case results := <-cacheChan:
|
|
|
|
if results == nil {
|
|
|
|
combinedResults = fetchAndCacheImageResults(query, safe, lang, page)
|
|
|
|
} else {
|
|
|
|
_, _, imageResults := convertToSpecificResults(results)
|
|
|
|
combinedResults = imageResults
|
|
|
|
}
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
log.Println("Cache check timeout")
|
|
|
|
combinedResults = fetchAndCacheImageResults(query, safe, lang, page)
|
|
|
|
}
|
|
|
|
|
|
|
|
return combinedResults
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchAndCacheImageResults(query, safe, lang string, page int) []ImageSearchResult {
|
|
|
|
results, err := fetchImageResults(query, safe, lang, page)
|
|
|
|
if err != nil || len(results) == 0 {
|
|
|
|
log.Printf("Error fetching image results: %v", err)
|
|
|
|
return []ImageSearchResult{
|
|
|
|
{Title: "Results are currently unavailable, sorry. Please try again later."},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the valid results
|
|
|
|
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "image"}
|
|
|
|
resultsCache.Set(cacheKey, convertToSearchResults(results))
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|