From 326ae967bdaec4143ccd4361c34d0ecd58d66c76 Mon Sep 17 00:00:00 2001 From: partisan Date: Sat, 15 Jun 2024 18:12:01 +0200 Subject: [PATCH] added brave search --- run.sh | 8 ++++-- text-brave.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ text.go | 3 ++- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 text-brave.go diff --git a/run.sh b/run.sh index c09b727..da5bcae 100755 --- a/run.sh +++ b/run.sh @@ -1,3 +1,7 @@ -#!/bin/bash +#!/bin/sh -go run main.go common.go search-engine.go init.go open-search.go images.go imageproxy.go images-quant.go images-imgur.go video.go map.go text.go text-searchxng.go text-duckduckgo.go text-librex.go text-google.go cache.go forums.go files.go files-torrentgalaxy.go files-thepiratebay.go agent.go \ No newline at end of file +# Find all .go files in the current directory +GO_FILES=$(find . -name '*.go' -print) + +# Run the Go program +go run $GO_FILES diff --git a/text-brave.go b/text-brave.go new file mode 100644 index 0000000..12330a3 --- /dev/null +++ b/text-brave.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "net/url" + "strings" + "time" + + "github.com/PuerkitoBio/goquery" +) + +// PerformBraveTextSearch performs a text search on Brave and returns the results. +func PerformBraveTextSearch(query, safe, lang string, offset int) ([]TextSearchResult, time.Duration, error) { + startTime := time.Now() // Start the timer + var results []TextSearchResult + + // Build the search URL + searchURL := fmt.Sprintf("https://search.brave.com/search?q=%s&offset=%d", url.QueryEscape(query), offset) + + req, err := http.NewRequest("GET", searchURL, nil) + if err != nil { + return nil, 0, fmt.Errorf("creating request: %v", err) + } + + // Set headers including User-Agent + TextUserAgent, err := GetUserAgent("Text-Search") + if err != nil { + return nil, 0, err + } + req.Header.Set("User-Agent", TextUserAgent) + + // Perform the HTTP request + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return nil, 0, fmt.Errorf("performing request: %v", err) + } + defer resp.Body.Close() + + // Read the response body + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, 0, fmt.Errorf("reading response body: %v", err) + } + + // Parse the response body + doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(body))) + if err != nil { + return nil, 0, fmt.Errorf("parsing response body: %v", err) + } + + // Extract search results + doc.Find(".snippet").Each(func(i int, s *goquery.Selection) { + title := s.Find(".title").Text() + description := s.Find(".snippet-description").Text() + url, exists := s.Find("a").Attr("href") + + // Add to results only if all components are present + if title != "" && description != "" && exists && url != "" { + results = append(results, TextSearchResult{ + Header: title, + URL: url, + Description: description, + }) + } + }) + + duration := time.Since(startTime) // Calculate the duration + + return results, duration, nil +} diff --git a/text.go b/text.go index 31c2f5d..555d9ed 100644 --- a/text.go +++ b/text.go @@ -14,7 +14,8 @@ func init() { textSearchEngines = []SearchEngine{ {Name: "Google", Func: wrapTextSearchFunc(PerformGoogleTextSearch), Weight: 1}, {Name: "LibreX", Func: wrapTextSearchFunc(PerformLibreXTextSearch), Weight: 2}, - {Name: "DuckDuckGo", Func: wrapTextSearchFunc(PerformDuckDuckGoTextSearch), Weight: 3}, // DuckDuckGo timeouts too fast and search results are trash + {Name: "Brave", Func: wrapTextSearchFunc(PerformBraveTextSearch), Weight: 2}, + {Name: "DuckDuckGo", Func: wrapTextSearchFunc(PerformDuckDuckGoTextSearch), Weight: 5}, // DuckDuckGo timeouts too fast and search results are trash // {Name: "SearXNG", Func: wrapTextSearchFunc(PerformSearXNGTextSearch), Weight: 2}, // Uncomment when implemented } }