golift.io/starr@v1.0.0/lidarr/artist.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 bpArtist = APIver + "/artist"
    16  
    17  // Artist represents the /api/v1/artist endpoint, and it's part of an Album.
    18  type Artist struct {
    19  	ID                int64             `json:"id"`
    20  	Status            string            `json:"status,omitempty"`
    21  	LastInfoSync      time.Time         `json:"lastInfoSync,omitempty"`
    22  	ArtistName        string            `json:"artistName,omitempty"`
    23  	ForeignArtistID   string            `json:"foreignArtistId,omitempty"`
    24  	TadbID            int64             `json:"tadbId,omitempty"`
    25  	DiscogsID         int64             `json:"discogsId,omitempty"`
    26  	QualityProfileID  int64             `json:"qualityProfileId,omitempty"`
    27  	MetadataProfileID int64             `json:"metadataProfileId,omitempty"`
    28  	Overview          string            `json:"overview,omitempty"`
    29  	ArtistType        string            `json:"artistType,omitempty"`
    30  	Disambiguation    string            `json:"disambiguation,omitempty"`
    31  	RootFolderPath    string            `json:"rootFolderPath,omitempty"`
    32  	Path              string            `json:"path,omitempty"`
    33  	CleanName         string            `json:"cleanName,omitempty"`
    34  	SortName          string            `json:"sortName,omitempty"`
    35  	Links             []*starr.Link     `json:"links,omitempty"`
    36  	Images            []*starr.Image    `json:"images,omitempty"`
    37  	Genres            []string          `json:"genres,omitempty"`
    38  	Tags              []int             `json:"tags,omitempty"`
    39  	Added             time.Time         `json:"added,omitempty"`
    40  	Ratings           *starr.Ratings    `json:"ratings,omitempty"`
    41  	Statistics        *Statistics       `json:"statistics,omitempty"`
    42  	LastAlbum         *Album            `json:"lastAlbum,omitempty"`
    43  	NextAlbum         *Album            `json:"nextAlbum,omitempty"`
    44  	AddOptions        *ArtistAddOptions `json:"addOptions,omitempty"`
    45  	AlbumFolder       bool              `json:"albumFolder,omitempty"`
    46  	Monitored         bool              `json:"monitored"`
    47  	Ended             bool              `json:"ended,omitempty"`
    48  }
    49  
    50  // Statistics is part of Artist and Album.
    51  type Statistics struct {
    52  	AlbumCount      int     `json:"albumCount,omitempty"`
    53  	TrackFileCount  int     `json:"trackFileCount"`
    54  	TrackCount      int     `json:"trackCount"`
    55  	TotalTrackCount int     `json:"totalTrackCount"`
    56  	SizeOnDisk      int     `json:"sizeOnDisk"`
    57  	PercentOfTracks float64 `json:"percentOfTracks"`
    58  }
    59  
    60  // GetArtist returns an artist or all artists.
    61  func (l *Lidarr) GetArtist(mbID string) ([]*Artist, error) {
    62  	return l.GetArtistContext(context.Background(), mbID)
    63  }
    64  
    65  // GetArtistContext returns an artist or all artists.
    66  func (l *Lidarr) GetArtistContext(ctx context.Context, mbID string) ([]*Artist, error) {
    67  	req := starr.Request{URI: bpArtist, Query: make(url.Values)}
    68  	if mbID != "" {
    69  		req.Query.Add("mbId", mbID)
    70  	}
    71  
    72  	var output []*Artist
    73  
    74  	if err := l.GetInto(ctx, req, &output); err != nil {
    75  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    76  	}
    77  
    78  	return output, nil
    79  }
    80  
    81  // GetArtistByID returns an artist from an ID.
    82  func (l *Lidarr) GetArtistByID(artistID int64) (*Artist, error) {
    83  	return l.GetArtistByIDContext(context.Background(), artistID)
    84  }
    85  
    86  // GetArtistByIDContext returns an artist from an ID.
    87  func (l *Lidarr) GetArtistByIDContext(ctx context.Context, artistID int64) (*Artist, error) {
    88  	var output Artist
    89  
    90  	req := starr.Request{URI: path.Join(bpArtist, fmt.Sprint(artistID))}
    91  	if err := l.GetInto(ctx, req, &output); err != nil {
    92  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    93  	}
    94  
    95  	return &output, nil
    96  }
    97  
    98  // AddArtist adds a new artist to Lidarr, and probably does not yet work.
    99  func (l *Lidarr) AddArtist(artist *Artist) (*Artist, error) {
   100  	return l.AddArtistContext(context.Background(), artist)
   101  }
   102  
   103  // AddArtistContext adds a new artist to Lidarr, and probably does not yet work.
   104  func (l *Lidarr) AddArtistContext(ctx context.Context, artist *Artist) (*Artist, error) {
   105  	var body bytes.Buffer
   106  	if err := json.NewEncoder(&body).Encode(artist); err != nil {
   107  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpArtist, err)
   108  	}
   109  
   110  	req := starr.Request{URI: bpArtist, Query: make(url.Values), Body: &body}
   111  
   112  	var output Artist
   113  	if err := l.PostInto(ctx, req, &output); err != nil {
   114  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
   115  	}
   116  
   117  	return &output, nil
   118  }
   119  
   120  // UpdateArtist updates an artist in place.
   121  func (l *Lidarr) UpdateArtist(artist *Artist, moveFiles bool) (*Artist, error) {
   122  	return l.UpdateArtistContext(context.Background(), artist, moveFiles)
   123  }
   124  
   125  // UpdateArtistContext updates an artist in place.
   126  func (l *Lidarr) UpdateArtistContext(ctx context.Context, artist *Artist, moveFiles bool) (*Artist, error) {
   127  	var body bytes.Buffer
   128  	if err := json.NewEncoder(&body).Encode(artist); err != nil {
   129  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpArtist, err)
   130  	}
   131  
   132  	req := starr.Request{URI: path.Join(bpArtist, fmt.Sprint(artist.ID)), Query: make(url.Values), Body: &body}
   133  	req.Query.Add("moveFiles", fmt.Sprint(moveFiles))
   134  
   135  	var output Artist
   136  	if err := l.PutInto(ctx, req, &output); err != nil {
   137  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   138  	}
   139  
   140  	return &output, nil
   141  }
   142  
   143  // DeleteArtist removes an artist from the database.
   144  // Setting deleteFiles true will delete all content for the artist.
   145  func (l *Lidarr) DeleteArtist(artistID int64, deleteFiles, addImportExclusion bool) error {
   146  	return l.DeleteArtistContext(context.Background(), artistID, deleteFiles, addImportExclusion)
   147  }
   148  
   149  // DeleteArtistContext removes an artist from the database.
   150  // Setting deleteFiles true will delete all content for the artist.
   151  func (l *Lidarr) DeleteArtistContext(ctx context.Context, artistID int64, deleteFiles, addImportExclusion bool) error {
   152  	req := starr.Request{URI: path.Join(bpArtist, fmt.Sprint(artistID)), Query: make(url.Values)}
   153  	req.Query.Set("deleteFiles", fmt.Sprint(deleteFiles))
   154  	req.Query.Set("addImportListExclusion", fmt.Sprint(addImportExclusion))
   155  
   156  	if err := l.DeleteAny(ctx, req); err != nil {
   157  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   158  	}
   159  
   160  	return nil
   161  }