Skip to content

Commit

Permalink
Revert "refactor: cleanup"
Browse files Browse the repository at this point in the history
This reverts commit c30fe94.
  • Loading branch information
s0up4200 committed Jan 17, 2025
1 parent c30fe94 commit 700d249
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions tracker/btn.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,21 @@ func (c *BTN) extractTorrentID(comment string) (string, error) {
return matches[1], nil
}

func (c *BTN) IsUnregistered(torrent *Torrent) (bool, error) {
func (c *BTN) IsUnregistered(torrent *Torrent) (error, bool) {
if !strings.EqualFold(torrent.TrackerName, "landof.tv") {
return false, nil
return nil, false
}

if torrent.Comment == "" {
return false, nil
//c.log.Debugf("Skipping torrent check - no comment available: %s", torrent.Name)
return nil, false
}

//c.log.Debugf("Checking torrent from %s: %s", torrent.TrackerName, torrent.Name)

torrentID, err := c.extractTorrentID(torrent.Comment)
if err != nil {
return false, nil
return nil, false
}

type JSONRPCRequest struct {
Expand Down Expand Up @@ -113,15 +116,15 @@ func (c *BTN) IsUnregistered(torrent *Torrent) (bool, error) {

jsonBody, err := json.Marshal(reqBody)
if err != nil {
c.log.WithError(err).Error("failed to marshal request body")
return false, fmt.Errorf("marshal request: %w", err)
c.log.WithError(err).Error("Failed to marshal request body")
return fmt.Errorf("btn: marshal request: %w", err), false
}

// 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 false, fmt.Errorf("create request: %w", err)
c.log.WithError(err).Error("Failed to create request")
return fmt.Errorf("btn: create request: %w", err), false
}

// set headers
Expand All @@ -131,53 +134,54 @@ func (c *BTN) IsUnregistered(torrent *Torrent) (bool, error) {
// 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 false, fmt.Errorf("request check: %w", err)
c.log.WithError(err).Errorf("Failed checking torrent %s (hash: %s)", torrent.Name, torrent.Hash)
return fmt.Errorf("btn: request check: %w", err), false
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
return fmt.Errorf("btn: unexpected status code: %d", resp.StatusCode), false
}

// 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 false, fmt.Errorf("decode response: %w", err)
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
}

// 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.Error("api requires ip authorization - check btn notices")
return false, fmt.Errorf("ip authorization required")
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
}

// default error case
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)
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
}

// check if we got any results
if response.Result.Results == "0" || len(response.Result.Torrents) == 0 {
return true, nil
return nil, true
}

// 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 false, nil
c.log.Debugf("Found matching torrent: %s", t.ReleaseName)
return nil, false
}
}

// 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 true, nil
c.log.Debugf("Torrent ID exists but hash mismatch for: %s", torrent.Name)
return nil, true
}

func (c *BTN) IsTrackerDown(torrent *Torrent) (bool, error) {
return false, nil
func (c *BTN) IsTrackerDown(torrent *Torrent) (error, bool) {
return nil, false
}

0 comments on commit 700d249

Please sign in to comment.