golift.io/starr@v1.0.0/lidarr/album.go (about)

     1  package lidarr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"net/url"
     9  	"path"
    10  	"time"
    11  
    12  	"golift.io/starr"
    13  )
    14  
    15  const bpAlbum = APIver + "/album"
    16  
    17  // Album is the /api/v1/album endpoint.
    18  type Album struct {
    19  	ID             int64            `json:"id,omitempty"`
    20  	Title          string           `json:"title"`
    21  	Disambiguation string           `json:"disambiguation"`
    22  	Overview       string           `json:"overview"`
    23  	ArtistID       int64            `json:"artistId"`
    24  	ForeignAlbumID string           `json:"foreignAlbumId"`
    25  	ProfileID      int64            `json:"profileId"`
    26  	Duration       int              `json:"duration"`
    27  	AlbumType      string           `json:"albumType"`
    28  	SecondaryTypes []interface{}    `json:"secondaryTypes"`
    29  	MediumCount    int              `json:"mediumCount"`
    30  	Ratings        *starr.Ratings   `json:"ratings"`
    31  	ReleaseDate    time.Time        `json:"releaseDate"`
    32  	Releases       []*Release       `json:"releases"`
    33  	Genres         []string         `json:"genres"`
    34  	Media          []*Media         `json:"media"`
    35  	Artist         *Artist          `json:"artist"`
    36  	Links          []*starr.Link    `json:"links"`
    37  	Images         []*starr.Image   `json:"images"`
    38  	Statistics     *Statistics      `json:"statistics"`
    39  	RemoteCover    string           `json:"remoteCover,omitempty"`
    40  	AddOptions     *AlbumAddOptions `json:"addOptions,omitempty"`
    41  	Monitored      bool             `json:"monitored"`
    42  	AnyReleaseOk   bool             `json:"anyReleaseOk"`
    43  	Grabbed        bool             `json:"grabbed"`
    44  }
    45  
    46  // Release is part of an Album.
    47  type Release struct {
    48  	ID               int64    `json:"id"`
    49  	AlbumID          int64    `json:"albumId"`
    50  	ForeignReleaseID string   `json:"foreignReleaseId"`
    51  	Title            string   `json:"title"`
    52  	Status           string   `json:"status"`
    53  	Duration         int      `json:"duration"`
    54  	TrackCount       int      `json:"trackCount"`
    55  	Media            []*Media `json:"media"`
    56  	MediumCount      int      `json:"mediumCount"`
    57  	Disambiguation   string   `json:"disambiguation"`
    58  	Country          []string `json:"country"`
    59  	Label            []string `json:"label"`
    60  	Format           string   `json:"format"`
    61  	Monitored        bool     `json:"monitored"`
    62  }
    63  
    64  // Media is part of an Album.
    65  type Media struct {
    66  	MediumNumber int64  `json:"mediumNumber"`
    67  	MediumName   string `json:"mediumName"`
    68  	MediumFormat string `json:"mediumFormat"`
    69  }
    70  
    71  // ArtistAddOptions is part of an artist and an album.
    72  type ArtistAddOptions struct {
    73  	Monitor                string `json:"monitor,omitempty"`
    74  	Monitored              bool   `json:"monitored,omitempty"`
    75  	SearchForMissingAlbums bool   `json:"searchForMissingAlbums,omitempty"`
    76  }
    77  
    78  // AddAlbumInput is currently unknown.
    79  type AddAlbumInput struct {
    80  	ForeignAlbumID string                  `json:"foreignAlbumId"`
    81  	Monitored      bool                    `json:"monitored"`
    82  	Releases       []*AddAlbumInputRelease `json:"releases"`
    83  	AddOptions     *AlbumAddOptions        `json:"addOptions"`
    84  	Artist         *Artist                 `json:"artist"`
    85  }
    86  
    87  // AddAlbumInputRelease is part of AddAlbumInput.
    88  type AddAlbumInputRelease struct {
    89  	ForeignReleaseID string   `json:"foreignReleaseId"`
    90  	Title            string   `json:"title"`
    91  	Media            []*Media `json:"media"`
    92  	Monitored        bool     `json:"monitored"`
    93  }
    94  
    95  // AlbumAddOptions is part of an Album.
    96  type AlbumAddOptions struct {
    97  	SearchForNewAlbum bool `json:"searchForNewAlbum,omitempty"`
    98  }
    99  
   100  // GetAlbum returns an album or all albums if mbID is "" (empty).
   101  // mbID is the music brainz UUID for a "release-group".
   102  func (l *Lidarr) GetAlbum(mbID string) ([]*Album, error) {
   103  	return l.GetAlbumContext(context.Background(), mbID)
   104  }
   105  
   106  // GetAlbumContext returns an album or all albums if mbID is "" (empty).
   107  // mbID is the music brainz UUID for a "release-group".
   108  func (l *Lidarr) GetAlbumContext(ctx context.Context, mbID string) ([]*Album, error) {
   109  	req := starr.Request{Query: make(url.Values), URI: bpAlbum}
   110  	if mbID != "" {
   111  		req.Query.Add("ForeignAlbumId", mbID)
   112  	}
   113  
   114  	var output []*Album
   115  
   116  	if err := l.GetInto(ctx, req, &output); err != nil {
   117  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
   118  	}
   119  
   120  	return output, nil
   121  }
   122  
   123  // GetAlbumByID returns an album by DB ID.
   124  func (l *Lidarr) GetAlbumByID(albumID int64) (*Album, error) {
   125  	return l.GetAlbumByIDContext(context.Background(), albumID)
   126  }
   127  
   128  // GetAlbumByIDContext returns an album by DB ID.
   129  func (l *Lidarr) GetAlbumByIDContext(ctx context.Context, albumID int64) (*Album, error) {
   130  	var output Album
   131  
   132  	req := starr.Request{URI: path.Join(bpAlbum, fmt.Sprint(albumID))}
   133  	if err := l.GetInto(ctx, req, &output); err != nil {
   134  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
   135  	}
   136  
   137  	return &output, nil
   138  }
   139  
   140  // UpdateAlbum updates an album in place; the output of this is currently unknown!!!!
   141  func (l *Lidarr) UpdateAlbum(albumID int64, album *Album, moveFiles bool) (*Album, error) {
   142  	return l.UpdateAlbumContext(context.Background(), albumID, album, moveFiles)
   143  }
   144  
   145  // UpdateAlbumContext updates an album in place; the output of this is currently unknown!!!!
   146  func (l *Lidarr) UpdateAlbumContext(ctx context.Context, albumID int64, album *Album, moveFiles bool) (*Album, error) {
   147  	var body bytes.Buffer
   148  	if err := json.NewEncoder(&body).Encode(album); err != nil {
   149  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpAlbum, err)
   150  	}
   151  
   152  	var output Album
   153  
   154  	req := starr.Request{
   155  		URI:   path.Join(bpAlbum, fmt.Sprint(albumID)),
   156  		Query: make(url.Values),
   157  		Body:  &body,
   158  	}
   159  	req.Query.Add("moveFiles", fmt.Sprint(moveFiles))
   160  
   161  	if err := l.PutInto(ctx, req, &output); err != nil {
   162  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   163  	}
   164  
   165  	return &output, nil
   166  }
   167  
   168  // AddAlbum adds a new album to Lidarr, and probably does not yet work.
   169  func (l *Lidarr) AddAlbum(album *AddAlbumInput) (*Album, error) {
   170  	return l.AddAlbumContext(context.Background(), album)
   171  }
   172  
   173  // AddAlbumContext adds a new album to Lidarr, and probably does not yet work.
   174  func (l *Lidarr) AddAlbumContext(ctx context.Context, album *AddAlbumInput) (*Album, error) {
   175  	if album.Releases == nil {
   176  		album.Releases = make([]*AddAlbumInputRelease, 0)
   177  	}
   178  
   179  	var body bytes.Buffer
   180  	if err := json.NewEncoder(&body).Encode(album); err != nil {
   181  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpAlbum, err)
   182  	}
   183  
   184  	req := starr.Request{
   185  		URI:   bpAlbum,
   186  		Query: make(url.Values),
   187  		Body:  &body,
   188  	}
   189  
   190  	var output Album
   191  	if err := l.PostInto(ctx, req, &output); err != nil {
   192  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
   193  	}
   194  
   195  	return &output, nil
   196  }
   197  
   198  // Lookup will search for albums matching the specified search term.
   199  func (l *Lidarr) Lookup(term string) ([]*Album, error) {
   200  	return l.LookupContext(context.Background(), term)
   201  }
   202  
   203  // LookupContext will search for albums matching the specified search term.
   204  func (l *Lidarr) LookupContext(ctx context.Context, term string) ([]*Album, error) {
   205  	var output []*Album
   206  
   207  	if term == "" {
   208  		return output, nil
   209  	}
   210  
   211  	req := starr.Request{
   212  		URI:   path.Join(bpAlbum, "lookup"),
   213  		Query: make(url.Values),
   214  	}
   215  	req.Query.Set("term", term)
   216  
   217  	err := l.GetInto(ctx, req, &output)
   218  	if err != nil {
   219  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
   220  	}
   221  
   222  	return output, nil
   223  }
   224  
   225  // DeleteAlbum removes an album from the database.
   226  // Setting deleteFiles true will delete all content for the album.
   227  func (l *Lidarr) DeleteAlbum(albumID int64, deleteFiles, addImportExclusion bool) error {
   228  	return l.DeleteAlbumContext(context.Background(), albumID, deleteFiles, addImportExclusion)
   229  }
   230  
   231  // DeleteAlbumContext removes an album from the database.
   232  // Setting deleteFiles true will delete all content for the album.
   233  func (l *Lidarr) DeleteAlbumContext(ctx context.Context, albumID int64, deleteFiles, addImportExclusion bool) error {
   234  	req := starr.Request{URI: path.Join(bpAlbum, fmt.Sprint(albumID)), Query: make(url.Values)}
   235  	req.Query.Set("deleteFiles", fmt.Sprint(deleteFiles))
   236  	req.Query.Set("addImportListExclusion", fmt.Sprint(addImportExclusion))
   237  
   238  	if err := l.DeleteAny(ctx, req); err != nil {
   239  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   240  	}
   241  
   242  	return nil
   243  }