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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue