Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get and set label methods #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading