-
Notifications
You must be signed in to change notification settings - Fork 16
/
blocklist.go
134 lines (109 loc) · 4.25 KB
/
blocklist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package sonarr
import (
"bytes"
"context"
"encoding/json"
"fmt"
"path"
"time"
"golift.io/starr"
)
// Define Base Path for Block List queries.
const bpBlocklist = APIver + "/blocklist"
// BlockList represents the /api/v3/blocklist endpoint.
type BlockList struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
SortKey string `json:"sortKey"`
SortDirection string `json:"sortDirection"`
TotalRecords int `json:"totalRecords"`
Records []*BlockListRecord `json:"records"`
}
// BlockListRecord represents a single block list item.
type BlockListRecord struct {
Series *Series `json:"series"`
Quality *starr.Quality `json:"quality"`
Languages []*starr.Value `json:"languages"`
CustomFormats []*CustomFormatOutput `json:"customFormats"`
EpisodeIDs []int64 `json:"episodeIds"`
ID int64 `json:"id"`
SeriesID int64 `json:"seriesId"`
Date time.Time `json:"date"`
SourceTitle string `json:"sourceTitle"`
Protocol string `json:"protocol"`
Indexer string `json:"indexer"`
Message string `json:"message"`
}
// GetBlockList returns the count of block list items requested.
// If you need control over the page, use GetBlockListPage().
func (s *Sonarr) GetBlockList(count int) (*BlockList, error) {
return s.GetBlockListContext(context.Background(), count)
}
// GetBlockListContext returns block list items.
func (s *Sonarr) GetBlockListContext(ctx context.Context, records int) (*BlockList, error) {
list := &BlockList{Records: []*BlockListRecord{}}
perPage := starr.SetPerPage(records, 0)
for page := 1; ; page++ {
curr, err := s.GetBlockListPageContext(ctx, &starr.PageReq{PageSize: perPage, Page: page})
if err != nil {
return nil, err
}
list.Records = append(list.Records, curr.Records...)
if len(list.Records) >= curr.TotalRecords ||
(len(list.Records) >= records && records != 0) ||
len(curr.Records) == 0 {
list.PageSize = curr.TotalRecords
list.TotalRecords = curr.TotalRecords
list.SortDirection = curr.SortDirection
list.SortKey = curr.SortKey
break
}
perPage = starr.AdjustPerPage(records, curr.TotalRecords, len(list.Records), perPage)
}
return list, nil
}
// GetBlockListPage returns block list items based on filters.
func (s *Sonarr) GetBlockListPage(params *starr.PageReq) (*BlockList, error) {
return s.GetBlockListPageContext(context.Background(), params)
}
// GetBlockListPageContext returns block list items based on filters.
func (s *Sonarr) GetBlockListPageContext(ctx context.Context, params *starr.PageReq) (*BlockList, error) {
var output BlockList
params.CheckSet("sortKey", "date")
req := starr.Request{URI: bpBlocklist, Query: params.Params()}
if err := s.GetInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
}
return &output, nil
}
// DeleteBlockList removes a single block list item.
func (s *Sonarr) DeleteBlockList(listID int64) error {
return s.DeleteBlockListContext(context.Background(), listID)
}
// DeleteBlockListContext removes a single block list item with a context.
func (s *Sonarr) DeleteBlockListContext(ctx context.Context, listID int64) error {
req := starr.Request{URI: path.Join(bpBlocklist, fmt.Sprint(listID))}
if err := s.DeleteAny(ctx, req); err != nil {
return fmt.Errorf("api.Delete(%s): %w", &req, err)
}
return nil
}
// DeleteBlockLists removes multiple block list items.
func (s *Sonarr) DeleteBlockLists(ids []int64) error {
return s.DeleteBlockListsContext(context.Background(), ids)
}
// DeleteBlockListsContext removes multiple block list items with a context.
func (s *Sonarr) DeleteBlockListsContext(ctx context.Context, ids []int64) error {
input := struct {
IDs []int64 `json:"ids"`
}{IDs: ids}
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(input); err != nil {
return fmt.Errorf("json.Marshal(%s): %w", bpBlocklist, err)
}
req := starr.Request{URI: path.Join(bpBlocklist, "bulk"), Body: &body}
if err := s.DeleteAny(ctx, req); err != nil {
return fmt.Errorf("api.Delete(%s): %w", &req, err)
}
return nil
}