mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
feat(filters): add music filters (#91)
* feat(filters): add music filters * feat: improve parsing and filtering * feat: add red api support
This commit is contained in:
parent
30c11d4ef1
commit
00bc8298ac
20 changed files with 1053 additions and 52 deletions
212
pkg/red/red.go
Normal file
212
pkg/red/red.go
Normal file
|
@ -0,0 +1,212 @@
|
|||
package red
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"golang.org/x/time/rate"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
type REDClient 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
|
||||
}
|
||||
|
||||
func NewClient(url string, apiKey string) REDClient {
|
||||
if url == "" {
|
||||
url = "https://redacted.ch/ajax.php"
|
||||
}
|
||||
|
||||
c := &Client{
|
||||
APIKey: apiKey,
|
||||
client: http.DefaultClient,
|
||||
URL: url,
|
||||
RateLimiter: rate.NewLimiter(rate.Every(10*time.Second), 10),
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
type TorrentDetailsResponse struct {
|
||||
Status string `json:"status"`
|
||||
Response struct {
|
||||
Group Group `json:"group"`
|
||||
Torrent Torrent `json:"torrent"`
|
||||
} `json:"response"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
//WikiBody string `json:"wikiBody"`
|
||||
//WikiImage string `json:"wikiImage"`
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Year int `json:"year"`
|
||||
RecordLabel string `json:"recordLabel"`
|
||||
CatalogueNumber string `json:"catalogueNumber"`
|
||||
ReleaseType int `json:"releaseType"`
|
||||
CategoryId int `json:"categoryId"`
|
||||
CategoryName string `json:"categoryName"`
|
||||
Time string `json:"time"`
|
||||
VanityHouse bool `json:"vanityHouse"`
|
||||
//MusicInfo struct {
|
||||
// Composers []interface{} `json:"composers"`
|
||||
// Dj []interface{} `json:"dj"`
|
||||
// Artists []struct {
|
||||
// Id int `json:"id"`
|
||||
// Name string `json:"name"`
|
||||
// } `json:"artists"`
|
||||
// With []struct {
|
||||
// Id int `json:"id"`
|
||||
// Name string `json:"name"`
|
||||
// } `json:"with"`
|
||||
// Conductor []interface{} `json:"conductor"`
|
||||
// RemixedBy []interface{} `json:"remixedBy"`
|
||||
// Producer []interface{} `json:"producer"`
|
||||
//} `json:"musicInfo"`
|
||||
}
|
||||
|
||||
type Torrent struct {
|
||||
Id int `json:"id"`
|
||||
InfoHash string `json:"infoHash"`
|
||||
Media string `json:"media"`
|
||||
Format string `json:"format"`
|
||||
Encoding string `json:"encoding"`
|
||||
Remastered bool `json:"remastered"`
|
||||
RemasterYear int `json:"remasterYear"`
|
||||
RemasterTitle string `json:"remasterTitle"`
|
||||
RemasterRecordLabel string `json:"remasterRecordLabel"`
|
||||
RemasterCatalogueNumber string `json:"remasterCatalogueNumber"`
|
||||
Scene bool `json:"scene"`
|
||||
HasLog bool `json:"hasLog"`
|
||||
HasCue bool `json:"hasCue"`
|
||||
LogScore int `json:"logScore"`
|
||||
FileCount int `json:"fileCount"`
|
||||
Size int `json:"size"`
|
||||
Seeders int `json:"seeders"`
|
||||
Leechers int `json:"leechers"`
|
||||
Snatched int `json:"snatched"`
|
||||
FreeTorrent bool `json:"freeTorrent"`
|
||||
IsNeutralleech bool `json:"isNeutralleech"`
|
||||
IsFreeload bool `json:"isFreeload"`
|
||||
Time string `json:"time"`
|
||||
Description string `json:"description"`
|
||||
FileList string `json:"fileList"`
|
||||
FilePath string `json:"filePath"`
|
||||
UserId int `json:"userId"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
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("red client request error : %v", url)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", c.APIKey)
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
res, err := c.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("red 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.StatusBadRequest {
|
||||
return nil, errors.New("bad id parameter")
|
||||
} else if res.StatusCode == http.StatusTooManyRequests {
|
||||
return nil, errors.New("rate-limited")
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error) {
|
||||
if torrentID == "" {
|
||||
return nil, fmt.Errorf("red client: must have torrentID")
|
||||
}
|
||||
|
||||
var r TorrentDetailsResponse
|
||||
|
||||
v := url.Values{}
|
||||
v.Add("id", torrentID)
|
||||
params := v.Encode()
|
||||
|
||||
url := fmt.Sprintf("%v?action=torrent&%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
|
||||
}
|
||||
|
||||
return &domain.TorrentBasic{
|
||||
Id: strconv.Itoa(r.Response.Torrent.Id),
|
||||
InfoHash: r.Response.Torrent.InfoHash,
|
||||
Size: strconv.Itoa(r.Response.Torrent.Size),
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
// TestAPI try api access against torrents page
|
||||
func (c *Client) TestAPI() (bool, error) {
|
||||
resp, err := c.get(c.URL + "?action=index")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
109
pkg/red/red_test.go
Normal file
109
pkg/red/red_test.go
Normal file
|
@ -0,0 +1,109 @@
|
|||
package red
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"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 TestREDClient_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("Authorization")
|
||||
if apiKey != key {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write(nil)
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.Contains(r.RequestURI, "29991962") {
|
||||
jsonPayload, _ := ioutil.ReadFile("testdata/get_torrent_by_id_not_found.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write(jsonPayload)
|
||||
return
|
||||
}
|
||||
|
||||
// read json response
|
||||
jsonPayload, _ := ioutil.ReadFile("testdata/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 error
|
||||
}{
|
||||
{
|
||||
name: "get_by_id_1",
|
||||
fields: fields{
|
||||
Url: ts.URL,
|
||||
APIKey: key,
|
||||
},
|
||||
args: args{torrentID: "29991962"},
|
||||
want: &domain.TorrentBasic{
|
||||
Id: "29991962",
|
||||
InfoHash: "B2BABD3A361EAFC6C4E9142C422DF7DDF5D7E163",
|
||||
Size: "527749302",
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "get_by_id_2",
|
||||
fields: fields{
|
||||
Url: ts.URL,
|
||||
APIKey: key,
|
||||
},
|
||||
args: args{torrentID: "100002"},
|
||||
want: nil,
|
||||
wantErr: errors.New("bad id parameter"),
|
||||
},
|
||||
{
|
||||
name: "get_by_id_3",
|
||||
fields: fields{
|
||||
Url: ts.URL,
|
||||
APIKey: "",
|
||||
},
|
||||
args: args{torrentID: "100002"},
|
||||
want: nil,
|
||||
wantErr: errors.New("unauthorized: bad credentials"),
|
||||
},
|
||||
}
|
||||
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 != nil && assert.Error(t, err) {
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
23
pkg/red/testdata/get_index.json
vendored
Normal file
23
pkg/red/testdata/get_index.json
vendored
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"status": "success",
|
||||
"response": {
|
||||
"username": "username",
|
||||
"id": 469,
|
||||
"authkey": "redacted",
|
||||
"passkey": "redacted",
|
||||
"api_version": "redacted-v2.0",
|
||||
"notifications": {
|
||||
"messages": 0,
|
||||
"notifications": 9000,
|
||||
"newAnnouncement": false,
|
||||
"newBlog": false
|
||||
},
|
||||
"userstats": {
|
||||
"uploaded": 585564424629,
|
||||
"downloaded": 177461229738,
|
||||
"ratio": 3.29,
|
||||
"requiredratio": 0.6,
|
||||
"class": "VIP"
|
||||
}
|
||||
}
|
||||
}
|
77
pkg/red/testdata/get_torrent_by_id.json
vendored
Normal file
77
pkg/red/testdata/get_torrent_by_id.json
vendored
Normal file
|
@ -0,0 +1,77 @@
|
|||
|
||||
{
|
||||
"status": "success",
|
||||
"response": {
|
||||
"group": {
|
||||
"wikiBody": "",
|
||||
"wikiImage": "http://whatimg.com/i/ralpc.jpg",
|
||||
"id": 72189681,
|
||||
"name": "Fear Not",
|
||||
"year": 2012,
|
||||
"recordLabel": "Hospital Records",
|
||||
"catalogueNumber": "NHS209CD",
|
||||
"releaseType": 1,
|
||||
"categoryId": 1,
|
||||
"categoryName": "Music",
|
||||
"time": "2012-05-02 07:39:30",
|
||||
"vanityHouse": false,
|
||||
"musicInfo": {
|
||||
"composers": [],
|
||||
"dj": [],
|
||||
"artists": [
|
||||
{
|
||||
"id": 1460,
|
||||
"name": "Logistics"
|
||||
}
|
||||
],
|
||||
"with": [
|
||||
{
|
||||
"id": 25351,
|
||||
"name": "Alice Smith"
|
||||
},
|
||||
{
|
||||
"id": 44545,
|
||||
"name": "Nightshade"
|
||||
},
|
||||
{
|
||||
"id": 249446,
|
||||
"name": "Sarah Callander"
|
||||
}
|
||||
],
|
||||
"conductor": [],
|
||||
"remixedBy": [],
|
||||
"producer": []
|
||||
}
|
||||
},
|
||||
"torrent": {
|
||||
"id": 29991962,
|
||||
"infoHash": "B2BABD3A361EAFC6C4E9142C422DF7DDF5D7E163",
|
||||
"media": "CD",
|
||||
"format": "FLAC",
|
||||
"encoding": "Lossless",
|
||||
"remastered": false,
|
||||
"remasterYear": 0,
|
||||
"remasterTitle": "",
|
||||
"remasterRecordLabel": "",
|
||||
"remasterCatalogueNumber": "",
|
||||
"scene": true,
|
||||
"hasLog": false,
|
||||
"hasCue": false,
|
||||
"logScore": 0,
|
||||
"fileCount": 19,
|
||||
"size": 527749302,
|
||||
"seeders": 20,
|
||||
"leechers": 0,
|
||||
"snatched": 55,
|
||||
"freeTorrent": false,
|
||||
"isNeutralleech": false,
|
||||
"isFreeload": false,
|
||||
"time": "2012-04-14 15:57:00",
|
||||
"description": "",
|
||||
"fileList": "00-logistics-fear_not-cd-flac-2012.jpg{{{1233205}}}|||00-logistics-fear_not-cd-flac-2012.m3u{{{538}}}|||00-logistics-fear_not-cd-flac-2012.nfo{{{1607}}}|||00-logistics-fear_not-cd-flac-2012.sfv{{{688}}}|||01-logistics-fear_not.flac{{{38139451}}}|||02-logistics-timelapse.flac{{{39346037}}}|||03-logistics-2999_(wherever_you_go).flac{{{41491133}}}|||04-logistics-try_again.flac{{{32151567}}}|||05-logistics-we_are_one.flac{{{40778041}}}|||06-logistics-crystal_skies_(feat_nightshade_and_sarah_callander).flac{{{34544405}}}|||07-logistics-feels_so_good.flac{{{41363732}}}|||08-logistics-running_late.flac{{{16679269}}}|||09-logistics-early_again.flac{{{35373278}}}|||10-logistics-believe_in_me.flac{{{39495420}}}|||11-logistics-letting_go.flac{{{30846730}}}|||12-logistics-sendai_song.flac{{{35021141}}}|||13-logistics-over_and_out.flac{{{44621200}}}|||14-logistics-destination_unknown.flac{{{13189493}}}|||15-logistics-watching_the_world_go_by_(feat_alice_smith).flac{{{43472367}}}",
|
||||
"filePath": "Logistics-Fear_Not-CD-FLAC-2012-TaBoo",
|
||||
"userId": 567,
|
||||
"username": null
|
||||
}
|
||||
}
|
||||
}
|
1
pkg/red/testdata/get_torrent_by_id_not_found.json
vendored
Normal file
1
pkg/red/testdata/get_torrent_by_id_not_found.json
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
{"status":"failure","error":"bad id parameter"}
|
Loading…
Add table
Add a link
Reference in a new issue