From 6ef817a2b09ecee632f597c427939cc5c9416f8c Mon Sep 17 00:00:00 2001 From: partisan Date: Wed, 21 Aug 2024 23:23:08 +0200 Subject: [PATCH] added search suggestions to all pages --- open-search.go | 6 +++--- suggestions.go | 37 ++++++++++++++++++------------------- templates/files.html | 5 +++++ templates/forums.html | 5 +++++ templates/images.html | 5 +++++ templates/map.html | 4 ++++ templates/text.html | 5 +++++ templates/videos.html | 5 +++++ 8 files changed, 50 insertions(+), 22 deletions(-) diff --git a/open-search.go b/open-search.go index ee3887f..8ae97e1 100644 --- a/open-search.go +++ b/open-search.go @@ -25,9 +25,9 @@ func generateOpenSearchXML(config Config) { opensearch := OpenSearchDescription{ Xmlns: "http://a9.com/-/spec/opensearch/1.1/", - ShortName: "Search Engine", - Description: "Search engine", - Tags: "search, engine", + ShortName: "Warp", + Description: "Warp search engine", + Tags: "search, engine, warp", URLs: []URL{ { Type: "text/html", diff --git a/suggestions.go b/suggestions.go index 3990e11..73e9b02 100644 --- a/suggestions.go +++ b/suggestions.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "log" "net/http" "net/url" ) @@ -32,15 +31,15 @@ func handleSuggestions(w http.ResponseWriter, r *http.Request) { for _, fetchFunc := range suggestionSources { suggestions = fetchFunc(query) if len(suggestions) > 0 { - log.Printf("Suggestions found using %T\n", fetchFunc) + printDebug("Suggestions found using %T", fetchFunc) break } else { - log.Printf("%T did not return any suggestions or failed.\n", fetchFunc) + printWarn("%T did not return any suggestions or failed.", fetchFunc) } } if len(suggestions) == 0 { - log.Println("All suggestion services failed. Returning empty response.") + printErr("All suggestion services failed. Returning empty response.") } // Return the final suggestions as JSON @@ -51,87 +50,87 @@ func handleSuggestions(w http.ResponseWriter, r *http.Request) { func fetchGoogleSuggestions(query string) []string { encodedQuery := url.QueryEscape(query) url := fmt.Sprintf("http://suggestqueries.google.com/complete/search?client=firefox&q=%s", encodedQuery) - log.Println("Fetching suggestions from Google:", url) + printDebug("Fetching suggestions from Google: %s", url) return fetchSuggestionsFromURL(url) } func fetchDuckDuckGoSuggestions(query string) []string { encodedQuery := url.QueryEscape(query) url := fmt.Sprintf("https://duckduckgo.com/ac/?q=%s&type=list", encodedQuery) - log.Println("Fetching suggestions from DuckDuckGo:", url) + printDebug("Fetching suggestions from DuckDuckGo: %s", url) return fetchSuggestionsFromURL(url) } func fetchEdgeSuggestions(query string) []string { encodedQuery := url.QueryEscape(query) url := fmt.Sprintf("https://api.bing.com/osjson.aspx?query=%s", encodedQuery) - log.Println("Fetching suggestions from Edge (Bing):", url) + printDebug("Fetching suggestions from Edge (Bing): %s", url) return fetchSuggestionsFromURL(url) } func fetchBraveSuggestions(query string) []string { encodedQuery := url.QueryEscape(query) url := fmt.Sprintf("https://search.brave.com/api/suggest?q=%s", encodedQuery) - log.Println("Fetching suggestions from Brave:", url) + printDebug("Fetching suggestions from Brave: %s", url) return fetchSuggestionsFromURL(url) } func fetchEcosiaSuggestions(query string) []string { encodedQuery := url.QueryEscape(query) url := fmt.Sprintf("https://ac.ecosia.org/?q=%s&type=list", encodedQuery) - log.Println("Fetching suggestions from Ecosia:", url) + printDebug("Fetching suggestions from Ecosia: %s", url) return fetchSuggestionsFromURL(url) } func fetchQwantSuggestions(query string) []string { encodedQuery := url.QueryEscape(query) url := fmt.Sprintf("https://api.qwant.com/v3/suggest?q=%s", encodedQuery) - log.Println("Fetching suggestions from Qwant:", url) + printDebug("Fetching suggestions from Qwant: %s", url) return fetchSuggestionsFromURL(url) } func fetchStartpageSuggestions(query string) []string { encodedQuery := url.QueryEscape(query) url := fmt.Sprintf("https://startpage.com/suggestions?q=%s", encodedQuery) - log.Println("Fetching suggestions from Startpage:", url) + printDebug("Fetching suggestions from Startpage: %s", url) return fetchSuggestionsFromURL(url) } func fetchSuggestionsFromURL(url string) []string { resp, err := http.Get(url) if err != nil { - log.Println("Error fetching suggestions from", url, ":", err) + printWarn("Error fetching suggestions from %s: %v", url, err) return []string{} } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - log.Println("Error reading response body from", url, ":", err) + printWarn("Error reading response body from %s: %v", url, err) return []string{} } // Log the Content-Type for debugging contentType := resp.Header.Get("Content-Type") - log.Println("Response Content-Type from", url, ":", contentType) + printDebug("Response Content-Type from %s: %s", url, contentType) // Check if the body is non-empty if len(body) == 0 { - log.Println("Received empty response body from", url) + printWarn("Received empty response body from %s", url) return []string{} } // Attempt to parse the response as JSON regardless of Content-Type var parsedResponse []interface{} if err := json.Unmarshal(body, &parsedResponse); err != nil { - log.Println("Error parsing JSON from", url, ":", err) - log.Println("Response body:", string(body)) // Log the body for debugging + printErr("Error parsing JSON from %s: %v", url, err) + printDebug("Response body: %s", string(body)) return []string{} } // Ensure the response structure is as expected if len(parsedResponse) < 2 { - log.Println("Unexpected response format from", url, ":", string(body)) + printWarn("Unexpected response format from %v: %v", url, string(body)) return []string{} } @@ -143,7 +142,7 @@ func fetchSuggestionsFromURL(url string) []string { } } } else { - log.Println("Unexpected suggestions format in response from", url) + printErr("Unexpected suggestions format in response from: %v", url) } return suggestions diff --git a/templates/files.html b/templates/files.html index af39437..8b0edee 100755 --- a/templates/files.html +++ b/templates/files.html @@ -14,6 +14,10 @@
+
+
    +
+
@@ -105,6 +109,7 @@ Try rephrasing your search term and/or recorrect any spelling mistakes.
{{ end }} +