From c30fe94d9ab284ea59b828b925435b12621fd813 Mon Sep 17 00:00:00 2001 From: s0up4200 Date: Fri, 17 Jan 2025 13:03:50 +0100 Subject: [PATCH] refactor: cleanup --- tracker/btn.go | 52 +++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/tracker/btn.go b/tracker/btn.go index 51d070c..6dc3d32 100644 --- a/tracker/btn.go +++ b/tracker/btn.go @@ -62,21 +62,18 @@ func (c *BTN) extractTorrentID(comment string) (string, error) { return matches[1], nil } -func (c *BTN) IsUnregistered(torrent *Torrent) (error, bool) { +func (c *BTN) IsUnregistered(torrent *Torrent) (bool, error) { if !strings.EqualFold(torrent.TrackerName, "landof.tv") { - return nil, false + return false, nil } if torrent.Comment == "" { - //c.log.Debugf("Skipping torrent check - no comment available: %s", torrent.Name) - return nil, false + return false, nil } - //c.log.Debugf("Checking torrent from %s: %s", torrent.TrackerName, torrent.Name) - torrentID, err := c.extractTorrentID(torrent.Comment) if err != nil { - return nil, false + return false, nil } type JSONRPCRequest struct { @@ -116,15 +113,15 @@ func (c *BTN) IsUnregistered(torrent *Torrent) (error, bool) { jsonBody, err := json.Marshal(reqBody) if err != nil { - c.log.WithError(err).Error("Failed to marshal request body") - return fmt.Errorf("btn: marshal request: %w", err), false + c.log.WithError(err).Error("failed to marshal request body") + return false, fmt.Errorf("marshal request: %w", err) } // create request req, err := http.NewRequest(http.MethodPost, "https://api.broadcasthe.net", bytes.NewReader(jsonBody)) if err != nil { - c.log.WithError(err).Error("Failed to create request") - return fmt.Errorf("btn: create request: %w", err), false + c.log.WithError(err).Error("failed to create request") + return false, fmt.Errorf("create request: %w", err) } // set headers @@ -134,54 +131,53 @@ func (c *BTN) IsUnregistered(torrent *Torrent) (error, bool) { // send request resp, err := c.http.Do(req) if err != nil { - c.log.WithError(err).Errorf("Failed checking torrent %s (hash: %s)", torrent.Name, torrent.Hash) - return fmt.Errorf("btn: request check: %w", err), false + c.log.WithError(err).Errorf("failed checking torrent %s (hash: %s)", torrent.Name, torrent.Hash) + return false, fmt.Errorf("request check: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return fmt.Errorf("btn: unexpected status code: %d", resp.StatusCode), false + return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode) } // decode response var response JSONRPCResponse if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { - c.log.WithError(err).Errorf("Failed decoding response for %s (hash: %s)", - torrent.Name, torrent.Hash) - return fmt.Errorf("btn: decode response: %w", err), false + c.log.WithError(err).Errorf("failed decoding response for %s (hash: %s)", torrent.Name, torrent.Hash) + return false, fmt.Errorf("decode response: %w", err) } // check for RPC error if response.Error != nil { // check message content for IP authorization if strings.Contains(strings.ToLower(response.Error.Message), "ip address needs authorization") { - c.log.Errorf("BTN API requires IP authorization. Please check your notices on BTN") - return fmt.Errorf("btn: IP authorization required - check BTN notices"), false + c.log.Error("api requires ip authorization - check btn notices") + return false, fmt.Errorf("ip authorization required") } // default error case - c.log.WithError(fmt.Errorf("%s", response.Error.Message)).Errorf("API error (code: %d)", response.Error.Code) - return fmt.Errorf("btn: api error: %s (code: %d)", response.Error.Message, response.Error.Code), false + c.log.WithError(fmt.Errorf(response.Error.Message)).Errorf("api error (code: %d)", response.Error.Code) + return false, fmt.Errorf("api error: %s (code: %d)", response.Error.Message, response.Error.Code) } // check if we got any results if response.Result.Results == "0" || len(response.Result.Torrents) == 0 { - return nil, true + return true, nil } // compare infohash for _, t := range response.Result.Torrents { if strings.EqualFold(t.InfoHash, torrent.Hash) { - c.log.Debugf("Found matching torrent: %s", t.ReleaseName) - return nil, false + c.log.Debugf("found matching torrent: %s", t.ReleaseName) + return false, nil } } // if we get here, the torrent ID exists but hash doesn't match - c.log.Debugf("Torrent ID exists but hash mismatch for: %s", torrent.Name) - return nil, true + c.log.Debugf("torrent id exists but hash mismatch for: %s", torrent.Name) + return true, nil } -func (c *BTN) IsTrackerDown(torrent *Torrent) (error, bool) { - return nil, false +func (c *BTN) IsTrackerDown(torrent *Torrent) (bool, error) { + return false, nil }