golift.io/starr@v1.0.0/radarr/queue.go (about) 1 package radarr 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/v3/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 is part of the activity Queue. 27 type QueueRecord struct { 28 MovieID int64 `json:"movieId"` 29 Languages []*starr.Value `json:"languages"` 30 Quality *starr.Quality `json:"quality"` 31 CustomFormats []*CustomFormatOutput `json:"customFormats"` 32 Size float64 `json:"size"` 33 Title string `json:"title"` 34 Sizeleft float64 `json:"sizeleft"` 35 Timeleft string `json:"timeleft"` 36 EstimatedCompletionTime time.Time `json:"estimatedCompletionTime"` 37 Status string `json:"status"` 38 TrackedDownloadStatus string `json:"trackedDownloadStatus"` 39 TrackedDownloadState string `json:"trackedDownloadState"` 40 StatusMessages []*starr.StatusMessage `json:"statusMessages"` 41 DownloadID string `json:"downloadId"` 42 Protocol string `json:"protocol"` 43 DownloadClient string `json:"downloadClient"` 44 Indexer string `json:"indexer"` 45 OutputPath string `json:"outputPath"` 46 ID int64 `json:"id"` 47 ErrorMessage string `json:"errorMessage"` 48 } 49 50 // GetQueue returns a single page from the Radarr Queue (processing, but not yet imported). 51 // If you need control over the page, use radarr.GetQueuePage(). 52 // This function simply returns the number of queue records desired, 53 // up to the number of records present in the application. 54 // It grabs records in (paginated) batches of perPage, and concatenates 55 // them into one list. Passing zero for records will return all of them. 56 func (r *Radarr) GetQueue(records, perPage int) (*Queue, error) { 57 return r.GetQueueContext(context.Background(), records, perPage) 58 } 59 60 // GetQueueContext returns a single page from the Radarr Queue (processing, but not yet imported). 61 func (r *Radarr) GetQueueContext(ctx context.Context, records, perPage int) (*Queue, error) { 62 queue := &Queue{Records: []*QueueRecord{}} 63 perPage = starr.SetPerPage(records, perPage) 64 65 for page := 1; ; page++ { 66 curr, err := r.GetQueuePageContext(ctx, &starr.PageReq{PageSize: perPage, Page: page}) 67 if err != nil { 68 return nil, err 69 } 70 71 queue.Records = append(queue.Records, curr.Records...) 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 Radarr Queue. 90 // The page size and number is configurable with the input request parameters. 91 func (r *Radarr) GetQueuePage(params *starr.PageReq) (*Queue, error) { 92 return r.GetQueuePageContext(context.Background(), params) 93 } 94 95 // GetQueuePage returns a single page from the Radarr Queue. 96 // The page size and number is configurable with the input request parameters. 97 func (r *Radarr) GetQueuePageContext(ctx context.Context, params *starr.PageReq) (*Queue, error) { 98 var output Queue 99 100 params.CheckSet("sortKey", "timeleft") 101 params.CheckSet("includeUnknownMovieItems", "true") 102 103 req := starr.Request{URI: bpQueue, Query: params.Params()} 104 if err := r.GetInto(ctx, req, &output); err != nil { 105 return nil, fmt.Errorf("api.Get(queue): %w", err) 106 } 107 108 return &output, nil 109 } 110 111 // DeleteQueue deletes an item from the Activity Queue. 112 func (r *Radarr) DeleteQueue(queueID int64, opts *starr.QueueDeleteOpts) error { 113 return r.DeleteQueueContext(context.Background(), queueID, opts) 114 } 115 116 // DeleteQueueContext deletes an item from the Activity Queue. 117 func (r *Radarr) 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 := r.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 (r *Radarr) QueueGrab(ids ...int64) error { 129 return r.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 (r *Radarr) 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 := r.PostInto(ctx, req, &output); err != nil { 148 return fmt.Errorf("api.Post(%s): %w", &req, err) 149 } 150 151 return nil 152 }