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

185
pkg/ptp/ptp.go Normal file
View file

@ -0,0 +1,185 @@
package ptp
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/rs/zerolog/log"
"golang.org/x/time/rate"
)
type PTPClient interface {
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
TestAPI() (bool, error)
}
type Client struct {
Url string
Timeout int
client *http.Client
Ratelimiter *rate.Limiter
APIUser string
APIKey string
Headers http.Header
}
func NewClient(url string, apiUser string, apiKey string) PTPClient {
// set default url
if url == "" {
url = "https://passthepopcorn.me/torrents.php"
}
c := &Client{
APIUser: apiUser,
APIKey: apiKey,
client: http.DefaultClient,
Url: url,
Ratelimiter: rate.NewLimiter(rate.Every(1*time.Second), 1), // 10 request every 10 seconds
}
return c
}
type TorrentResponse struct {
Page string `json:"Page"`
Result string `json:"Result"`
GroupId string `json:"GroupId"`
Name string `json:"Name"`
Year string `json:"Year"`
CoverImage string `json:"CoverImage"`
AuthKey string `json:"AuthKey"`
PassKey string `json:"PassKey"`
TorrentId string `json:"TorrentId"`
ImdbId string `json:"ImdbId"`
ImdbRating string `json:"ImdbRating"`
ImdbVoteCount int `json:"ImdbVoteCount"`
Torrents []Torrent `json:"Torrents"`
}
type Torrent struct {
Id string `json:"Id"`
InfoHash string `json:"InfoHash"`
Quality string `json:"Quality"`
Source string `json:"Source"`
Container string `json:"Container"`
Codec string `json:"Codec"`
Resolution string `json:"Resolution"`
Size string `json:"Size"`
Scene bool `json:"Scene"`
UploadTime string `json:"UploadTime"`
Snatched string `json:"Snatched"`
Seeders string `json:"Seeders"`
Leechers string `json:"Leechers"`
ReleaseName string `json:"ReleaseName"`
ReleaseGroup *string `json:"ReleaseGroup"`
Checked bool `json:"Checked"`
GoldenPopcorn bool `json:"GoldenPopcorn"`
RemasterTitle string `json:"RemasterTitle,omitempty"`
}
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
}
func (c *Client) get(url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
if err != nil {
log.Error().Err(err).Msgf("ptp client request error : %v", url)
return nil, err
}
req.Header.Add("ApiUser", c.APIUser)
req.Header.Add("ApiKey", c.APIKey)
req.Header.Set("User-Agent", "autobrr")
res, err := c.Do(req)
if err != nil {
log.Error().Err(err).Msgf("ptp client request error : %v", url)
return nil, err
}
if res.StatusCode == http.StatusUnauthorized {
return nil, errors.New("unauthorized: bad credentials")
} else if res.StatusCode == http.StatusForbidden {
return nil, nil
} else if res.StatusCode == http.StatusTooManyRequests {
return nil, nil
}
return res, nil
}
func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, fmt.Errorf("ptp client: must have torrentID")
}
var r TorrentResponse
v := url.Values{}
v.Add("torrentid", torrentID)
params := v.Encode()
url := fmt.Sprintf("%v?%v", c.Url, params)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
return nil, readErr
}
err = json.Unmarshal(body, &r)
if err != nil {
return nil, err
}
for _, torrent := range r.Torrents {
if torrent.Id == torrentID {
return &domain.TorrentBasic{
Id: torrent.Id,
InfoHash: torrent.InfoHash,
Size: torrent.Size,
}, nil
}
}
return nil, nil
}
// TestAPI try api access against torrents page
func (c *Client) TestAPI() (bool, error) {
resp, err := c.get(c.Url)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return true, nil
}
return false, nil
}

176
pkg/ptp/ptp_test.go Normal file
View file

@ -0,0 +1,176 @@
package ptp
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/autobrr/autobrr/internal/domain"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
func TestPTPClient_GetTorrentByID(t *testing.T) {
// disable logger
zerolog.SetGlobalLevel(zerolog.Disabled)
user := "mock-user"
key := "mock-key"
ts := httptest.NewServer(http.HandlerFunc(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
}
apiUser := r.Header.Get("ApiUser")
if apiUser != user {
w.WriteHeader(http.StatusUnauthorized)
w.Write(nil)
return
}
// read json response
jsonPayload, _ := ioutil.ReadFile("testdata/ptp_get_torrent_by_id.json")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonPayload)
}))
defer ts.Close()
type fields struct {
Url string
APIUser string
APIKey string
}
type args struct {
torrentID string
}
tests := []struct {
name string
fields fields
args args
want *domain.TorrentBasic
wantErr bool
}{
{
name: "get_by_id_1",
fields: fields{
Url: ts.URL,
APIUser: user,
APIKey: key,
},
args: args{torrentID: "000001"},
want: &domain.TorrentBasic{
Id: "000001",
InfoHash: "F57AA86DFB03F87FCC7636E310D35918442EAE5C",
Size: "1344512700",
},
wantErr: false,
},
{
name: "get_by_id_2",
fields: fields{
Url: ts.URL,
APIUser: user,
APIKey: key,
},
args: args{torrentID: "100002"},
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIUser, 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)
})
}
}
func Test(t *testing.T) {
// disable logger
zerolog.SetGlobalLevel(zerolog.Disabled)
user := "mock-user"
key := "mock-key"
ts := httptest.NewServer(http.HandlerFunc(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
}
apiUser := r.Header.Get("ApiUser")
if apiUser != user {
w.WriteHeader(http.StatusUnauthorized)
w.Write(nil)
return
}
// read json response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(nil)
}))
defer ts.Close()
type fields struct {
Url string
APIUser string
APIKey string
}
tests := []struct {
name string
fields fields
want bool
wantErr bool
}{
{
name: "ok",
fields: fields{
Url: ts.URL,
APIUser: user,
APIKey: key,
},
want: true,
wantErr: false,
},
{
name: "bad_creds",
fields: fields{
Url: ts.URL,
APIUser: user,
APIKey: "",
},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIUser, tt.fields.APIKey)
got, err := c.TestAPI()
if tt.wantErr && assert.Error(t, err) {
assert.Equal(t, tt.wantErr, err)
}
assert.Equalf(t, tt.want, got, "Test()")
})
}
}

View file

@ -0,0 +1,112 @@
{
"Page": "Details",
"Result": "OK",
"GroupId": "54664",
"Name": "That Movie",
"Year": "1980",
"CoverImage": "https:\/\/ptpimg.me\/58999s.jpg",
"AuthKey": "",
"PassKey": "",
"TorrentId": "999734",
"ImdbId": "0081229",
"ImdbRating": "4.7",
"ImdbVoteCount": 1859,
"Torrents": [
{
"Id": "000001",
"InfoHash": "F57AA86DFB03F87FCC7636E310D35918442EAE5C",
"Quality": "Standard Definition",
"Source": "DVD",
"Container": "MKV",
"Codec": "x264",
"Resolution": "720x480",
"Size": "1344512700",
"Scene": false,
"UploadTime": "2011-12-09 00:00:15",
"Snatched": "98",
"Seeders": "19",
"Leechers": "0",
"ReleaseName": "That.Movie.1980.DVDRip.x264-HANDJOB",
"ReleaseGroup": "HANDJOB",
"Checked": true,
"GoldenPopcorn": false
},
{
"Id": "999734",
"InfoHash": "692978AB777A84262D53AE6994E399DDA835F8AD",
"Quality": "Standard Definition",
"Source": "Blu-ray",
"Container": "MKV",
"Codec": "x264",
"Resolution": "576p",
"Size": "1943081527",
"Scene": false,
"UploadTime": "2022-01-02 15:13:27",
"Snatched": "0",
"Seeders": "1",
"Leechers": "1",
"ReleaseName": "Director - (1980) That Movie",
"ReleaseGroup": null,
"Checked": false,
"GoldenPopcorn": false
},
{
"Id": "179783",
"InfoHash": "1D29AD8663A501FB1699FEF82D9B1637CEA27FD0",
"Quality": "Standard Definition",
"Source": "DVD",
"Container": "VOB IFO",
"Codec": "DVD5",
"Resolution": "NTSC",
"Size": "4011452416",
"Scene": false,
"UploadTime": "2012-11-23 02:46:15",
"Snatched": "17",
"Seeders": "4",
"Leechers": "0",
"ReleaseName": "That Movie (1980)",
"ReleaseGroup": null,
"Checked": true,
"GoldenPopcorn": false
},
{
"Id": "435503",
"InfoHash": "C45AE6F06BC8CFBAD5180B12CAFB6A86A0DABE7E",
"Quality": "Standard Definition",
"Source": "DVD",
"Container": "VOB IFO",
"Codec": "DVD9",
"Resolution": "PAL",
"Size": "8200126464",
"Scene": false,
"UploadTime": "2016-07-15 14:32:08",
"Snatched": "15",
"Seeders": "2",
"Leechers": "0",
"ReleaseName": "That Movie [1980]",
"ReleaseGroup": null,
"Checked": true,
"GoldenPopcorn": false
},
{
"Id": "997572",
"InfoHash": "CA5DC722F5C7BDCB5434D84ADA2104DDF07F07C6",
"Quality": "High Definition",
"Source": "Blu-ray",
"Container": "m2ts",
"Codec": "BD50",
"Resolution": "1080p",
"Size": "67796072528",
"Scene": false,
"UploadTime": "2021-12-24 03:23:58",
"RemasterTitle": "2-Disc Set",
"Snatched": "18",
"Seeders": "12",
"Leechers": "0",
"ReleaseName": "That Movie 2 DISC (Severin) (_10_)",
"ReleaseGroup": null,
"Checked": true,
"GoldenPopcorn": false
}
]
}