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

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