added search suggestions to all pages
This commit is contained in:
parent
a520488de8
commit
30aa85d321
8 changed files with 50 additions and 22 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue