mirror of
https://github.com/idanoo/autobrr
synced 2025-07-22 16:29:12 +00:00
Feature: Get size by api for ptp btn and ggn (#66)
* chore: add package * feat: get size by api for ptp and btn * feat: download and parse torrent if not api * feat: bypass tls check and load meta from file * fix: no invite command needed for btn * feat: add ggn api * feat: imrpove logging * feat: build request url * feat: improve err logging
This commit is contained in:
parent
d2aa7c1e7e
commit
2ea2293745
32 changed files with 2181 additions and 99 deletions
58
pkg/btn/client.go
Normal file
58
pkg/btn/client.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package btn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/jsonrpc"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
type BTNClient interface {
|
||||
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
|
||||
TestAPI() (bool, error)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
Timeout int
|
||||
client *http.Client
|
||||
rpcClient jsonrpc.Client
|
||||
Ratelimiter *rate.Limiter
|
||||
APIKey string
|
||||
Headers http.Header
|
||||
}
|
||||
|
||||
func NewClient(url string, apiKey string) BTNClient {
|
||||
if url == "" {
|
||||
url = "https://api.broadcasthe.net/"
|
||||
}
|
||||
|
||||
c := &Client{
|
||||
client: http.DefaultClient,
|
||||
rpcClient: jsonrpc.NewClientWithOpts(url, &jsonrpc.ClientOpts{
|
||||
Headers: map[string]string{
|
||||
"User-Agent": "autobrr",
|
||||
},
|
||||
}),
|
||||
APIKey: apiKey,
|
||||
Ratelimiter: rate.NewLimiter(rate.Every(150*time.Hour), 1), // 150 rpcRequest every 1 hour
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
ctx := context.Background()
|
||||
err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue