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

     1  package lidarr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"path"
     9  	"time"
    10  
    11  	"golift.io/starr"
    12  )
    13  
    14  const bpQueue = APIver + "/queue"
    15  
    16  // Queue is the /api/v1/queue endpoint.
    17  type Queue struct {
    18  	Page          int            `json:"page"`
    19  	PageSize      int            `json:"pageSize"`
    20  	SortKey       string         `json:"sortKey"`
    21  	SortDirection string         `json:"sortDirection"`
    22  	TotalRecords  int            `json:"totalRecords"`
    23  	Records       []*QueueRecord `json:"records"`
    24  }
    25  
    26  // QueueRecord represents the records returns by the /api/v1/queue endpoint.
    27  type QueueRecord struct {
    28  	ArtistID                int64                  `json:"artistId"`
    29  	AlbumID                 int64                  `json:"albumId"`
    30  	Quality                 *starr.Quality         `json:"quality"`
    31  	Size                    float64                `json:"size"`
    32  	Title                   string                 `json:"title"`
    33  	Sizeleft                float64                `json:"sizeleft"`
    34  	Timeleft                string                 `json:"timeleft"`
    35  	EstimatedCompletionTime time.Time              `json:"estimatedCompletionTime"`
    36  	Status                  string                 `json:"status"`
    37  	TrackedDownloadStatus   string                 `json:"trackedDownloadStatus"`
    38  	StatusMessages          []*starr.StatusMessage `json:"statusMessages"`
    39  	DownloadID              string                 `json:"downloadId"`
    40  	Protocol                string                 `json:"protocol"`
    41  	DownloadClient          string                 `json:"downloadClient"`
    42  	Indexer                 string                 `json:"indexer"`
    43  	OutputPath              string                 `json:"outputPath"`
    44  	DownloadForced          bool                   `json:"downloadForced"`
    45  	ID                      int64                  `json:"id"`
    46  	ErrorMessage            string                 `json:"errorMessage"`
    47  }
    48  
    49  // GetQueue returns a single page from the Lidarr Queue (processing, but not yet imported).
    50  // If you need control over the page, use lidarr.GetQueuePage().
    51  // This function simply returns the number of queue records desired,
    52  // up to the number of records present in the application.
    53  // It grabs records in (paginated) batches of perPage, and concatenates
    54  // them into one list.  Passing zero for records will return all of them.
    55  func (l *Lidarr) GetQueue(records, perPage int) (*Queue, error) {
    56  	return l.GetQueueContext(context.Background(), records, perPage)
    57  }
    58  
    59  // GetQueueContext returns a single page from the Lidarr Queue (processing, but not yet imported).
    60  func (l *Lidarr) GetQueueContext(ctx context.Context, records, perPage int) (*Queue, error) {
    61  	queue := &Queue{Records: []*QueueRecord{}}
    62  	perPage = starr.SetPerPage(records, perPage)
    63  
    64  	for page := 1; ; page++ {
    65  		curr, err := l.GetQueuePageContext(ctx, &starr.PageReq{PageSize: perPage, Page: page})
    66  		if err != nil {
    67  			return nil, err
    68  		}
    69  
    70  		queue.Records = append(queue.Records, curr.Records...)
    71  
    72  		if len(queue.Records) >= curr.TotalRecords ||
    73  			(len(queue.Records) >= records && records != 0) ||
    74  			len(curr.Records) == 0 {
    75  			queue.PageSize = curr.TotalRecords
    76  			queue.TotalRecords = curr.TotalRecords
    77  			queue.SortDirection = curr.SortDirection
    78  			queue.SortKey = curr.SortKey
    79  
    80  			break
    81  		}
    82  
    83  		perPage = starr.AdjustPerPage(records, curr.TotalRecords, len(queue.Records), perPage)
    84  	}
    85  
    86  	return queue, nil
    87  }
    88  
    89  // GetQueuePage returns a single page from the Lidarr Queue.
    90  // The page size and number is configurable with the input request parameters.
    91  func (l *Lidarr) GetQueuePage(params *starr.PageReq) (*Queue, error) {
    92  	return l.GetQueuePageContext(context.Background(), params)
    93  }
    94  
    95  // GetQueuePageContext returns a single page from the Lidarr Queue.
    96  // The page size and number is configurable with the input request parameters.
    97  func (l *Lidarr) GetQueuePageContext(ctx context.Context, params *starr.PageReq) (*Queue, error) {
    98  	var output Queue
    99  
   100  	params.CheckSet("sortKey", "timeleft")
   101  	params.CheckSet("includeUnknownArtistItems", "true")
   102  
   103  	req := starr.Request{URI: bpQueue, Query: params.Params()}
   104  	if err := l.GetInto(ctx, req, &output); err != nil {
   105  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
   106  	}
   107  
   108  	return &output, nil
   109  }
   110  
   111  // DeleteQueue deletes an item from the Activity Queue.
   112  func (l *Lidarr) DeleteQueue(queueID int64, opts *starr.QueueDeleteOpts) error {
   113  	return l.DeleteQueueContext(context.Background(), queueID, opts)
   114  }
   115  
   116  // DeleteQueueContext deletes an item from the Activity Queue.
   117  func (l *Lidarr) DeleteQueueContext(ctx context.Context, queueID int64, opts *starr.QueueDeleteOpts) error {
   118  	req := starr.Request{URI: path.Join(bpQueue, fmt.Sprint(queueID)), Query: opts.Values()}
   119  	if err := l.DeleteAny(ctx, req); err != nil {
   120  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  // QueueGrab tells the app to grab an item that's in queue.
   127  // Most often used on items with a delay set from a delay profile.
   128  func (l *Lidarr) QueueGrab(ids ...int64) error {
   129  	return l.QueueGrabContext(context.Background(), ids...)
   130  }
   131  
   132  // QueueGrabContext tells the app to grab an item that's in queue, probably set to a delay.
   133  // Most often used on items with a delay set from a delay profile.
   134  func (l *Lidarr) QueueGrabContext(ctx context.Context, ids ...int64) error {
   135  	idList := struct {
   136  		IDs []int64 `json:"ids"`
   137  	}{IDs: ids}
   138  
   139  	var body bytes.Buffer
   140  	if err := json.NewEncoder(&body).Encode(idList); err != nil {
   141  		return fmt.Errorf("json.Marshal(%s): %w", bpQueue, err)
   142  	}
   143  
   144  	var output interface{} // any ok
   145  
   146  	req := starr.Request{URI: path.Join(bpQueue, "grab", "bulk"), Body: &body}
   147  	if err := l.PostInto(ctx, req, &output); err != nil {
   148  		return fmt.Errorf("api.Post(%s): %w", &req, err)
   149  	}
   150  
   151  	return nil
   152  }