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

     1  package readarr
     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 is 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  	BookID              int64          `json:"bookId"`
    30  	AuthorID            int64          `json:"authorId"`
    31  	SourceTitle         string         `json:"sourceTitle"`
    32  	Quality             *starr.Quality `json:"quality"`
    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  		DownloadForced  string    `json:"downloadForced"`
    43  		DownloadURL     string    `json:"downloadUrl"`
    44  		DroppedPath     string    `json:"droppedPath"`
    45  		GUID            string    `json:"guid"`
    46  		ImportedPath    string    `json:"importedPath"`
    47  		Indexer         string    `json:"indexer"`
    48  		Message         string    `json:"message"`
    49  		NzbInfoURL      string    `json:"nzbInfoUrl"`
    50  		Protocol        string    `json:"protocol"`
    51  		PublishedDate   time.Time `json:"publishedDate"`
    52  		Reason          string    `json:"reason"`
    53  		ReleaseGroup    string    `json:"releaseGroup"`
    54  		Size            string    `json:"size"`
    55  		StatusMessages  string    `json:"statusMessages"`
    56  		TorrentInfoHash string    `json:"torrentInfoHash"`
    57  	} `json:"data"`
    58  }
    59  
    60  // GetHistory returns the Readarr History (grabs/failures/completed).
    61  // If you need control over the page, use readarr.GetHistoryPage().
    62  // This function simply returns the number of history records desired,
    63  // up to the number of records present in the application.
    64  // It grabs records in (paginated) batches of perPage, and concatenates
    65  // them into one list.  Passing zero for records will return all of them.
    66  func (r *Readarr) GetHistory(records, perPage int) (*History, error) {
    67  	return r.GetHistoryContext(context.Background(), records, perPage)
    68  }
    69  
    70  // GetHistoryContext returns the Readarr History (grabs/failures/completed).
    71  // If you need control over the page, use readarr.GetHistoryPageContext().
    72  func (r *Readarr) 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 := r.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 Readarr History (grabs/failures/completed).
   102  // The page size and number is configurable with the input request parameters.
   103  func (r *Readarr) GetHistoryPage(params *starr.PageReq) (*History, error) {
   104  	return r.GetHistoryPageContext(context.Background(), params)
   105  }
   106  
   107  // GetHistoryPageContext returns a single page from the Readarr History (grabs/failures/completed).
   108  // The page size and number is configurable with the input request parameters.
   109  func (r *Readarr) 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 := r.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 (r *Readarr) Fail(historyID int64) error {
   122  	return r.FailContext(context.Background(), historyID)
   123  }
   124  
   125  // FailContext marks the given history item as failed by id.
   126  func (r *Readarr) 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 := r.PostInto(ctx, req, &output); err != nil {
   138  		return fmt.Errorf("api.Post(%s): %w", &req, err)
   139  	}
   140  
   141  	return nil
   142  }