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

     1  package lidarr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"path"
     8  	"time"
     9  
    10  	"golift.io/starr"
    11  )
    12  
    13  const bpHistory = APIver + "/history"
    14  
    15  // History represents the /api/v1/history endpoint.
    16  type History struct {
    17  	Page          int              `json:"page"`
    18  	PageSize      int              `json:"pageSize"`
    19  	SortKey       string           `json:"sortKey"`
    20  	SortDirection string           `json:"sortDirection"`
    21  	TotalRecords  int              `json:"totalRecords"`
    22  	Records       []*HistoryRecord `json:"records"`
    23  }
    24  
    25  // HistoryRecord is part of the history. Not all items have all Data members.
    26  // Check EventType for events you need.
    27  type HistoryRecord struct {
    28  	ID                  int64          `json:"id"`
    29  	AlbumID             int64          `json:"albumId"`
    30  	ArtistID            int64          `json:"artistId"`
    31  	TrackID             int64          `json:"trackId"`
    32  	SourceTitle         string         `json:"sourceTitle"`
    33  	Quality             *starr.Quality `json:"quality"`
    34  	QualityCutoffNotMet bool           `json:"qualityCutoffNotMet"`
    35  	Date                time.Time      `json:"date"`
    36  	DownloadID          string         `json:"downloadId"`
    37  	EventType           string         `json:"eventType"`
    38  	Data                struct {
    39  		Age             string    `json:"age"`
    40  		AgeHours        string    `json:"ageHours"`
    41  		AgeMinutes      string    `json:"ageMinutes"`
    42  		DownloadClient  string    `json:"downloadClient"`
    43  		DownloadForced  string    `json:"downloadForced"`
    44  		DownloadURL     string    `json:"downloadUrl"`
    45  		DroppedPath     string    `json:"droppedPath"`
    46  		GUID            string    `json:"guid"`
    47  		ImportedPath    string    `json:"importedPath"`
    48  		Indexer         string    `json:"indexer"`
    49  		Message         string    `json:"message"`
    50  		NzbInfoURL      string    `json:"nzbInfoUrl"`
    51  		Protocol        string    `json:"protocol"`
    52  		PublishedDate   time.Time `json:"publishedDate"`
    53  		Reason          string    `json:"reason"`
    54  		ReleaseGroup    string    `json:"releaseGroup"`
    55  		Size            string    `json:"size"`
    56  		StatusMessages  string    `json:"statusMessages"`
    57  		TorrentInfoHash string    `json:"torrentInfoHash"`
    58  	} `json:"data"`
    59  }
    60  
    61  // GetHistory returns the Lidarr History (grabs/failures/completed).
    62  // If you need control over the page, use lidarr.GetHistoryPage().
    63  // This function simply returns the number of history records desired,
    64  // up to the number of records present in the application.
    65  // It grabs records in (paginated) batches of perPage, and concatenates
    66  // them into one list.  Passing zero for records will return all of them.
    67  func (l *Lidarr) GetHistory(records, perPage int) (*History, error) {
    68  	return l.GetHistoryContext(context.Background(), records, perPage)
    69  }
    70  
    71  // GetHistoryContext returns the Lidarr History (grabs/failures/completed).
    72  func (l *Lidarr) GetHistoryContext(ctx context.Context, records, perPage int) (*History, error) {
    73  	hist := &History{Records: []*HistoryRecord{}}
    74  	perPage = starr.SetPerPage(records, perPage)
    75  
    76  	for page := 1; ; page++ {
    77  		curr, err := l.GetHistoryPageContext(ctx, &starr.PageReq{PageSize: perPage, Page: page})
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  
    82  		hist.Records = append(hist.Records, curr.Records...)
    83  
    84  		if len(hist.Records) >= curr.TotalRecords ||
    85  			(len(hist.Records) >= records && records != 0) ||
    86  			len(curr.Records) == 0 {
    87  			hist.PageSize = curr.TotalRecords
    88  			hist.TotalRecords = curr.TotalRecords
    89  			hist.SortDirection = curr.SortDirection
    90  			hist.SortKey = curr.SortKey
    91  
    92  			break
    93  		}
    94  
    95  		perPage = starr.AdjustPerPage(records, curr.TotalRecords, len(hist.Records), perPage)
    96  	}
    97  
    98  	return hist, nil
    99  }
   100  
   101  // GetHistoryPage returns a single page from the Lidarr History (grabs/failures/completed).
   102  // The page size and number is configurable with the input request parameters.
   103  func (l *Lidarr) GetHistoryPage(params *starr.PageReq) (*History, error) {
   104  	return l.GetHistoryPageContext(context.Background(), params)
   105  }
   106  
   107  // GetHistoryPageContext returns a single page from the Lidarr History (grabs/failures/completed).
   108  // The page size and number is configurable with the input request parameters.
   109  func (l *Lidarr) GetHistoryPageContext(ctx context.Context, params *starr.PageReq) (*History, error) {
   110  	var output History
   111  
   112  	req := starr.Request{URI: bpHistory, Query: params.Params()}
   113  	if err := l.GetInto(ctx, req, &output); err != nil {
   114  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
   115  	}
   116  
   117  	return &output, nil
   118  }
   119  
   120  // Fail marks the given history item as failed by id.
   121  func (l *Lidarr) Fail(historyID int64) error {
   122  	return l.FailContext(context.Background(), historyID)
   123  }
   124  
   125  // FailContext marks the given history item as failed by id.
   126  func (l *Lidarr) FailContext(ctx context.Context, historyID int64) error {
   127  	if historyID < 1 {
   128  		return fmt.Errorf("%w: invalid history ID: %d", starr.ErrRequestError, historyID)
   129  	}
   130  
   131  	var output interface{}
   132  
   133  	req := starr.Request{
   134  		URI:  path.Join(bpHistory, "failed"),
   135  		Body: bytes.NewBufferString("id=" + fmt.Sprint(historyID)),
   136  	}
   137  	if err := l.PostInto(ctx, req, &output); err != nil {
   138  		return fmt.Errorf("api.Post(%s): %w", &req, err)
   139  	}
   140  
   141  	return nil
   142  }