added config values to configure enabled meta-search engines

This commit is contained in:
partisan 2025-02-21 19:47:55 +01:00
parent dc4a3a4bec
commit 5e6fc73038
7 changed files with 184 additions and 88 deletions

View file

@ -3,14 +3,11 @@ package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"time"
)
const LIBREX_DOMAIN = "librex.antopie.org"
type LibreXResult struct {
Title string `json:"title"`
URL string `json:"url"`
@ -20,13 +17,10 @@ type LibreXResult struct {
type LibreXResponse []LibreXResult
func PerformLibreXTextSearch(query, safe, lang string, page int) ([]TextSearchResult, time.Duration, error) {
startTime := time.Now() // Start the timer
startTime := time.Now()
// LibreX/Y uses offset instead of page that starts at 0
page--
page = page * 10
searchURL := fmt.Sprintf("https://%s/api.php?q=%s&p=%d&t=0", LIBREX_DOMAIN, url.QueryEscape(query), page)
// LibreX uses offset instead of page (starting at 0)
pageOffset := (page - 1) * 10
// Generate User-Agent
userAgent, err := GetUserAgent("librex-text-search")
@ -34,58 +28,62 @@ func PerformLibreXTextSearch(query, safe, lang string, page int) ([]TextSearchRe
return nil, 0, err
}
req, err := http.NewRequest("GET", searchURL, nil)
if err != nil {
return nil, 0, err
}
req.Header.Set("User-Agent", userAgent)
var allResults []TextSearchResult
// Perform the request using the appropriate client
var resp *http.Response
if config.MetaProxyEnabled && config.MetaProxyStrict && metaProxyClient != nil {
resp, err = metaProxyClient.Do(req)
} else {
client := &http.Client{}
resp, err = client.Do(req)
}
for _, domain := range config.LibreXInstances {
searchURL := fmt.Sprintf("https://%s/api.php?q=%s&p=%d&t=0",
domain,
url.QueryEscape(query),
pageOffset,
)
if err != nil {
return nil, 0, logError("error making request to LibreX", err)
}
defer resp.Body.Close()
req, err := http.NewRequest("GET", searchURL, nil)
if err != nil {
printWarn("failed to create request for domain %s: %v", domain, err)
continue
}
req.Header.Set("User-Agent", userAgent)
if resp.StatusCode != http.StatusOK {
return nil, 0, logError("unexpected status code", fmt.Errorf("%d", resp.StatusCode))
}
// Respect MetaProxy if enabled and strict
var resp *http.Response
if config.MetaProxyEnabled && config.MetaProxyStrict && metaProxyClient != nil {
resp, err = metaProxyClient.Do(req)
} else {
client := &http.Client{}
resp, err = client.Do(req)
}
if err != nil {
printWarn("error requesting domain %s: %v", domain, err)
continue
}
defer resp.Body.Close()
var librexResp LibreXResponse
if err := json.NewDecoder(resp.Body).Decode(&librexResp); err != nil {
return nil, 0, logError("error decoding LibreX response", err)
}
var results []TextSearchResult
for _, item := range librexResp {
result := TextSearchResult{
URL: item.URL,
Header: item.Title,
Description: item.Description,
Source: "LibreX",
if resp.StatusCode != http.StatusOK {
printWarn("unexpected status code from %s: %d", domain, resp.StatusCode)
continue
}
results = append(results, result)
var librexResp LibreXResponse
if err := json.NewDecoder(resp.Body).Decode(&librexResp); err != nil {
printWarn("error decoding response from %s: %v", domain, err)
continue
}
// Accumulate results from this instance
for _, item := range librexResp {
allResults = append(allResults, TextSearchResult{
URL: item.URL,
Header: item.Title,
Description: item.Description,
Source: "LibreX",
})
}
}
duration := time.Since(startTime) // Calculate the duration
if len(results) == 0 {
return nil, duration, fmt.Errorf("no results found")
duration := time.Since(startTime)
if len(allResults) == 0 {
return nil, duration, fmt.Errorf("no results found from any LibreX instance")
}
return results, duration, nil
}
// This is just stupid it will probably lead to printing error twice
func logError(message string, err error) error {
log.Printf("%s: %v", message, err)
return fmt.Errorf("%s: %w", message, err)
return allResults, duration, nil
}