From 6fe3685f920e3e8d5e286f9ac3372891f4cfb02d Mon Sep 17 00:00:00 2001 From: partisan Date: Sun, 7 Apr 2024 15:48:25 +0200 Subject: [PATCH] added "next page" button --- images.go | 68 ++++++++++++++++++++++++++++++++---------- main.go | 10 ++++++- static/search.html | 4 +-- templates/images.html | 19 +++++++----- templates/results.html | 2 +- 5 files changed, 77 insertions(+), 26 deletions(-) diff --git a/images.go b/images.go index a1d2e1a..71b838b 100644 --- a/images.go +++ b/images.go @@ -37,19 +37,50 @@ type QwantAPIResponse struct { } `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} +var funcs = template.FuncMap{ + "sub": func(a, b int) int { + return a - b + }, + "add": func(a, b int) int { + return a + b + }, +} - // 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)) +// FetchImageResults contacts the image search API and returns a slice of ImageSearchResult +func fetchImageResults(query string, safe, lang string, page int) ([]ImageSearchResult, error) { + const resultsPerPage = 50 + var offset int + if page <= 1 { + offset = 0 // Assuming the API expects offset to start from 0 for the first page + } else { + offset = (page - 1) * resultsPerPage + } + + // Ensuring safe search is enabled by default if not specified + if safe == "" { + safe = "1" + } + + // Defaulting to English Canada locale if not specified + if lang == "" { + lang = "en_CA" + } + + 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} 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)") + 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") resp, err := client.Do(req) if err != nil { @@ -83,15 +114,16 @@ func fetchImageResults(query string) ([]ImageSearchResult, error) { } // HandleImageSearch is the HTTP handler for image search requests -func handleImageSearch(w http.ResponseWriter, r *http.Request, query string) { - results, err := fetchImageResults(query) +func handleImageSearch(w http.ResponseWriter, query, safe, lang string, page int) { + results, err := fetchImageResults(query, safe, lang, page) 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 + // Parsing the template file with the custom function map + tmpl, err := template.New("images.html").Funcs(funcs).ParseFiles("templates/images.html") if err != nil { log.Printf("Error parsing template: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) @@ -99,13 +131,19 @@ func handleImageSearch(w http.ResponseWriter, r *http.Request, query string) { } data := struct { - Results []ImageSearchResult - Query string - Fetched string + Results []ImageSearchResult + Query string + Page int + Fetched string + HasPrevPage bool + HasNextPage bool }{ - Results: results, - Query: query, - Fetched: fmt.Sprintf("%.2f seconds", time.Since(time.Now()).Seconds()), + Results: results, + Query: query, + Page: page, + Fetched: fmt.Sprintf("%.2f seconds", time.Since(time.Now()).Seconds()), + HasPrevPage: page > 1, + HasNextPage: len(results) >= 50, } err = tmpl.Execute(w, data) diff --git a/main.go b/main.go index 731be03..fd29099 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/http" + "strconv" ) // LanguageOption represents a language option for search @@ -80,12 +81,19 @@ func main() { func handleSearch(w http.ResponseWriter, r *http.Request) { var query, safe, lang, searchType string + var page int if r.Method == "GET" { query = r.URL.Query().Get("q") safe = r.URL.Query().Get("safe") lang = r.URL.Query().Get("lang") searchType = r.URL.Query().Get("t") + pageStr := r.URL.Query().Get("p") + var err error + page, err = strconv.Atoi(pageStr) + if err != nil || page < 1 { + page = 1 // Default to page 1 if no valid page is specified + } } else if r.Method == "POST" { query = r.FormValue("q") safe = r.FormValue("safe") @@ -102,7 +110,7 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { case "text": handleTextSearch(w, r, query, safe, lang) // Handles fetching and rendering text search results case "image": - handleImageSearch(w, r, query) // Handles fetching and rendering image search results + handleImageSearch(w, query, safe, lang, page) // Adjusted: Pass *http.Request to match the function signature default: http.ServeFile(w, r, "static/search.html") } diff --git a/static/search.html b/static/search.html index 1524046..acd3057 100644 --- a/static/search.html +++ b/static/search.html @@ -3,7 +3,7 @@ - Search with TailsGo + Search with Ocásek @@ -11,7 +11,7 @@
-

TailsGo

+

Ocásek