Skip to content

Commit

Permalink
use 'all' endpoint, batch requests
Browse files Browse the repository at this point in the history
  • Loading branch information
xel86 committed Feb 7, 2022
1 parent 7a03319 commit 8caeb76
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 25 deletions.
35 changes: 26 additions & 9 deletions cmd/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"log"
"time"

"github.com/ludviglundgren/qbittorrent-cli/internal/config"
"github.com/ludviglundgren/qbittorrent-cli/pkg/qbittorrent"
Expand Down Expand Up @@ -56,13 +57,18 @@ func RunPause() *cobra.Command {
os.Exit(1)
}

var foundTorrents []qbittorrent.Torrent
if pauseAll {
foundTorrents, err = qb.GetTorrents()
} else {
foundTorrents, err = qb.GetTorrentsByPrefixes(args, hashes, names)
if pauseAll {
qb.Pause([]string{"all"})
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not pause torrents: %v\n", err)
os.Exit(1)
}

log.Printf("All torrents paused successfully")
return
}

foundTorrents, err := qb.GetTorrentsByPrefixes(args, hashes, names)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: failed to retrieve torrents: %v\n", err)
os.Exit(1)
Expand All @@ -78,10 +84,21 @@ func RunPause() *cobra.Command {
return
}

err = qb.Pause(hashesToPause)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not pause torrents %v\n", err)
os.Exit(1)
// Split the hashes to pause into groups of 20 to avoid flooding qbittorrent
batch := 20
for i := 0; i < len(hashesToPause); i += batch {
j := i + batch
if j > len(hashesToPause) {
j = len(hashesToPause)
}

qb.Pause(hashesToPause[i:j])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not pause torrents: %v\n", err)
os.Exit(1)
}

time.Sleep(time.Second * 1)
}

log.Printf("torrent(s) successfully paused")
Expand Down
33 changes: 25 additions & 8 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"log"
"time"

"github.com/ludviglundgren/qbittorrent-cli/internal/config"
"github.com/ludviglundgren/qbittorrent-cli/pkg/qbittorrent"
Expand Down Expand Up @@ -58,13 +59,18 @@ func RunRemove() *cobra.Command {
os.Exit(1)
}

var foundTorrents []qbittorrent.Torrent
if removeAll {
foundTorrents, err = qb.GetTorrents()
} else {
foundTorrents, err = qb.GetTorrentsByPrefixes(args, hashes, names)
qb.DeleteTorrents([]string{"all"}, deleteFiles)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not delete torrents: %v\n", err)
os.Exit(1)
}

log.Printf("All torrents removed successfully")
return
}

foundTorrents, err := qb.GetTorrentsByPrefixes(args, hashes, names)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: failed to retrieve torrents: %v\n", err)
os.Exit(1)
Expand All @@ -80,10 +86,21 @@ func RunRemove() *cobra.Command {
return
}

err = qb.DeleteTorrents(hashesToRemove, deleteFiles)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not delete torrents: %v\n", err)
os.Exit(1)
// Split the hashes to remove into groups of 20 to avoid flooding qbittorrent
batch := 20
for i := 0; i < len(hashesToRemove); i += batch {
j := i + batch
if j > len(hashesToRemove) {
j = len(hashesToRemove)
}

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

time.Sleep(time.Second * 1)
}

log.Printf("torrent(s) successfully deleted")
Expand Down
34 changes: 26 additions & 8 deletions cmd/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"log"
"time"

"github.com/ludviglundgren/qbittorrent-cli/internal/config"
"github.com/ludviglundgren/qbittorrent-cli/pkg/qbittorrent"
Expand Down Expand Up @@ -56,13 +57,19 @@ func RunResume() *cobra.Command {
os.Exit(1)
}

var foundTorrents []qbittorrent.Torrent

if resumeAll {
foundTorrents, err = qb.GetTorrents()
} else {
foundTorrents, err = qb.GetTorrentsByPrefixes(args, hashes, names)
qb.Resume([]string{"all"})
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not resume torrents: %v\n", err)
os.Exit(1)
}

log.Printf("All torrents resumed successfully")
return
}

foundTorrents, err := qb.GetTorrentsByPrefixes(args, hashes, names)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: failed to retrieve torrents: %v\n", err)
os.Exit(1)
Expand All @@ -78,10 +85,21 @@ func RunResume() *cobra.Command {
return
}

err = qb.Resume(hashesToResume)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not resume torrents %v\n", err)
os.Exit(1)
// Split the hashes to resume into groups of 20 to avoid flooding qbittorrent
batch := 20
for i := 0; i < len(hashesToResume); i += batch {
j := i + batch
if j > len(hashesToResume) {
j = len(hashesToResume)
}

qb.Resume(hashesToResume[i:j])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not resume torrents: %v\n", err)
os.Exit(1)
}

time.Sleep(time.Second * 1)
}

log.Printf("torrent(s) successfully resumed")
Expand Down
4 changes: 4 additions & 0 deletions pkg/qbittorrent/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func (c *Client) GetTorrentsByPrefixes(terms []string, hashes bool, names bool)
break
}
}

if matchedTorrents[torrent] {
continue
}
}

if names {
Expand Down

0 comments on commit 8caeb76

Please sign in to comment.