golift.io/starr@v1.0.0/lidarr/trackfile.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 bpTrackFile = APIver + "/trackfile"
    16  
    17  // TrackFile represents the data sent to and returned from the trackfile endpoint.
    18  type TrackFile struct {
    19  	ID            int64          `json:"id"`
    20  	ArtistID      int64          `json:"artistId"`
    21  	AlbumID       int64          `json:"albumId"`
    22  	Path          string         `json:"path"`
    23  	Size          int64          `json:"size"`
    24  	DateAdded     time.Time      `json:"dateAdded"`
    25  	Quality       *starr.Quality `json:"quality"`
    26  	QualityWeight int            `json:"qualityWeight"`
    27  	MediaInfo     MediaInfo      `json:"mediaInfo"`
    28  	CutoffNotMet  bool           `json:"qualityCutoffNotMet"`
    29  	AudioTags     *AudioTags     `json:"audioTags"`
    30  }
    31  
    32  // MediaInfo is part of a TrackFile.
    33  type MediaInfo struct {
    34  	ID              int64  `json:"id"`
    35  	AudioChannels   int    `json:"audioChannels"`
    36  	AudioBitRate    string `json:"audioBitRate"`
    37  	AudioCodec      string `json:"audioCodec"`
    38  	AudioBits       string `json:"audioBits"`
    39  	AudioSampleRate string `json:"audioSampleRate"`
    40  }
    41  
    42  /* I've never seen audio tags in the wild. */
    43  
    44  // AudioTags is (optionally) part of a TrackFile.
    45  type AudioTags struct {
    46  	Title           string           `json:"title"`
    47  	CleanTitle      string           `json:"cleanTitle"`
    48  	ArtistTitle     string           `json:"artistTitle"`
    49  	AlbumTitle      string           `json:"albumTitle"`
    50  	ArtistTitleInfo *ArtistTitleInfo `json:"artistTitleInfo"`
    51  	ArtistMBID      string           `json:"artistMBId"`
    52  	AlbumMBID       string           `json:"albumMBId"`
    53  	ReleaseMBID     string           `json:"releaseMBId"`
    54  	RecordingMBID   string           `json:"recordingMBId"`
    55  	TrackMBID       string           `json:"trackMBId"`
    56  	DiscNumber      int              `json:"discNumber"`
    57  	DiscCount       int              `json:"discCount"`
    58  	Country         *AudioCountry    `json:"country"`
    59  	Year            int              `json:"year"`
    60  	Label           string           `json:"label"`
    61  	CatalogNumber   string           `json:"catalogNumber"`
    62  	Disambiguation  string           `json:"disambiguation"`
    63  	Duration        *starr.TimeSpan  `json:"duration"`
    64  	Quality         *starr.Quality   `json:"quality"`
    65  	MediaInfo       *AudioMediaInfo  `json:"mediaInfo"`
    66  	TrackNumbers    []int            `json:"trackNumbers"`
    67  	ReleaseGroup    string           `json:"releaseGroup"`
    68  	ReleaseHash     string           `json:"releaseHash"`
    69  }
    70  
    71  // AudioMediaInfo is part of AudioTags.
    72  type AudioMediaInfo struct {
    73  	AudioFormat     string `json:"audioFormat"`
    74  	AudioBitrate    int64  `json:"audioBitrate"`
    75  	AudioChannels   int    `json:"audioChannels"`
    76  	AudioBits       int    `json:"audioBits"`
    77  	AudioSampleRate int    `json:"audioSampleRate"`
    78  }
    79  
    80  // AudioCountry is part of AudioTags.
    81  type AudioCountry struct {
    82  	TwoLetterCode string `json:"twoLetterCode"`
    83  	Name          string `json:"name"`
    84  }
    85  
    86  // ArtistTitleInfo is part of AudioTags.
    87  type ArtistTitleInfo struct {
    88  	Title            string `json:"title"`
    89  	TitleWithoutYear string `json:"titleWithoutYear"`
    90  	Year             int    `json:"year"`
    91  }
    92  
    93  // GetTrackFilesForArtist returns the track files for an artist.
    94  func (l *Lidarr) GetTrackFilesForArtist(artistID int64) ([]*TrackFile, error) {
    95  	return l.GetTrackFilesForArtistContext(context.Background(), artistID)
    96  }
    97  
    98  // GetTrackFilesForArtistContext returns the track files for an artist.
    99  func (l *Lidarr) GetTrackFilesForArtistContext(ctx context.Context, artistID int64) ([]*TrackFile, error) {
   100  	var output []*TrackFile
   101  
   102  	req := starr.Request{URI: bpTrackFile, Query: make(url.Values)}
   103  	req.Query.Add("artistId", fmt.Sprint(artistID))
   104  
   105  	if err := l.GetInto(ctx, req, &output); err != nil {
   106  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
   107  	}
   108  
   109  	return output, nil
   110  }
   111  
   112  // GetTrackFilesForAlbum returns the track files for an album.
   113  func (l *Lidarr) GetTrackFilesForAlbum(albumID int64) ([]*TrackFile, error) {
   114  	return l.GetTrackFilesForAlbumContext(context.Background(), albumID)
   115  }
   116  
   117  // GetTrackFilesForAlbumContext returns the track files for an album.
   118  func (l *Lidarr) GetTrackFilesForAlbumContext(ctx context.Context, albumID int64) ([]*TrackFile, error) {
   119  	var output []*TrackFile
   120  
   121  	req := starr.Request{URI: bpTrackFile, Query: make(url.Values)}
   122  	req.Query.Add("albumId", fmt.Sprint(albumID))
   123  
   124  	if err := l.GetInto(ctx, req, &output); err != nil {
   125  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
   126  	}
   127  
   128  	return output, nil
   129  }
   130  
   131  // GetTrackFiles returns the requested track files by ID.
   132  func (l *Lidarr) GetTrackFiles(trackFileIDs []int64) ([]*TrackFile, error) {
   133  	return l.GetTrackFilesContext(context.Background(), trackFileIDs)
   134  }
   135  
   136  // GetTrackFilesContext returns the requested track files by their IDs.
   137  func (l *Lidarr) GetTrackFilesContext(ctx context.Context, trackFileIDs []int64) ([]*TrackFile, error) {
   138  	var output []*TrackFile
   139  
   140  	if len(trackFileIDs) == 0 {
   141  		return output, nil
   142  	}
   143  
   144  	req := starr.Request{
   145  		URI:   bpTrackFile,
   146  		Query: url.Values{"trackFileIds": make([]string, len(trackFileIDs))},
   147  	}
   148  
   149  	for idx, fileID := range trackFileIDs {
   150  		req.Query["trackFileIds"][idx] = fmt.Sprint(fileID)
   151  	}
   152  
   153  	if err := l.GetInto(ctx, req, &output); err != nil {
   154  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
   155  	}
   156  
   157  	return output, nil
   158  }
   159  
   160  // UpdateTrackFile updates a track file.
   161  func (l *Lidarr) UpdateTrackFile(trackFile *TrackFile) (*TrackFile, error) {
   162  	return l.UpdateTrackFileContext(context.Background(), trackFile)
   163  }
   164  
   165  // UpdateTrackFileContext updates a track file.
   166  func (l *Lidarr) UpdateTrackFileContext(ctx context.Context, trackFile *TrackFile) (*TrackFile, error) {
   167  	var output TrackFile
   168  
   169  	var body bytes.Buffer
   170  	if err := json.NewEncoder(&body).Encode(trackFile); err != nil {
   171  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpTrackFile, err)
   172  	}
   173  
   174  	req := starr.Request{URI: path.Join(bpTrackFile, fmt.Sprint(trackFile.ID)), Body: &body}
   175  	if err := l.PutInto(ctx, req, &output); err != nil {
   176  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   177  	}
   178  
   179  	return &output, nil
   180  }
   181  
   182  // DeleteTrackFile deletes a track file.
   183  func (l *Lidarr) DeleteTrackFile(trackFileID int64) error {
   184  	return l.DeleteTrackFileContext(context.Background(), trackFileID)
   185  }
   186  
   187  // DeleteTrackFileContext deletes a track file.
   188  func (l *Lidarr) DeleteTrackFileContext(ctx context.Context, trackFileID int64) error {
   189  	req := starr.Request{URI: path.Join(bpTrackFile, fmt.Sprint(trackFileID))}
   190  	if err := l.DeleteAny(ctx, req); err != nil {
   191  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   192  	}
   193  
   194  	return nil
   195  }
   196  
   197  // DeleteTrackFiles bulk deletes track files by their IDs.
   198  func (l *Lidarr) DeleteTrackFiles(trackFileIDs []int64) error {
   199  	return l.DeleteTrackFilesContext(context.Background(), trackFileIDs)
   200  }
   201  
   202  // DeleteTrackFilesContext bulk deletes track files by their IDs.
   203  func (l *Lidarr) DeleteTrackFilesContext(ctx context.Context, trackFileIDs []int64) error {
   204  	postData := struct {
   205  		T []int64 `json:"trackFileIDs"`
   206  	}{trackFileIDs}
   207  
   208  	var body bytes.Buffer
   209  	if err := json.NewEncoder(&body).Encode(&postData); err != nil {
   210  		return fmt.Errorf("json.Marshal(%s): %w", bpTrackFile, err)
   211  	}
   212  
   213  	req := starr.Request{URI: path.Join(bpTrackFile, "bulk"), Body: &body}
   214  	if err := l.DeleteAny(ctx, req); err != nil {
   215  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   216  	}
   217  
   218  	return nil
   219  }