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

245
pkg/ggn/ggn.go Normal file
View file

@ -0,0 +1,245 @@
package ggn
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/rs/zerolog/log"
"golang.org/x/time/rate"
)
type Client interface {
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
TestAPI() (bool, error)
}
type client struct {
Url string
Timeout int
client *http.Client
Ratelimiter *rate.Limiter
APIKey string
Headers http.Header
}
func NewClient(url string, apiKey string) Client {
// set default url
if url == "" {
url = "https://gazellegames.net/api.php"
}
c := &client{
APIKey: apiKey,
client: http.DefaultClient,
Url: url,
Ratelimiter: rate.NewLimiter(rate.Every(5*time.Second), 1), // 5 request every 10 seconds
}
return c
}
type Group struct {
BbWikiBody string `json:"bbWikiBody"`
WikiBody string `json:"wikiBody"`
WikiImage string `json:"wikiImage"`
Id int `json:"id"`
Name string `json:"name"`
Aliases []string `json:"aliases"`
Year int `json:"year"`
CategoryId int `json:"categoryId"`
CategoryName string `json:"categoryName"`
MasterGroup int `json:"masterGroup"`
Time string `json:"time"`
GameInfo struct {
Screenshots []string `json:"screenshots"`
Trailer string `json:"trailer"`
Rating string `json:"rating"`
MetaRating struct {
Score string `json:"score"`
Percent string `json:"percent"`
Link string `json:"link"`
} `json:"metaRating"`
IgnRating struct {
Score string `json:"score"`
Percent string `json:"percent"`
Link string `json:"link"`
} `json:"ignRating"`
GamespotRating struct {
Score string `json:"score"`
Percent string `json:"percent"`
Link string `json:"link"`
} `json:"gamespotRating"`
Weblinks struct {
GamesWebsite string `json:"GamesWebsite"`
Wikipedia string `json:"Wikipedia"`
Giantbomb string `json:"Giantbomb"`
GameFAQs string `json:"GameFAQs"`
PCGamingWiki string `json:"PCGamingWiki"`
Steam string `json:"Steam"`
Amazon string `json:"Amazon"`
GOG string `json:"GOG"`
HowLongToBeat string `json:"HowLongToBeat"`
} `json:"weblinks"`
} `json:"gameInfo"`
Tags []string `json:"tags"`
Platform string `json:"platform"`
}
type Torrent struct {
Id int `json:"id"`
InfoHash string `json:"infoHash"`
Type string `json:"type"`
Link string `json:"link"`
Format string `json:"format"`
Encoding string `json:"encoding"`
Region string `json:"region"`
Language string `json:"language"`
Remastered bool `json:"remastered"`
RemasterYear int `json:"remasterYear"`
RemasterTitle string `json:"remasterTitle"`
Scene bool `json:"scene"`
HasCue bool `json:"hasCue"`
ReleaseTitle string `json:"releaseTitle"`
ReleaseType string `json:"releaseType"`
GameDOXType string `json:"gameDOXType"`
GameDOXVersion string `json:"gameDOXVersion"`
FileCount int `json:"fileCount"`
Size uint64 `json:"size"`
Seeders int `json:"seeders"`
Leechers int `json:"leechers"`
Snatched int `json:"snatched"`
FreeTorrent bool `json:"freeTorrent"`
NeutralTorrent bool `json:"neutralTorrent"`
Reported bool `json:"reported"`
Time string `json:"time"`
BbDescription string `json:"bbDescription"`
Description string `json:"description"`
FileList []struct {
Ext string `json:"ext"`
Size string `json:"size"`
Name string `json:"name"`
} `json:"fileList"`
FilePath string `json:"filePath"`
UserId int `json:"userId"`
Username string `json:"username"`
}
type TorrentResponse struct {
Group Group `json:"group"`
Torrent Torrent `json:"torrent"`
}
type Response struct {
Status string `json:"status"`
Response TorrentResponse `json:"response,omitempty"`
Error string `json:"error,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("ggn client request error : %v", url)
return nil, err
}
req.Header.Add("X-API-Key", c.APIKey)
req.Header.Set("User-Agent", "autobrr")
res, err := c.Do(req)
if err != nil {
log.Error().Err(err).Msgf("ggn 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("ggn client: must have torrentID")
}
var r Response
v := url.Values{}
v.Add("id", torrentID)
params := v.Encode()
url := fmt.Sprintf("%v?%v&%v", c.Url, "request=torrent", 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
}
if r.Status != "success" {
return nil, fmt.Errorf("bad status: %v", r.Status)
}
t := &domain.TorrentBasic{
Id: strconv.Itoa(r.Response.Torrent.Id),
InfoHash: r.Response.Torrent.InfoHash,
Size: strconv.FormatUint(r.Response.Torrent.Size, 10),
}
return t, 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
}

99
pkg/ggn/ggn_test.go Normal file
View file

@ -0,0 +1,99 @@
package ggn
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/autobrr/autobrr/internal/domain"
)
func Test_client_GetTorrentByID(t *testing.T) {
// disable logger
zerolog.SetGlobalLevel(zerolog.Disabled)
key := "mock-key"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// request validation logic
apiKey := r.Header.Get("X-API-Key")
if apiKey != key {
w.WriteHeader(http.StatusUnauthorized)
w.Write(nil)
return
}
if !strings.Contains(r.RequestURI, "422368") {
jsonPayload, _ := ioutil.ReadFile("testdata/ggn_get_torrent_by_id_not_found.json")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonPayload)
return
}
// read json response
jsonPayload, _ := ioutil.ReadFile("testdata/ggn_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
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,
APIKey: key,
},
args: args{torrentID: "422368"},
want: &domain.TorrentBasic{
Id: "422368",
InfoHash: "78DA2811E6732012B8224198D4DC2FD49A5E950F",
Size: "134800",
},
wantErr: false,
},
{
name: "get_by_id_2",
fields: fields{
Url: ts.URL,
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.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)
})
}
}

View file

@ -0,0 +1 @@
{"status":"failure","error":"bad id parameter"}

1
pkg/ggn/testdata/ggn_get_index.json vendored Normal file
View file

@ -0,0 +1 @@
{"status":"success","response":{"api_version":"3.1.0"}}

View file

@ -0,0 +1,111 @@
{
"status": "success",
"response": {
"group": {
"bbWikiBody": "Some Game",
"wikiBody": "Some Game",
"wikiImage": "https:\/\/ptpimg.me\/2y8t7a.jpg",
"id": 2279,
"name": "Some Game",
"aliases": [
""
],
"year": 2011,
"categoryId": 1,
"categoryName": "Games",
"masterGroup": 769,
"time": "2022-01-04 22:34:14",
"gameInfo": {
"screenshots": [
"https%3A%2F%2Fptpimg.me%2Fw176o2.jpg",
"https%3A%2F%2Fptpimg.me%2Fw44511.jpg",
"https%3A%2F%2Fptpimg.me%2F591yx9.jpg",
"https%3A%2F%2Fptpimg.me%2Fqm7bw1.jpg"
],
"trailer": "http:\/\/www.youtube.com\/watch?v=xxxxxxxxxxxx",
"rating": "7+",
"metaRating": {
"score": "73",
"percent": "73%",
"link": "http%3A%2F%2Fwww.metacritic.com%2Fgame%2Fpc%2Fthat-game"
},
"ignRating": {
"score": "0",
"percent": "0%",
"link": ""
},
"gamespotRating": {
"score": "6.5",
"percent": "65%",
"link": "http%3A%2F%2Fwww.gamespot.com%2Freviews%2Fthat-game"
},
"weblinks": {
"GamesWebsite": "http:\/\/games.disney.com\/some-game",
"Wikipedia": "https:\/\/en.wikipedia.org\/wiki\/some-game",
"Giantbomb": "http:\/\/www.giantbomb.com\/some-game\/3030-33207\/",
"GameFAQs": "http:\/\/www.gamefaqs.com\/pc\/612250-some-game",
"PCGamingWiki": "http:\/\/pcgamingwiki.com\/wiki\/Some_Game",
"Steam": "http:\/\/store.steampowered.com\/app\/0000000\/",
"Amazon": "http:\/\/www.amazon.com\/Some-Game-PC\/dp\/B002I0JJMK",
"GOG": "https:\/\/www.gog.com\/game\/some_game",
"HowLongToBeat": "https:\/\/howlongtobeat.com\/game.php?id=0000"
}
},
"tags": [
"action",
"adventure"
],
"platform": "Windows"
},
"torrent": {
"id": 422368,
"infoHash": "78DA2811E6732012B8224198D4DC2FD49A5E950F",
"type": "Torrent",
"link": "",
"format": "",
"encoding": "",
"region": "",
"language": "English",
"remastered": false,
"remasterYear": 0,
"remasterTitle": "",
"scene": true,
"hasCue": false,
"releaseTitle": "Some_Game_Patch_1_Plus_5_Trainer-RazorDOX",
"releaseType": "GameDOX",
"gameDOXType": "Trainer",
"gameDOXVersion": "",
"fileCount": 3,
"size": 134800,
"seeders": 10,
"leechers": 0,
"snatched": 20,
"freeTorrent": false,
"neutralTorrent": false,
"reported": false,
"time": "2022-01-04 22:34:14",
"bbDescription": "[align=center][img]https:\/\/gazellegames.net\/nfoimg\/422368.png[\/img][\/align]",
"description": "<div style=\"text-align:center;\"><img style=\"max-width: 600px;\" onclick=\"lightbox.init(this,600);\" alt=\"https:\/\/gazellegames.net\/nfoimg\/422368.png\" src=\"\/nfoimg\/422368.png\" \/><\/div>",
"fileList": [
{
"ext": ".nfo",
"size": "4982",
"name": "rzr-lpp1.nfo"
},
{
"ext": ".rar",
"size": "129795",
"name": "rzr-lpp1.rar"
},
{
"ext": ".sfv",
"size": "23",
"name": "rzr-lpp1.sfv"
}
],
"filePath": "Some_Game_Patch_1_Plus_5_Trainer-RazorDOX",
"userId": 10000,
"username": "username"
}
}
}

View file

@ -0,0 +1 @@
{"status":401,"error":"APIKey is not valid."}

231
pkg/jsonrpc/jsonrpc.go Normal file
View file

@ -0,0 +1,231 @@
package jsonrpc
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
"strconv"
)
type Client interface {
Call(method string, params ...interface{}) (*RPCResponse, error)
}
type RPCRequest struct {
JsonRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params"`
ID int `json:"id"`
}
func NewRequest(method string, params ...interface{}) *RPCRequest {
return &RPCRequest{
JsonRPC: "2.0",
Method: method,
Params: Params(params...),
ID: 0,
}
}
type RPCResponse struct {
JsonRPC string `json:"jsonrpc"`
Result interface{} `json:"result,omitempty"`
Error *RPCError `json:"error,omitempty"`
ID int `json:"id"`
}
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
func (e *RPCError) Error() string {
return strconv.Itoa(e.Code) + ":" + e.Message
}
type HTTPError struct {
Code int
err error
}
func (e *HTTPError) Error() string {
return e.err.Error()
}
type rpcClient struct {
endpoint string
httpClient *http.Client
headers map[string]string
}
type ClientOpts struct {
HTTPClient *http.Client
Headers map[string]string
}
type RPCResponses []*RPCResponse
func NewClient(endpoint string) Client {
return NewClientWithOpts(endpoint, nil)
}
func NewClientWithOpts(endpoint string, opts *ClientOpts) Client {
c := &rpcClient{
endpoint: endpoint,
httpClient: &http.Client{},
headers: make(map[string]string),
}
if opts == nil {
return c
}
if opts.HTTPClient != nil {
c.httpClient = opts.HTTPClient
}
if opts.Headers != nil {
for k, v := range opts.Headers {
c.headers[k] = v
}
}
return c
}
func (c *rpcClient) Call(method string, params ...interface{}) (*RPCResponse, error) {
request := RPCRequest{
JsonRPC: "2.0",
Method: method,
Params: Params(params...),
}
return c.doCall(request)
}
func (c *rpcClient) newRequest(req interface{}) (*http.Request, error) {
body, err := json.Marshal(req)
if err != nil {
return nil, err
}
request, err := http.NewRequest("POST", c.endpoint, bytes.NewReader(body))
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Accept", "application/json")
for k, v := range c.headers {
request.Header.Set(k, v)
}
return request, nil
}
func (c *rpcClient) doCall(request RPCRequest) (*RPCResponse, error) {
httpRequest, err := c.newRequest(request)
if err != nil {
return nil, err
}
httpResponse, err := c.httpClient.Do(httpRequest)
if err != nil {
return nil, err
}
defer httpResponse.Body.Close()
var rpcResponse *RPCResponse
decoder := json.NewDecoder(httpResponse.Body)
decoder.DisallowUnknownFields()
decoder.UseNumber()
err = decoder.Decode(&rpcResponse)
if err != nil {
if httpResponse.StatusCode >= 400 {
return nil, fmt.Errorf("rpc call %v() on %v status code: %v. Could not decode body to rpc response: %v", request.Method, httpRequest.URL.String(), httpResponse.StatusCode, err.Error())
}
// 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
// } else if res.StatusCode == http.StatusBadRequest {
// return nil, nil
// } else if res.StatusCode == http.StatusNotFound {
// return nil, nil
// } else if res.StatusCode == http.StatusServiceUnavailable {
// return nil, nil
// }
}
if rpcResponse == nil {
return nil, fmt.Errorf("rpc call %v() on %v status code: %v. rpc response missing", request.Method, httpRequest.URL.String(), httpResponse.StatusCode)
}
return rpcResponse, nil
}
func Params(params ...interface{}) interface{} {
var finalParams interface{}
// if params was nil skip this and p stays nil
if params != nil {
switch len(params) {
case 0: // no parameters were provided, do nothing so finalParam is nil and will be omitted
case 1: // one param was provided, use it directly as is, or wrap primitive types in array
if params[0] != nil {
var typeOf reflect.Type
// traverse until nil or not a pointer type
for typeOf = reflect.TypeOf(params[0]); typeOf != nil && typeOf.Kind() == reflect.Ptr; typeOf = typeOf.Elem() {
}
if typeOf != nil {
// now check if we can directly marshal the type or if it must be wrapped in an array
switch typeOf.Kind() {
// for these types we just do nothing, since value of p is already unwrapped from the array params
case reflect.Struct:
finalParams = params[0]
case reflect.Array:
finalParams = params[0]
case reflect.Slice:
finalParams = params[0]
case reflect.Interface:
finalParams = params[0]
case reflect.Map:
finalParams = params[0]
default: // everything else must stay in an array (int, string, etc)
finalParams = params
}
}
} else {
finalParams = params
}
default: // if more than one parameter was provided it should be treated as an array
finalParams = params
}
}
return finalParams
}
func (r *RPCResponse) GetObject(toType interface{}) error {
js, err := json.Marshal(r.Result)
if err != nil {
return err
}
err = json.Unmarshal(js, toType)
if err != nil {
return err
}
return nil
}

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