Skip to content

Commit

Permalink
refactor(btn): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
s0up4200 committed Jan 17, 2025
1 parent 700d249 commit 5fe6473
Showing 1 changed file with 11 additions and 37 deletions.
48 changes: 11 additions & 37 deletions tracker/btn.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@ type BTNConfig struct {
}

type BTN struct {
cfg BTNConfig
http *http.Client
headers map[string]string
log *logrus.Entry
cfg BTNConfig
http *http.Client
log *logrus.Entry
}

func NewBTN(c BTNConfig) *BTN {
l := logger.GetLogger("btn-api")
return &BTN{
cfg: c,
http: httputils.NewRetryableHttpClient(15*time.Second, ratelimit.New(1, ratelimit.WithoutSlack), l),
headers: map[string]string{
"Accept": "application/json",
},
log: l,
log: l,
}
}

Expand Down Expand Up @@ -63,17 +59,10 @@ func (c *BTN) extractTorrentID(comment string) (string, error) {
}

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

if torrent.Comment == "" {
//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 nil, false
Expand All @@ -91,15 +80,13 @@ func (c *BTN) IsUnregistered(torrent *Torrent) (error, bool) {
ReleaseName string `json:"ReleaseName"`
}

type TorrentsResponse struct {
Results string `json:"results"`
Torrents map[string]TorrentInfo `json:"torrents"`
}

type JSONRPCResponse struct {
JsonRPC string `json:"jsonrpc"`
Result TorrentsResponse `json:"result"`
Error *struct {
JsonRPC string `json:"jsonrpc"`
Result struct {
Results string `json:"results"`
Torrents map[string]TorrentInfo `json:"torrents"`
} `json:"result"`
Error *struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error,omitempty"`
Expand All @@ -116,25 +103,21 @@ 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
}

// 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
}

// set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")

// 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
}
defer resp.Body.Close()
Expand All @@ -146,21 +129,15 @@ func (c *BTN) IsUnregistered(torrent *Torrent) (error, bool) {
// 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
}

// 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
}

// 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
}

Expand All @@ -172,13 +149,10 @@ func (c *BTN) IsUnregistered(torrent *Torrent) (error, bool) {
// 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
}
}

// 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
}

Expand Down

0 comments on commit 5fe6473

Please sign in to comment.