Skip to content

Commit

Permalink
add get and set label methods
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Feb 7, 2024
1 parent cf48333 commit 5cb3713
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const (
GetTorrentStat = "core.get_torrent_status"
GetAllTorrents = "core.get_torrents_status"
HostStatus = "web.get_host_status"
GeHosts = "web.get_hosts"
GetHosts = "web.get_hosts"
GetLabels = "label.get_labels"
SetLabel = "label.set_torrent"
)

// Config is the data needed to poll Deluge.
Expand Down
38 changes: 37 additions & 1 deletion deluge.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (d *Deluge) LoginContext(ctx context.Context) error {

// setVersion digs into the first server in the web UI to find the version.
func (d *Deluge) setVersion(ctx context.Context) error {
response, err := d.Get(ctx, GeHosts, []string{})
response, err := d.Get(ctx, GetHosts, []string{})
if err != nil {
return err
}
Expand Down Expand Up @@ -246,6 +246,42 @@ func (d *Deluge) GetXfersCompatContext(ctx context.Context) (map[string]*XferSta
return xfers, nil
}

// GetLabels gets all the labels from Deluge.
func (d *Deluge) GetLabels() ([]string, error) {
return d.GetLabelsContext(context.Background())
}

// GetLabelsContext gets all the labels from Deluge.
func (d *Deluge) GetLabelsContext(ctx context.Context) ([]string, error) {
labels := []string{}

response, err := d.Get(ctx, GetLabels, []string{})
if err != nil {
return nil, fmt.Errorf("get(GetLabels): %w", err)
}

if err := json.Unmarshal(response.Result, &labels); err != nil {
return nil, fmt.Errorf("json.Unmarshal(labels): %w", err)
}

return labels, nil
}

// SetLabel sets a label on a torrent.
func (d *Deluge) SetLabel(torrentID string, label string) error {
return d.SetLabelContext(context.Background(), torrentID, label)
}

// SetLabelContext sets a label on a torrent.
func (d *Deluge) SetLabelContext(ctx context.Context, torrentID string, label string) error {
_, err := d.Get(ctx, SetLabel, []string{torrentID, label})
if err != nil {
return fmt.Errorf("get(SetLabel): %w", err)
}

return nil
}

// Get a response from Deluge.
func (d *Deluge) Get(ctx context.Context, method string, params interface{}) (*Response, error) {
return d.req(ctx, method, params, true)
Expand Down

0 comments on commit 5cb3713

Please sign in to comment.