automatic reputation for search engines
This commit is contained in:
parent
dd9ed4cc53
commit
e3d568f6cb
9 changed files with 198 additions and 126 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
const LIBREX_DOMAIN = "librex.antopie.org"
|
||||
|
@ -18,7 +19,8 @@ type LibreXResult struct {
|
|||
|
||||
type LibreXResponse []LibreXResult
|
||||
|
||||
func PerformLibreXTextSearch(query, safe, lang string, page int) ([]TextSearchResult, error) {
|
||||
func PerformLibreXTextSearch(query, safe, lang string, page int) ([]TextSearchResult, time.Duration, error) {
|
||||
startTime := time.Now() // Start the timer
|
||||
|
||||
// LibreX/Y uses offset instead of page that starts at 0
|
||||
page--
|
||||
|
@ -29,7 +31,7 @@ func PerformLibreXTextSearch(query, safe, lang string, page int) ([]TextSearchRe
|
|||
// User Agent generation
|
||||
userAgent, err := GetUserAgent("librex-text-search")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if debugMode {
|
||||
|
@ -38,24 +40,24 @@ func PerformLibreXTextSearch(query, safe, lang string, page int) ([]TextSearchRe
|
|||
|
||||
req, err := http.NewRequest("GET", searchURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, logError("error making request to LibreX", err)
|
||||
return nil, 0, logError("error making request to LibreX", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, logError("unexpected status code", fmt.Errorf("%d", resp.StatusCode))
|
||||
return nil, 0, logError("unexpected status code", fmt.Errorf("%d", resp.StatusCode))
|
||||
}
|
||||
|
||||
var librexResp LibreXResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&librexResp); err != nil {
|
||||
return nil, logError("error decoding LibreX response", err)
|
||||
return nil, 0, logError("error decoding LibreX response", err)
|
||||
}
|
||||
|
||||
var results []TextSearchResult
|
||||
|
@ -70,7 +72,9 @@ func PerformLibreXTextSearch(query, safe, lang string, page int) ([]TextSearchRe
|
|||
results = append(results, result)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
duration := time.Since(startTime) // Calculate the duration
|
||||
|
||||
return results, duration, nil
|
||||
}
|
||||
|
||||
func logError(message string, err error) error {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue