Skip to content

Commit

Permalink
Merge pull request #17 from ludviglundgren/feat/pause-torrents
Browse files Browse the repository at this point in the history
Feat: pause and resume all torrents
  • Loading branch information
ludviglundgren authored Nov 16, 2021
2 parents 5acc8c0 + 9cbaab8 commit c1abbb5
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cmd/pause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"fmt"
"os"

"github.com/ludviglundgren/qbittorrent-cli/internal/config"
"github.com/ludviglundgren/qbittorrent-cli/pkg/qbittorrent"

"github.com/spf13/cobra"
)

// RunPause cmd to pause torrents
func RunPause() *cobra.Command {
var command = &cobra.Command{
Use: "pause",
Short: "Pause all torrents",
Long: `Pause all torrents`,
}
command.Run = func(cmd *cobra.Command, args []string) {
qbtSettings := qbittorrent.Settings{
Hostname: config.Qbit.Host,
Port: config.Qbit.Port,
Username: config.Qbit.Login,
Password: config.Qbit.Password,
}
qb := qbittorrent.NewClient(qbtSettings)

err := qb.Login()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: connection failed: %v\n", err)
os.Exit(1)
}

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

return command
}
2 changes: 2 additions & 0 deletions cmd/qbt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Documentation is available at https://github.com/ludviglundgren/qbittorrent-cli`
rootCmd.AddCommand(cmd.RunList())
rootCmd.AddCommand(cmd.RunAdd())
rootCmd.AddCommand(cmd.RunImport())
rootCmd.AddCommand(cmd.RunPause())
rootCmd.AddCommand(cmd.RunResume())

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down
44 changes: 44 additions & 0 deletions cmd/resume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"
"os"

"github.com/ludviglundgren/qbittorrent-cli/internal/config"
"github.com/ludviglundgren/qbittorrent-cli/pkg/qbittorrent"

"github.com/spf13/cobra"
)

// RunResume cmd to resume torrents
func RunResume() *cobra.Command {
var command = &cobra.Command{
Use: "resume",
Short: "Resume all torrents",
Long: `Resume all torrents`,
}

command.Run = func(cmd *cobra.Command, args []string) {
qbtSettings := qbittorrent.Settings{
Hostname: config.Qbit.Host,
Port: config.Qbit.Port,
Username: config.Qbit.Login,
Password: config.Qbit.Password,
}
qb := qbittorrent.NewClient(qbtSettings)

err := qb.Login()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: connection failed: %v\n", err)
os.Exit(1)
}

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

return command
}
48 changes: 48 additions & 0 deletions pkg/qbittorrent/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,51 @@ func (c *Client) ReAnnounceTorrents(hashes []string) error {

return nil
}

func (c *Client) Pause(hashes []string) error {
v := url.Values{}
encodedHashes := "all"

if len(hashes) > 0 {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
v.Add("hashes", hv)

encodedHashes = v.Encode()
}

resp, err := c.get("torrents/pause?"+encodedHashes, nil)
if err != nil {
log.Fatalf("error pausing torrents: %v", err)
} else if resp.StatusCode != http.StatusOK {
return err
}

defer resp.Body.Close()

return nil
}

func (c *Client) Resume(hashes []string) error {
v := url.Values{}
encodedHashes := "all"

if len(hashes) > 0 {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
v.Add("hashes", hv)

encodedHashes = v.Encode()
}

resp, err := c.get("torrents/resume?"+encodedHashes, nil)
if err != nil {
log.Fatalf("error resuming torrents: %v", err)
} else if resp.StatusCode != http.StatusOK {
return err
}

defer resp.Body.Close()

return nil
}

0 comments on commit c1abbb5

Please sign in to comment.