Skip to content

Commit

Permalink
adding delete queue items
Browse files Browse the repository at this point in the history
  • Loading branch information
scripterkiddd committed Jul 31, 2022
1 parent 9cdd4b2 commit 453cb64
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 62 deletions.
18 changes: 18 additions & 0 deletions radarr/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package radarr
import (
"context"
"fmt"
"net/url"
"time"

"golift.io/starr"
Expand Down Expand Up @@ -104,3 +105,20 @@ func (r *Radarr) GetQueuePageContext(ctx context.Context, params *starr.Req) (*Q

return &queue, nil
}

type DeleteQueueRecordParam struct {
Blocklist bool
RemoveFromClient bool
}

// https://radarr.video/docs/api/#/Queue/delete_api_v3_queue__id_
func (r *Radarr) DeleteQueueRecord(ctx context.Context, record *QueueRecord, p *DeleteQueueRecordParam) error {
params := make(url.Values)
params.Set("blocklist", fmt.Sprintf("%t", p.Blocklist))
params.Set("removeFromClient", fmt.Sprintf("%t", p.RemoveFromClient))
_, err := r.Delete(ctx, fmt.Sprintf("v3/queue/%d", record.ID), params)
if err != nil {
return fmt.Errorf("api.Delete(queue): %w", err)
}
return nil
}
84 changes: 84 additions & 0 deletions sonarr/queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package sonarr

import (
"context"
"fmt"
"golift.io/starr"
"net/url"
)

// GetQueue returns a single page from the Sonarr Queue (processing, but not yet imported).
// WARNING: 12/30/2021 - this method changed.
// If you need control over the page, use sonarr.GetQueuePage().
// This function simply returns the number of queue records desired,
// up to the number of records present in the application.
// It grabs records in (paginated) batches of perPage, and concatenates
// them into one list. Passing zero for records will return all of them.
func (s *Sonarr) GetQueue(records, perPage int) (*Queue, error) {
return s.GetQueueContext(context.Background(), records, perPage)
}

func (s *Sonarr) GetQueueContext(ctx context.Context, records, perPage int) (*Queue, error) {
queue := &Queue{Records: []*QueueRecord{}}
perPage = starr.SetPerPage(records, perPage)

for page := 1; ; page++ {
curr, err := s.GetQueuePageContext(ctx, &starr.Req{PageSize: perPage, Page: page})
if err != nil {
return nil, err
}

queue.Records = append(queue.Records, curr.Records...)

if len(queue.Records) >= curr.TotalRecords ||
(len(queue.Records) >= records && records != 0) ||
len(curr.Records) == 0 {
queue.PageSize = curr.TotalRecords
queue.TotalRecords = curr.TotalRecords
queue.SortDirection = curr.SortDirection
queue.SortKey = curr.SortKey

break
}

perPage = starr.AdjustPerPage(records, curr.TotalRecords, len(queue.Records), perPage)
}

return queue, nil
}

// GetQueuePage returns a single page from the Sonarr Queue.
// The page size and number is configurable with the input request parameters.
func (s *Sonarr) GetQueuePage(params *starr.Req) (*Queue, error) {
return s.GetQueuePageContext(context.Background(), params)
}

func (s *Sonarr) GetQueuePageContext(ctx context.Context, params *starr.Req) (*Queue, error) {
var queue Queue

params.CheckSet("sortKey", "timeleft")
params.CheckSet("includeUnknownSeriesItems", "true")

_, err := s.GetInto(ctx, "v3/queue", params.Params(), &queue)
if err != nil {
return nil, fmt.Errorf("api.Get(queue): %w", err)
}

return &queue, nil
}

type DeleteQueueRecordParam struct {
Blacklist bool
}

// https://github.com/Sonarr/Sonarr/wiki/Queue
func (s *Sonarr) DeleteQueueRecord(ctx context.Context, record *QueueRecord, p *DeleteQueueRecordParam) error {
params := make(url.Values)
params.Set("blacklist", fmt.Sprintf("%t", p.Blacklist))

_, err := s.Delete(ctx, fmt.Sprintf("v3/queue/%d", record.ID), params)
if err != nil {
return fmt.Errorf("api.Delete(queue): %w", err)
}
return nil
}
62 changes: 0 additions & 62 deletions sonarr/sonarr.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package sonarr

import (
"context"
"crypto/tls"
"fmt"
"net/http"

"golift.io/starr"
Expand Down Expand Up @@ -49,63 +47,3 @@ func New(config *starr.Config) *Sonarr {

return &Sonarr{APIer: config}
}

// GetQueue returns a single page from the Sonarr Queue (processing, but not yet imported).
// WARNING: 12/30/2021 - this method changed.
// If you need control over the page, use sonarr.GetQueuePage().
// This function simply returns the number of queue records desired,
// up to the number of records present in the application.
// It grabs records in (paginated) batches of perPage, and concatenates
// them into one list. Passing zero for records will return all of them.
func (s *Sonarr) GetQueue(records, perPage int) (*Queue, error) {
return s.GetQueueContext(context.Background(), records, perPage)
}

func (s *Sonarr) GetQueueContext(ctx context.Context, records, perPage int) (*Queue, error) {
queue := &Queue{Records: []*QueueRecord{}}
perPage = starr.SetPerPage(records, perPage)

for page := 1; ; page++ {
curr, err := s.GetQueuePageContext(ctx, &starr.Req{PageSize: perPage, Page: page})
if err != nil {
return nil, err
}

queue.Records = append(queue.Records, curr.Records...)

if len(queue.Records) >= curr.TotalRecords ||
(len(queue.Records) >= records && records != 0) ||
len(curr.Records) == 0 {
queue.PageSize = curr.TotalRecords
queue.TotalRecords = curr.TotalRecords
queue.SortDirection = curr.SortDirection
queue.SortKey = curr.SortKey

break
}

perPage = starr.AdjustPerPage(records, curr.TotalRecords, len(queue.Records), perPage)
}

return queue, nil
}

// GetQueuePage returns a single page from the Sonarr Queue.
// The page size and number is configurable with the input request parameters.
func (s *Sonarr) GetQueuePage(params *starr.Req) (*Queue, error) {
return s.GetQueuePageContext(context.Background(), params)
}

func (s *Sonarr) GetQueuePageContext(ctx context.Context, params *starr.Req) (*Queue, error) {
var queue Queue

params.CheckSet("sortKey", "timeleft")
params.CheckSet("includeUnknownSeriesItems", "true")

_, err := s.GetInto(ctx, "v3/queue", params.Params(), &queue)
if err != nil {
return nil, fmt.Errorf("api.Get(queue): %w", err)
}

return &queue, nil
}

0 comments on commit 453cb64

Please sign in to comment.