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

feat(remove): add option to remove paused #57

Merged
merged 4 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 30 additions & 5 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
// RunRemove cmd to remove torrents
func RunRemove() *cobra.Command {
var (
removeAll bool
deleteFiles bool
hashes bool
names bool
removeAll bool
removePaused bool
deleteFiles bool
hashes bool
names bool
)

var command = &cobra.Command{
Expand All @@ -30,6 +31,7 @@ func RunRemove() *cobra.Command {
}

command.Flags().BoolVar(&removeAll, "all", false, "Removes all torrents")
command.Flags().BoolVar(&removePaused, "paused", false, "Removes all paused torrents")
command.Flags().BoolVar(&deleteFiles, "delete-files", false, "Also delete downloaded files from torrent(s)")
command.Flags().BoolVar(&hashes, "hashes", false, "Provided arguments will be read as torrent hashes")
command.Flags().BoolVar(&names, "names", false, "Provided arguments will be read as torrent names")
Expand Down Expand Up @@ -66,7 +68,30 @@ func RunRemove() *cobra.Command {
}

if removeAll {
if err := qb.DeleteTorrents(ctx, []string{"all"}, deleteFiles); err != nil {
if removePaused {
pausedTorrents, err := qb.GetPausedTorrents(ctx)
ludviglundgren marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: failed to retrieve paused torrents: %v\n", err)
os.Exit(1)
}

hashesToRemove := []string{}
for _, torrent := range pausedTorrents {
hashesToRemove = append(hashesToRemove, torrent.Hash)
}

if len(hashesToRemove) < 1 {
log.Printf("No paused torrents found to remove")
return
}

if err := qb.DeleteTorrents(ctx, hashesToRemove, deleteFiles); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not delete paused torrents: %v\n", err)
os.Exit(1)
}

log.Print("Paused torrents removed successfully")
} else if err := qb.DeleteTorrents(ctx, []string{"all"}, deleteFiles); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not delete torrents: %v\n", err)
os.Exit(1)
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/qbittorrent/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package qbittorrent

import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -329,6 +330,27 @@ func (c *Client) ReAnnounceTorrents(ctx context.Context, hashes []string) error
return nil
}

func (c *Client) GetPausedTorrents(ctx context.Context) ([]Torrent, error) {
// Initiate request to appropriate endpoint to fetch all paused torrent info.
resp, err := c.getCtx(ctx, "torrents/info?filter=paused", nil)
if err != nil {
log.Printf("Error retrieving paused torrent info: %v", err)
return nil, fmt.Errorf("error retrieving paused torrent info: %v", err)
}
defer resp.Body.Close()

// Parse response.
var torrents []Torrent
if err := json.NewDecoder(resp.Body).Decode(&torrents); err != nil {
log.Printf("Error decoding response: %v", err)
return nil, fmt.Errorf("error decoding response: %v", err)
}

log.Printf("Found %d paused torrents", len(torrents))

return torrents, nil
}

func (c *Client) Pause(ctx context.Context, hashes []string) error {
v := url.Values{}

Expand Down