From 24c7a0947985ad9191f674b18735d1c2e4f302f9 Mon Sep 17 00:00:00 2001 From: partisan Date: Sat, 11 Jan 2025 22:06:52 +0100 Subject: [PATCH] replaced deprecated 'io/ioutil' with 'io' --- node.go | 7 +++++-- text-brave.go | 11 +++++++++-- text-searchxng.go | 19 ++++++++++++------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/node.go b/node.go index 5fd247a..aac0804 100644 --- a/node.go +++ b/node.go @@ -5,7 +5,7 @@ import ( "crypto/rand" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "time" ) @@ -65,7 +65,10 @@ func sendMessage(serverAddr string, msg Message) error { defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - body, _ := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("failed to read response body: %v", err) + } return fmt.Errorf("server error: %s", body) } diff --git a/text-brave.go b/text-brave.go index 43e752e..b8dc86a 100644 --- a/text-brave.go +++ b/text-brave.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -21,12 +21,14 @@ func PerformBraveTextSearch(query, safe, lang string, offset int) ([]TextSearchR req, err := http.NewRequest("GET", searchURL, nil) if err != nil { + printWarn("Error creating request: %v", err) return nil, 0, fmt.Errorf("creating request: %v", err) } // Set headers including User-Agent TextUserAgent, err := GetUserAgent("Text-Search") if err != nil { + printWarn("Error generating User-Agent: %v", err) return nil, 0, err } req.Header.Set("User-Agent", TextUserAgent) @@ -35,19 +37,22 @@ func PerformBraveTextSearch(query, safe, lang string, offset int) ([]TextSearchR client := &http.Client{} resp, err := client.Do(req) if err != nil { + printWarn("Error performing request: %v", err) return nil, 0, fmt.Errorf("performing request: %v", err) } defer resp.Body.Close() // Read the response body - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { + printWarn("Error reading response body: %v", err) 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 { + printErr("Error parsing response body: %v", err) return nil, 0, fmt.Errorf("parsing response body: %v", err) } @@ -71,8 +76,10 @@ func PerformBraveTextSearch(query, safe, lang string, offset int) ([]TextSearchR // Return an error if no results are found if len(results) == 0 { + printDebug("No results found for query: %s", query) return nil, duration, fmt.Errorf("no results found") } + printDebug("Search completed successfully for query: %s, found %d results", query, len(results)) return results, duration, nil } diff --git a/text-searchxng.go b/text-searchxng.go index 3ed118f..0796730 100644 --- a/text-searchxng.go +++ b/text-searchxng.go @@ -3,8 +3,7 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" - "log" + "io" "net/http" "time" ) @@ -23,7 +22,13 @@ func fetchInstances() ([]Instance, error) { if err != nil { return nil, err } - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") + + XNGUserAgent, err := GetUserAgent("Text-Search-XNG") + if err != nil { + return nil, err + } + + req.Header.Set("User-Agent", XNGUserAgent) resp, err := client.Do(req) if err != nil { @@ -35,7 +40,7 @@ func fetchInstances() ([]Instance, error) { return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -53,20 +58,20 @@ func validateInstance(instance Instance) bool { client := &http.Client{Timeout: 10 * time.Second} req, err := http.NewRequest("GET", fmt.Sprintf("%s/search?q=test&categories=general&language=en&safe_search=1&page=1&format=json", instance.URL), nil) if err != nil { - log.Printf("Error creating request for URL: %s, Error: %v", instance.URL, err) + printWarn("Error creating request for URL: %s, Error: %v", instance.URL, err) return false } req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") resp, err := client.Do(req) if err != nil { - log.Printf("Error performing request for URL: %s, Error: %v", instance.URL, err) + printWarn("Error performing request for URL: %s, Error: %v", instance.URL, err) return false } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - log.Printf("Instance validation failed for URL: %s, StatusCode: %d", instance.URL, resp.StatusCode) + printWarn("Instance validation failed for URL: %s, StatusCode: %d", instance.URL, resp.StatusCode) return false }