Compare commits

..

2 commits

Author SHA1 Message Date
partisan
afe420a0ed trim search suggestions if there is too many 2024-09-25 18:26:27 +02:00
partisan
2679b04284 added Yahoo to search suggestions 2024-09-25 18:21:06 +02:00

View file

@ -41,16 +41,21 @@ var suggestionSources = []SuggestionSource{
FetchFunc: fetchEcosiaSuggestions,
Latency: 50 * time.Millisecond,
},
{
Name: "Qwant",
FetchFunc: fetchQwantSuggestions,
Latency: 50 * time.Millisecond,
},
// { // Not working with fetchSuggestionsFromURL func
// Name: "Qwant",
// FetchFunc: fetchQwantSuggestions,
// Latency: 50 * time.Millisecond,
// },
{
Name: "Startpage",
FetchFunc: fetchStartpageSuggestions,
Latency: 50 * time.Millisecond,
},
{
Name: "Yahoo",
FetchFunc: fetchYahooSuggestions,
Latency: 50 * time.Millisecond,
},
// I advise against it, but you can use it if you want to
// {
// Name: "Google",
@ -94,6 +99,9 @@ func handleSuggestions(w http.ResponseWriter, r *http.Request) {
}
}
// Trim the suggestions to a maximum of 8 items
suggestions = trimSuggestions(suggestions)
if len(suggestions) == 0 {
printErr("All suggestion services failed. Returning empty response.")
}
@ -103,6 +111,14 @@ func handleSuggestions(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `["",%s]`, toJSONStringArray(suggestions))
}
// trimSuggestions trims the suggestion list to a maximum of 8 suggestions.
func trimSuggestions(suggestions []string) []string {
if len(suggestions) > 8 {
return suggestions[:8]
}
return suggestions
}
// updateLatency updates the latency of a suggestion source using an exponential moving average.
func updateLatency(source *SuggestionSource, newLatency time.Duration) {
source.mu.Lock()
@ -161,7 +177,20 @@ func fetchStartpageSuggestions(query string) []string {
return fetchSuggestionsFromURL(url)
}
// fetchSuggestionsFromURL fetches suggestions from the given URL.
func fetchYahooSuggestions(query string) []string {
encodedQuery := url.QueryEscape(query)
url := fmt.Sprintf("https://search.yahoo.com/sugg/gossip/gossip-us-ura/?output=fxjson&command=%s", encodedQuery)
printDebug("Fetching suggestions from Yahoo: %s", url)
return fetchSuggestionsFromURL(url)
}
// func fetchBaiduSuggestions(query string) []string {
// encodedQuery := url.QueryEscape(query)
// url := fmt.Sprintf("https://suggestion.baidu.com/su?wd=%s", encodedQuery)
// printDebug("Fetching suggestions from Baidu: %s", url)
// return fetchSuggestionsFromURL(url)
// }
func fetchSuggestionsFromURL(url string) []string {
resp, err := http.Get(url)
if err != nil {
@ -176,6 +205,9 @@ func fetchSuggestionsFromURL(url string) []string {
return []string{}
}
// Print the raw HTTP response for debugging
fmt.Printf("Raw response from %s:\n%s\n", url, string(body))
// Log the Content-Type for debugging.
contentType := resp.Header.Get("Content-Type")
printDebug("Response Content-Type from %s: %s", url, contentType)
@ -225,3 +257,30 @@ func toJSONStringArray(strings []string) string {
}
return "[" + result + "]"
}
// func testSuggestionSources(query string) {
// for _, source := range suggestionSources {
// fmt.Printf("Testing %s...\n", source.Name)
// // Fetch suggestions
// suggestions := source.FetchFunc(query)
// // If we get results, print them
// if len(suggestions) > 0 {
// fmt.Printf("Suggestions from %s:\n", source.Name)
// for i, suggestion := range suggestions {
// fmt.Printf("%d: %s\n", i+1, suggestion)
// }
// } else {
// fmt.Printf("No suggestions from %s.\n", source.Name)
// }
// // Small separator for clarity
// fmt.Println("--------------------------")
// }
// }
// func main() {
// query := "test query"
// testSuggestionSources(query)
// }