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