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

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