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:
Ludvig Lundgren 2022-01-05 23:52:29 +01:00 committed by GitHub
parent d2aa7c1e7e
commit 2ea2293745
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 2181 additions and 99 deletions

93
pkg/btn/btn.go Normal file
View file

@ -0,0 +1,93 @@
package btn
import (
"fmt"
"github.com/autobrr/autobrr/internal/domain"
)
func (c *Client) TestAPI() (bool, error) {
res, err := c.rpcClient.Call("userInfo", [2]string{c.APIKey})
if err != nil {
return false, err
}
var u *UserInfo
err = res.GetObject(&u)
if err != nil {
return false, err
}
if u.Username != "" {
return true, nil
}
return false, nil
}
func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, fmt.Errorf("btn client: must have torrentID")
}
res, err := c.rpcClient.Call("getTorrentById", [2]string{torrentID, c.APIKey})
if err != nil {
return nil, err
}
var r *domain.TorrentBasic
err = res.GetObject(&r)
if err != nil {
return nil, err
}
return r, nil
}
type Torrent struct {
GroupName string `json:"GroupName"`
GroupID string `json:"GroupID"`
TorrentID string `json:"TorrentID"`
SeriesID string `json:"SeriesID"`
Series string `json:"Series"`
SeriesBanner string `json:"SeriesBanner"`
SeriesPoster string `json:"SeriesPoster"`
YoutubeTrailer string `json:"YoutubeTrailer"`
Category string `json:"Category"`
Snatched string `json:"Snatched"`
Seeders string `json:"Seeders"`
Leechers string `json:"Leechers"`
Source string `json:"Source"`
Container string `json:"Container"`
Codec string `json:"Codec"`
Resolution string `json:"Resolution"`
Origin string `json:"Origin"`
ReleaseName string `json:"ReleaseName"`
Size string `json:"Size"`
Time string `json:"Time"`
TvdbID string `json:"TvdbID"`
TvrageID string `json:"TvrageID"`
ImdbID string `json:"ImdbID"`
InfoHash string `json:"InfoHash"`
DownloadURL string `json:"DownloadURL"`
}
type UserInfo struct {
UserID string `json:"UserID"`
Username string `json:"Username"`
Email string `json:"Email"`
Upload string `json:"Upload"`
Download string `json:"Download"`
Lumens string `json:"Lumens"`
Bonus string `json:"Bonus"`
JoinDate string `json:"JoinDate"`
Title string `json:"Title"`
Enabled string `json:"Enabled"`
Paranoia string `json:"Paranoia"`
Invites string `json:"Invites"`
Class string `json:"Class"`
ClassLevel string `json:"ClassLevel"`
HnR string `json:"HnR"`
UploadsSnatched string `json:"UploadsSnatched"`
Snatches string `json:"Snatches"`
}

175
pkg/btn/btn_test.go Normal file
View file

@ -0,0 +1,175 @@
package btn
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/autobrr/autobrr/internal/domain"
"github.com/stretchr/testify/assert"
"github.com/rs/zerolog"
)
func TestAPI(t *testing.T) {
// disable logger
zerolog.SetGlobalLevel(zerolog.Disabled)
mux := http.NewServeMux()
ts := httptest.NewServer(mux)
defer ts.Close()
key := "mock-key"
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// request validation logic
//apiKey := r.Header.Get("ApiKey")
//if apiKey != key {
// w.WriteHeader(http.StatusUnauthorized)
// w.Write(nil)
// return
//}
// read json response
jsonPayload, _ := ioutil.ReadFile("testdata/btn_get_user_info.json")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonPayload)
})
type fields struct {
Url string
APIKey string
}
tests := []struct {
name string
fields fields
want bool
wantErr bool
}{
{
name: "test_user",
fields: fields{
Url: ts.URL,
APIKey: key,
},
want: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIKey)
got, err := c.TestAPI()
if tt.wantErr && assert.Error(t, err) {
assert.Equal(t, tt.wantErr, err)
}
assert.Equal(t, tt.want, got)
})
}
}
func TestClient_GetTorrentByID(t *testing.T) {
// disable logger
zerolog.SetGlobalLevel(zerolog.Disabled)
mux := http.NewServeMux()
ts := httptest.NewServer(mux)
defer ts.Close()
key := "mock-key"
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Expected 'POST' reqeust, got '%v'", r.Method)
}
defer r.Body.Close()
data, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("expected error to be nil got %v", err)
}
if !strings.Contains(string(data), "1555073") {
//t.Errorf(
// `response body "%s" does not contain "1555073"`,
// string(data),
//)
w.WriteHeader(http.StatusNotFound)
return
}
if !strings.Contains(string(data), key) {
jsonPayload, _ := ioutil.ReadFile("testdata/btn_bad_creds.json")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
w.Write(jsonPayload)
return
}
// read json response
jsonPayload, _ := ioutil.ReadFile("testdata/btn_get_torrent_by_id.json")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonPayload)
})
type fields struct {
Url string
APIKey string
}
type args struct {
torrentID string
}
tests := []struct {
name string
fields fields
args args
want *domain.TorrentBasic
wantErr bool
}{
{
name: "btn_get_torrent_by_id",
fields: fields{
Url: ts.URL,
APIKey: key,
},
args: args{torrentID: "1555073"},
want: &domain.TorrentBasic{
Id: "",
TorrentId: "1555073",
InfoHash: "56CD94119F6BF7FC294A92D7A4099C3D1815C907",
Size: "3288852849",
},
wantErr: false,
},
{
name: "btn_get_torrent_by_id_not_found",
fields: fields{
Url: ts.URL,
APIKey: key,
},
args: args{torrentID: "9555073"},
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIKey)
got, err := c.GetTorrentByID(tt.args.torrentID)
if tt.wantErr && assert.Error(t, err) {
assert.Equal(t, tt.wantErr, err)
}
assert.Equal(t, tt.want, got)
})
}
}

58
pkg/btn/client.go Normal file
View 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
}

8
pkg/btn/testdata/btn_bad_creds.json vendored Normal file
View file

@ -0,0 +1,8 @@
{
"id": 1,
"result": null,
"error": {
"code": -32001,
"message": "Invalid API Key"
}
}

View file

@ -0,0 +1,30 @@
{
"id": 1,
"result": {
"GroupName": "S05E04",
"GroupID": "755034",
"TorrentID": "1555073",
"SeriesID": "70834",
"Series": "That Show",
"SeriesBanner": "\/\/cdn2.broadcasthe.net\/tvdb\/banners\/graphical\/0000000000000.jpg",
"SeriesPoster": "\/\/cdn2.broadcasthe.net\/tvdb\/banners\/posters\/0000000000000\/resized_w300.jpg",
"YoutubeTrailer": "",
"Category": "Episode",
"Snatched": "4",
"Seeders": "5",
"Leechers": "41",
"Source": "WEB-DL",
"Container": "MP4",
"Codec": "H.264",
"Resolution": "1080p",
"Origin": "None",
"ReleaseName": "That.Show.S05E04.1080p.WEB-DL.H.264-NOGRP",
"Size": "3288852849",
"Time": "1641153886",
"TvdbID": "332747",
"TvrageID": "0",
"ImdbID": "7252812",
"InfoHash": "56CD94119F6BF7FC294A92D7A4099C3D1815C907",
"DownloadURL": "https:\/\/broadcasthe.net\/torrents.php?action=download&id=1555073&authkey=REDACTED&torrent_pass=REDACTED"
}
}

22
pkg/btn/testdata/btn_get_user_info.json vendored Normal file
View file

@ -0,0 +1,22 @@
{
"id": 1,
"result": {
"UserID": "0000000",
"Username": "username",
"Email": "email@example.com",
"Upload": "90000000000004",
"Download": "10000000000002",
"Lumens": "10000",
"Bonus": "1000000000",
"JoinDate": "1578088136",
"Title": "",
"Enabled": "1",
"Paranoia": "1",
"Invites": "0",
"Class": "Elite",
"ClassLevel": "301",
"HnR": "0",
"UploadsSnatched": "100",
"Snatches": "100"
}
}