quant+google+duckdcuckgo results
This commit is contained in:
parent
c8cf762222
commit
a3a7c69679
6 changed files with 187 additions and 40 deletions
103
text.go
103
text.go
|
@ -1,12 +1,13 @@
|
|||
// text.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -14,49 +15,101 @@ type TextSearchResult struct {
|
|||
URL string
|
||||
Header string
|
||||
Description string
|
||||
Source string
|
||||
}
|
||||
|
||||
func handleTextSearch(w http.ResponseWriter, query, safe, lang string) {
|
||||
googleResults, googleErr := PerformGoogleTextSearch(query, safe, lang)
|
||||
if googleErr != nil {
|
||||
log.Printf("Error performing Google text search: %v", googleErr)
|
||||
var debugMode bool
|
||||
|
||||
func init() {
|
||||
flag.BoolVar(&debugMode, "debug", false, "enable debug mode")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func HandleTextSearch(w http.ResponseWriter, query, safe, lang string) {
|
||||
startTime := time.Now()
|
||||
var combinedResults []TextSearchResult
|
||||
var resultMap = make(map[string]TextSearchResult)
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
|
||||
resultsChan := make(chan []TextSearchResult)
|
||||
|
||||
searchFuncs := []struct {
|
||||
Func func(string, string, string) ([]TextSearchResult, error)
|
||||
Source string
|
||||
}{
|
||||
{PerformGoogleTextSearch, "Google"},
|
||||
{PerformDuckDuckGoTextSearch, "DuckDuckGo"},
|
||||
{PerformQwantTextSearch, "Qwant"},
|
||||
}
|
||||
|
||||
qwantResults, qwantErr := PerformQwantTextSearch(query, safe, lang)
|
||||
if qwantErr != nil {
|
||||
log.Printf("Error performing Qwant text search: %v", qwantErr)
|
||||
wg.Add(len(searchFuncs))
|
||||
|
||||
for _, searchFunc := range searchFuncs {
|
||||
go func(searchFunc func(string, string, string) ([]TextSearchResult, error), source string) {
|
||||
defer wg.Done()
|
||||
results, err := searchFunc(query, safe, lang)
|
||||
if err == nil {
|
||||
for i := range results {
|
||||
results[i].Source = source
|
||||
}
|
||||
resultsChan <- results
|
||||
} else {
|
||||
log.Printf("Error performing search from %s: %v", source, err)
|
||||
}
|
||||
}(searchFunc.Func, searchFunc.Source)
|
||||
}
|
||||
|
||||
// Use a map to track URLs and prioritize Qwant results
|
||||
resultMap := make(map[string]TextSearchResult)
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(resultsChan)
|
||||
}()
|
||||
|
||||
// Add Qwant results to the map first
|
||||
for _, result := range qwantResults {
|
||||
resultMap[result.URL] = result
|
||||
}
|
||||
|
||||
// Add Google results to the map if the URL is not already present
|
||||
for _, result := range googleResults {
|
||||
if _, exists := resultMap[result.URL]; !exists {
|
||||
resultMap[result.URL] = result
|
||||
for results := range resultsChan {
|
||||
mu.Lock()
|
||||
for _, result := range results {
|
||||
existingResult, exists := resultMap[result.URL]
|
||||
if !exists || shouldReplace(existingResult.Source, result.Source) {
|
||||
resultMap[result.URL] = result
|
||||
}
|
||||
if debugMode {
|
||||
log.Printf("Result from %s: %+v\n", result.Source, result)
|
||||
}
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
// Convert the map back to a slice
|
||||
var combinedResults []TextSearchResult
|
||||
for _, result := range resultMap {
|
||||
combinedResults = append(combinedResults, result)
|
||||
}
|
||||
|
||||
// Sort results (optional, based on some criteria)
|
||||
// Custom sorting: Google first, DuckDuckGo second, Qwant third
|
||||
sort.SliceStable(combinedResults, func(i, j int) bool {
|
||||
return combinedResults[i].Header < combinedResults[j].Header
|
||||
return sourceOrder(combinedResults[i].Source) < sourceOrder(combinedResults[j].Source)
|
||||
})
|
||||
|
||||
displayResults(w, combinedResults, query, lang)
|
||||
displayResults(w, combinedResults, query, lang, time.Since(startTime).Seconds())
|
||||
}
|
||||
|
||||
func displayResults(w http.ResponseWriter, results []TextSearchResult, query, lang string) {
|
||||
func shouldReplace(existingSource, newSource string) bool {
|
||||
return sourceOrder(newSource) < sourceOrder(existingSource)
|
||||
}
|
||||
|
||||
func sourceOrder(source string) int {
|
||||
switch source {
|
||||
case "Qwant":
|
||||
return 1
|
||||
case "DuckDuckGo":
|
||||
return 2
|
||||
case "Google":
|
||||
return 3
|
||||
default:
|
||||
return 4
|
||||
}
|
||||
}
|
||||
|
||||
func displayResults(w http.ResponseWriter, results []TextSearchResult, query, lang string, elapsed float64) {
|
||||
tmpl, err := template.ParseFiles("templates/text.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
|
@ -72,7 +125,7 @@ func displayResults(w http.ResponseWriter, results []TextSearchResult, query, la
|
|||
}{
|
||||
Results: results,
|
||||
Query: query,
|
||||
Fetched: fmt.Sprintf("%.2f seconds", time.Since(time.Now()).Seconds()),
|
||||
Fetched: fmt.Sprintf("%.2f seconds", elapsed),
|
||||
LanguageOptions: languageOptions,
|
||||
CurrentLang: lang,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue