diff --git a/config.go b/config.go index 71ca05e..81fe1a1 100644 --- a/config.go +++ b/config.go @@ -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. diff --git a/deluge.go b/deluge.go index 12647d2..3f9b3be 100644 --- a/deluge.go +++ b/deluge.go @@ -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 } @@ -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)