Compare commits
No commits in common. "afe420a0ed823b7f633b894ec135964f9cb68824" and "a5622b9099c6baaa29d0b16e5c343c1cf8001b45" have entirely different histories.
afe420a0ed
...
a5622b9099
1 changed files with 5 additions and 64 deletions
|
@ -41,19 +41,14 @@ var suggestionSources = []SuggestionSource{
|
||||||
FetchFunc: fetchEcosiaSuggestions,
|
FetchFunc: fetchEcosiaSuggestions,
|
||||||
Latency: 50 * time.Millisecond,
|
Latency: 50 * time.Millisecond,
|
||||||
},
|
},
|
||||||
// { // Not working with fetchSuggestionsFromURL func
|
|
||||||
// Name: "Qwant",
|
|
||||||
// FetchFunc: fetchQwantSuggestions,
|
|
||||||
// Latency: 50 * time.Millisecond,
|
|
||||||
// },
|
|
||||||
{
|
{
|
||||||
Name: "Startpage",
|
Name: "Qwant",
|
||||||
FetchFunc: fetchStartpageSuggestions,
|
FetchFunc: fetchQwantSuggestions,
|
||||||
Latency: 50 * time.Millisecond,
|
Latency: 50 * time.Millisecond,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Yahoo",
|
Name: "Startpage",
|
||||||
FetchFunc: fetchYahooSuggestions,
|
FetchFunc: fetchStartpageSuggestions,
|
||||||
Latency: 50 * time.Millisecond,
|
Latency: 50 * time.Millisecond,
|
||||||
},
|
},
|
||||||
// I advise against it, but you can use it if you want to
|
// I advise against it, but you can use it if you want to
|
||||||
|
@ -99,9 +94,6 @@ func handleSuggestions(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trim the suggestions to a maximum of 8 items
|
|
||||||
suggestions = trimSuggestions(suggestions)
|
|
||||||
|
|
||||||
if len(suggestions) == 0 {
|
if len(suggestions) == 0 {
|
||||||
printErr("All suggestion services failed. Returning empty response.")
|
printErr("All suggestion services failed. Returning empty response.")
|
||||||
}
|
}
|
||||||
|
@ -111,14 +103,6 @@ func handleSuggestions(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(w, `["",%s]`, toJSONStringArray(suggestions))
|
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.
|
// updateLatency updates the latency of a suggestion source using an exponential moving average.
|
||||||
func updateLatency(source *SuggestionSource, newLatency time.Duration) {
|
func updateLatency(source *SuggestionSource, newLatency time.Duration) {
|
||||||
source.mu.Lock()
|
source.mu.Lock()
|
||||||
|
@ -177,20 +161,7 @@ func fetchStartpageSuggestions(query string) []string {
|
||||||
return fetchSuggestionsFromURL(url)
|
return fetchSuggestionsFromURL(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchYahooSuggestions(query string) []string {
|
// fetchSuggestionsFromURL fetches suggestions from the given URL.
|
||||||
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 {
|
func fetchSuggestionsFromURL(url string) []string {
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -205,9 +176,6 @@ func fetchSuggestionsFromURL(url string) []string {
|
||||||
return []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.
|
// Log the Content-Type for debugging.
|
||||||
contentType := resp.Header.Get("Content-Type")
|
contentType := resp.Header.Get("Content-Type")
|
||||||
printDebug("Response Content-Type from %s: %s", url, contentType)
|
printDebug("Response Content-Type from %s: %s", url, contentType)
|
||||||
|
@ -257,30 +225,3 @@ func toJSONStringArray(strings []string) string {
|
||||||
}
|
}
|
||||||
return "[" + result + "]"
|
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)
|
|
||||||
// }
|
|
||||||
|
|
Loading…
Reference in a new issue