feat(lists): integrate Omegabrr (#1885)

* feat(lists): integrate Omegabrr

* feat(lists): add missing lists index

* feat(lists): add db repo

* feat(lists): add db migrations

* feat(lists): labels

* feat(lists): url lists and more arrs

* fix(lists): db migrations client_id wrong type

* fix(lists): db fields

* feat(lists): create list form wip

* feat(lists): show in list and create

* feat(lists): update and delete

* feat(lists): trigger via webhook

* feat(lists): add webhook handler

* fix(arr): encode json to pointer

* feat(lists): rename endpoint to lists

* feat(lists): fetch tags from arr

* feat(lists): process plaintext lists

* feat(lists): add background refresh job

* run every 6th hour with a random start delay between 1-35 seconds

* feat(lists): refresh on save and improve logging

* feat(lists): cast arr client to pointer

* feat(lists): improve error handling

* feat(lists): reset shows field with match release

* feat(lists): filter opts all lists

* feat(lists): trigger on update if enabled

* feat(lists): update option for lists

* feat(lists): show connected filters in list

* feat(lists): missing listSvc dep

* feat(lists): cleanup

* feat(lists): typo arr list

* feat(lists): radarr include original

* feat(lists): rename ExcludeAlternateTitle to IncludeAlternateTitle

* fix(lists): arr client type conversion to pointer

* fix(actions): only log panic recover if err not nil

* feat(lists): show spinner on save

* feat(lists): show icon in filters list

* feat(lists): change icon color in filters list

* feat(lists): delete relations on filter delete
This commit is contained in:
ze0s 2024-12-25 13:23:37 +01:00 committed by GitHub
parent b68ae334ca
commit 221bc35371
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
77 changed files with 5025 additions and 254 deletions

198
pkg/arr/lidarr/client.go Normal file
View file

@ -0,0 +1,198 @@
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package lidarr
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"path"
"github.com/autobrr/autobrr/pkg/errors"
)
func (c *Client) get(ctx context.Context, endpoint string) (int, []byte, error) {
u, err := url.Parse(c.config.Hostname)
if err != nil {
return 0, nil, errors.Wrap(err, "could not parse url: %s", c.config.Hostname)
}
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
reqUrl := u.String()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, http.NoBody)
if err != nil {
return 0, nil, errors.Wrap(err, "lidarr Client request error : %v", reqUrl)
}
if c.config.BasicAuth {
req.SetBasicAuth(c.config.Username, c.config.Password)
}
c.setHeaders(req)
resp, err := c.http.Do(req)
if err != nil {
return 0, nil, errors.Wrap(err, "lidarr.http.Do(req)")
}
defer resp.Body.Close()
if resp.Body == nil {
return resp.StatusCode, nil, errors.New("response body is nil")
}
var buf bytes.Buffer
if _, err = io.Copy(&buf, resp.Body); err != nil {
return resp.StatusCode, nil, errors.Wrap(err, "lidarr.io.Copy error")
}
return resp.StatusCode, buf.Bytes(), nil
}
func (c *Client) getJSON(ctx context.Context, endpoint string, params url.Values, data any) error {
u, err := url.Parse(c.config.Hostname)
if err != nil {
return errors.Wrap(err, "could not parse url: %s", c.config.Hostname)
}
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
reqUrl := u.String()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, http.NoBody)
if err != nil {
return errors.Wrap(err, "could not build request")
}
if c.config.BasicAuth {
req.SetBasicAuth(c.config.Username, c.config.Password)
}
c.setHeaders(req)
req.URL.RawQuery = params.Encode()
resp, err := c.http.Do(req)
if err != nil {
return errors.Wrap(err, "lidarr.http.Do(req): %+v", req)
}
defer resp.Body.Close()
if resp.Body == nil {
return errors.New("response body is nil")
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return errors.Wrap(err, "could not unmarshal data")
}
return nil
}
func (c *Client) post(ctx context.Context, endpoint string, data interface{}) (*http.Response, error) {
u, err := url.Parse(c.config.Hostname)
if err != nil {
return nil, errors.Wrap(err, "could not parse url: %s", c.config.Hostname)
}
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
reqUrl := u.String()
jsonData, err := json.Marshal(data)
if err != nil {
return nil, errors.Wrap(err, "lidarr Client could not marshal data: %v", reqUrl)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
if err != nil {
return nil, errors.Wrap(err, "lidarr Client request error: %v", reqUrl)
}
if c.config.BasicAuth {
req.SetBasicAuth(c.config.Username, c.config.Password)
}
req.Header.Add("X-Api-Key", c.config.APIKey)
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
req.Header.Set("User-Agent", "autobrr")
res, err := c.http.Do(req)
if err != nil {
return res, errors.Wrap(err, "lidarr Client request error: %v", reqUrl)
}
// validate response
if res.StatusCode == http.StatusUnauthorized {
return res, errors.New("lidarr: unauthorized: bad credentials")
} else if res.StatusCode != http.StatusOK {
return res, errors.New("lidarr: bad request")
}
// return raw response and let the caller handle json unmarshal of body
return res, nil
}
func (c *Client) postBody(ctx context.Context, endpoint string, data interface{}) (int, []byte, error) {
u, err := url.Parse(c.config.Hostname)
if err != nil {
return 0, nil, errors.Wrap(err, "could not parse url: %s", c.config.Hostname)
}
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
reqUrl := u.String()
jsonData, err := json.Marshal(data)
if err != nil {
return 0, nil, errors.Wrap(err, "lidarr Client could not marshal data: %v", reqUrl)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
if err != nil {
return 0, nil, errors.Wrap(err, "lidarr Client request error: %v", reqUrl)
}
if c.config.BasicAuth {
req.SetBasicAuth(c.config.Username, c.config.Password)
}
c.setHeaders(req)
resp, err := c.http.Do(req)
if err != nil {
return 0, nil, errors.Wrap(err, "lidarr.http.Do(req)")
}
defer resp.Body.Close()
if resp.Body == nil {
return resp.StatusCode, nil, errors.New("response body is nil")
}
var buf bytes.Buffer
if _, err = io.Copy(&buf, resp.Body); err != nil {
return resp.StatusCode, nil, errors.Wrap(err, "lidarr.io.Copy")
}
if resp.StatusCode == http.StatusBadRequest {
return resp.StatusCode, buf.Bytes(), nil
} else if resp.StatusCode < 200 || resp.StatusCode > 401 {
return resp.StatusCode, buf.Bytes(), errors.New("lidarr: bad request: %v (status: %s): %s", resp.Request.RequestURI, resp.Status, buf.String())
}
return resp.StatusCode, buf.Bytes(), nil
}
func (c *Client) setHeaders(req *http.Request) {
if req.Body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("User-Agent", "autobrr")
req.Header.Set("X-Api-Key", c.config.APIKey)
}

147
pkg/arr/lidarr/lidarr.go Normal file
View file

@ -0,0 +1,147 @@
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package lidarr
import (
"context"
"encoding/json"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
)
type Config struct {
Hostname string
APIKey string
// basic auth username and password
BasicAuth bool
Username string
Password string
Log *log.Logger
}
type ClientInterface interface {
Test(ctx context.Context) (*SystemStatusResponse, error)
Push(ctx context.Context, release Release) ([]string, error)
}
type Client struct {
config Config
http *http.Client
Log *log.Logger
}
// New create new lidarr Client
func New(config Config) *Client {
httpClient := &http.Client{
Timeout: time.Second * 120,
Transport: sharedhttp.Transport,
}
c := &Client{
config: config,
http: httpClient,
Log: log.New(io.Discard, "", log.LstdFlags),
}
if config.Log != nil {
c.Log = config.Log
}
return c
}
func (c *Client) Test(ctx context.Context) (*SystemStatusResponse, error) {
status, res, err := c.get(ctx, "system/status")
if err != nil {
return nil, errors.Wrap(err, "lidarr Client get error")
}
if status == http.StatusUnauthorized {
return nil, errors.New("unauthorized: bad credentials")
}
c.Log.Printf("lidarr system/status response status: %v body: %v", status, string(res))
response := SystemStatusResponse{}
err = json.Unmarshal(res, &response)
if err != nil {
return nil, errors.Wrap(err, "lidarr Client error json unmarshal")
}
return &response, nil
}
func (c *Client) Push(ctx context.Context, release Release) ([]string, error) {
status, res, err := c.postBody(ctx, "release/push", release)
if err != nil {
return nil, errors.Wrap(err, "lidarr Client post error")
}
c.Log.Printf("lidarr release/push response status: %v body: %v", status, string(res))
if status == http.StatusBadRequest {
badRequestResponses := make([]*BadRequestResponse, 0)
if err = json.Unmarshal(res, &badRequestResponses); err != nil {
return nil, errors.Wrap(err, "could not unmarshal data")
}
rejections := []string{}
for _, response := range badRequestResponses {
rejections = append(rejections, response.String())
}
return rejections, nil
}
pushResponse := PushResponse{}
if err = json.Unmarshal(res, &pushResponse); err != nil {
return nil, errors.Wrap(err, "lidarr Client error json unmarshal")
}
// log and return if rejected
if pushResponse.Rejected {
rejections := strings.Join(pushResponse.Rejections, ", ")
c.Log.Printf("lidarr release/push rejected %v reasons: %q\n", release.Title, rejections)
return pushResponse.Rejections, nil
}
return nil, nil
}
func (c *Client) GetAlbums(ctx context.Context, mbID int64) ([]Album, error) {
params := make(url.Values)
if mbID != 0 {
params.Set("ForeignAlbumId", strconv.FormatInt(mbID, 10))
}
data := make([]Album, 0)
err := c.getJSON(ctx, "album", params, &data)
if err != nil {
return nil, errors.Wrap(err, "could not get tags")
}
return data, nil
}
func (c *Client) GetArtistByID(ctx context.Context, artistID int64) (*Artist, error) {
var data Artist
err := c.getJSON(ctx, "artist/"+strconv.FormatInt(artistID, 10), nil, &data)
if err != nil {
return nil, errors.Wrap(err, "could not get tags")
}
return &data, nil
}

View file

@ -0,0 +1,187 @@
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
//go:build integration
package lidarr
import (
"context"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
func Test_client_Push(t *testing.T) {
// disable logger
zerolog.SetGlobalLevel(zerolog.Disabled)
mux := http.NewServeMux()
ts := httptest.NewServer(mux)
defer ts.Close()
key := "mock-key"
mux.HandleFunc("/api/v1/release/push", func(w http.ResponseWriter, r *http.Request) {
// request validation logic
apiKey := r.Header.Get("X-Api-Key")
if apiKey != "" {
if apiKey != key {
w.WriteHeader(http.StatusUnauthorized)
w.Write(nil)
return
}
}
// read json response
jsonPayload, _ := os.ReadFile("testdata/release_push_response.json")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonPayload)
})
type fields struct {
config Config
}
type args struct {
release Release
}
tests := []struct {
name string
fields fields
args args
err error
rejections []string
wantErr bool
}{
{
name: "push",
fields: fields{
config: Config{
Hostname: ts.URL,
APIKey: "",
BasicAuth: false,
Username: "",
Password: "",
},
},
args: args{release: Release{
Title: "JR Get Money - Nobody But You [2008] [Single] - FLAC / Lossless / Log / 100% / Cue / CD",
DownloadUrl: "https://www.test.org/rss/download/0000001/00000000000000000000/That Show S01 2160p ATVP WEB-DL DDP 5.1 Atmos DV HEVC-NOGROUP.torrent",
Size: 0,
Indexer: "test",
DownloadProtocol: "torrent",
Protocol: "torrent",
PublishDate: "2021-08-21T15:36:00Z",
}},
rejections: []string{"Unknown Artist"},
},
{
name: "push_error",
fields: fields{
config: Config{
Hostname: ts.URL,
APIKey: key,
BasicAuth: false,
Username: "",
Password: "",
},
},
args: args{release: Release{
Title: "JR Get Money - Nobody But You [2008] [Single] - FLAC / Lossless / Log / 100% / Cue / CD",
DownloadUrl: "https://www.test.org/rss/download/0000001/00000000000000000000/That Show S01 2160p ATVP WEB-DL DDP 5.1 Atmos DV HEVC-NOGROUP.torrent",
Size: 0,
Indexer: "test",
DownloadProtocol: "torrent",
Protocol: "torrent",
PublishDate: "2021-08-21T15:36:00Z",
}},
rejections: []string{"Unknown Artist"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := New(tt.fields.config)
rejections, err := c.Push(context.Background(), tt.args.release)
assert.Equal(t, tt.rejections, rejections)
if tt.wantErr && assert.Error(t, err) {
assert.Equal(t, tt.err, err)
}
})
}
}
func Test_client_Test(t *testing.T) {
// disable logger
zerolog.SetGlobalLevel(zerolog.Disabled)
key := "mock-key"
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apiKey := r.Header.Get("X-Api-Key")
if apiKey != "" {
if apiKey != key {
w.WriteHeader(http.StatusUnauthorized)
w.Write(nil)
return
}
}
jsonPayload, _ := os.ReadFile("testdata/system_status_response.json")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonPayload)
}))
defer srv.Close()
tests := []struct {
name string
cfg Config
want *SystemStatusResponse
expectedErr string
wantErr bool
}{
{
name: "fetch",
cfg: Config{
Hostname: srv.URL,
APIKey: key,
BasicAuth: false,
Username: "",
Password: "",
},
want: &SystemStatusResponse{Version: "0.8.1.2135"},
expectedErr: "",
wantErr: false,
},
{
name: "fetch_unauthorized",
cfg: Config{
Hostname: srv.URL,
APIKey: "bad-mock-key",
BasicAuth: false,
Username: "",
Password: "",
},
want: nil,
wantErr: true,
expectedErr: "unauthorized: bad credentials",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := New(tt.cfg)
got, err := c.Test(context.Background())
if tt.wantErr && assert.Error(t, err) {
assert.EqualErrorf(t, err, tt.expectedErr, "Error should be: %v, got: %v", tt.wantErr, err)
}
assert.Equal(t, tt.want, got)
})
}
}

View file

@ -0,0 +1,39 @@
{
"guid": "PUSH-https://localhost/test/download.torrent",
"quality": {
"quality": {
"id": 6,
"name": "FLAC"
},
"revision": {
"version": 1,
"real": 0,
"isRepack": false
}
},
"qualityWeight": 1,
"age": 1301,
"ageHours": 31240.51104018011,
"ageMinutes": 1874430.6624115533,
"size": 0,
"indexerId": 0,
"indexer": "test",
"releaseHash": "",
"title": "JR Get Money - Nobody But You [2008] [Single] - FLAC / Lossless / Log / 100% / Cue / CD",
"discography": false,
"sceneSource": false,
"artistName": "JR Get Money",
"albumTitle": "Nobody But You",
"approved": false,
"temporarilyRejected": false,
"rejected": true,
"rejections": [
"Unknown Artist"
],
"publishDate": "2018-01-28T07:00:00Z",
"downloadUrl": "https://localhost/test/download.torrent",
"downloadAllowed": false,
"releaseWeight": 0,
"preferredWordScore": 0,
"protocol": "torrent"
}

View file

@ -0,0 +1,28 @@
{
"version": "0.8.1.2135",
"buildTime": "2021-04-18T15:43:22Z",
"isDebug": false,
"isProduction": true,
"isAdmin": false,
"isUserInteractive": false,
"startupPath": "/opt/Lidarr",
"appData": "/home/test/.config/Lidarr",
"osName": "debian",
"osVersion": "10",
"isNetCore": true,
"isMono": false,
"isLinux": true,
"isOsx": false,
"isWindows": false,
"isDocker": false,
"mode": "console",
"branch": "master",
"authentication": "none",
"sqliteVersion": "3.27.2",
"migrationVersion": 45,
"urlBase": "/lidarr",
"runtimeVersion": "3.1.13",
"runtimeName": "netCore",
"startTime": "2021-08-21T23:18:16.4948193Z",
"packageUpdateMechanism": "builtIn"
}

150
pkg/arr/lidarr/types.go Normal file
View file

@ -0,0 +1,150 @@
package lidarr
import (
"fmt"
"time"
"github.com/autobrr/autobrr/pkg/arr"
)
type Release struct {
Title string `json:"title"`
InfoUrl string `json:"infoUrl,omitempty"`
DownloadUrl string `json:"downloadUrl,omitempty"`
MagnetUrl string `json:"magnetUrl,omitempty"`
Size uint64 `json:"size"`
Indexer string `json:"indexer"`
DownloadProtocol string `json:"downloadProtocol"`
Protocol string `json:"protocol"`
PublishDate string `json:"publishDate"`
DownloadClientId int `json:"downloadClientId,omitempty"`
DownloadClient string `json:"downloadClient,omitempty"`
}
type PushResponse struct {
Approved bool `json:"approved"`
Rejected bool `json:"rejected"`
TempRejected bool `json:"temporarilyRejected"`
Rejections []string `json:"rejections"`
}
type BadRequestResponse struct {
PropertyName string `json:"propertyName"`
ErrorMessage string `json:"errorMessage"`
ErrorCode string `json:"errorCode"`
AttemptedValue string `json:"attemptedValue"`
Severity string `json:"severity"`
}
func (r BadRequestResponse) String() string {
return fmt.Sprintf("[%s: %s] %s: %s - got value: %s", r.Severity, r.ErrorCode, r.PropertyName, r.ErrorMessage, r.AttemptedValue)
}
type SystemStatusResponse struct {
Version string `json:"version"`
}
type Statistics struct {
AlbumCount int `json:"albumCount,omitempty"`
TrackFileCount int `json:"trackFileCount"`
TrackCount int `json:"trackCount"`
TotalTrackCount int `json:"totalTrackCount"`
SizeOnDisk int `json:"sizeOnDisk"`
PercentOfTracks float64 `json:"percentOfTracks"`
}
type Artist struct {
ID int64 `json:"id"`
Status string `json:"status,omitempty"`
LastInfoSync time.Time `json:"lastInfoSync,omitempty"`
ArtistName string `json:"artistName,omitempty"`
ForeignArtistID string `json:"foreignArtistId,omitempty"`
TadbID int64 `json:"tadbId,omitempty"`
DiscogsID int64 `json:"discogsId,omitempty"`
QualityProfileID int64 `json:"qualityProfileId,omitempty"`
MetadataProfileID int64 `json:"metadataProfileId,omitempty"`
Overview string `json:"overview,omitempty"`
ArtistType string `json:"artistType,omitempty"`
Disambiguation string `json:"disambiguation,omitempty"`
RootFolderPath string `json:"rootFolderPath,omitempty"`
Path string `json:"path,omitempty"`
CleanName string `json:"cleanName,omitempty"`
SortName string `json:"sortName,omitempty"`
Links []*arr.Link `json:"links,omitempty"`
Images []*arr.Image `json:"images,omitempty"`
Genres []string `json:"genres,omitempty"`
Tags []int `json:"tags,omitempty"`
Added time.Time `json:"added,omitempty"`
Ratings *arr.Ratings `json:"ratings,omitempty"`
Statistics *Statistics `json:"statistics,omitempty"`
LastAlbum *Album `json:"lastAlbum,omitempty"`
NextAlbum *Album `json:"nextAlbum,omitempty"`
AddOptions *ArtistAddOptions `json:"addOptions,omitempty"`
AlbumFolder bool `json:"albumFolder,omitempty"`
Monitored bool `json:"monitored"`
Ended bool `json:"ended,omitempty"`
}
type Album struct {
ID int64 `json:"id,omitempty"`
Title string `json:"title"`
Disambiguation string `json:"disambiguation"`
Overview string `json:"overview"`
ArtistID int64 `json:"artistId"`
ForeignAlbumID string `json:"foreignAlbumId"`
ProfileID int64 `json:"profileId"`
Duration int `json:"duration"`
AlbumType string `json:"albumType"`
SecondaryTypes []interface{} `json:"secondaryTypes"`
MediumCount int `json:"mediumCount"`
Ratings *arr.Ratings `json:"ratings"`
ReleaseDate time.Time `json:"releaseDate"`
Releases []*AlbumRelease `json:"releases"`
Genres []interface{} `json:"genres"`
Media []*Media `json:"media"`
Artist *Artist `json:"artist"`
Links []*arr.Link `json:"links"`
Images []*arr.Image `json:"images"`
Statistics *Statistics `json:"statistics"`
RemoteCover string `json:"remoteCover,omitempty"`
AddOptions *AlbumAddOptions `json:"addOptions,omitempty"`
Monitored bool `json:"monitored"`
AnyReleaseOk bool `json:"anyReleaseOk"`
Grabbed bool `json:"grabbed"`
}
// Release is part of an Album.
type AlbumRelease struct {
ID int64 `json:"id"`
AlbumID int64 `json:"albumId"`
ForeignReleaseID string `json:"foreignReleaseId"`
Title string `json:"title"`
Status string `json:"status"`
Duration int `json:"duration"`
TrackCount int `json:"trackCount"`
Media []*Media `json:"media"`
MediumCount int `json:"mediumCount"`
Disambiguation string `json:"disambiguation"`
Country []string `json:"country"`
Label []string `json:"label"`
Format string `json:"format"`
Monitored bool `json:"monitored"`
}
// Media is part of an Album.
type Media struct {
MediumNumber int64 `json:"mediumNumber"`
MediumName string `json:"mediumName"`
MediumFormat string `json:"mediumFormat"`
}
// ArtistAddOptions is part of an artist and an album.
type ArtistAddOptions struct {
Monitor string `json:"monitor,omitempty"`
Monitored bool `json:"monitored,omitempty"`
SearchForMissingAlbums bool `json:"searchForMissingAlbums,omitempty"`
}
type AlbumAddOptions struct {
SearchForNewAlbum bool `json:"searchForNewAlbum,omitempty"`
}