Skip to content

Commit

Permalink
feat(torrent): edit trackers (#79)
Browse files Browse the repository at this point in the history
* chore(deps): update go-qbittorrent to v1.5.0

* feat(torrent): edit tracker

* feat(torrent): update readme
  • Loading branch information
ludviglundgren authored Sep 24, 2023
1 parent a5afb93 commit 023e947
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 1 deletion.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ Available Commands:
reannounce Reannounce torrent(s)
remove Removes specified torrent(s)
resume Resume specified torrent(s)
tracker Torrent tracker subcommand
Flags:
-h, --help help for torrent
Expand Down Expand Up @@ -793,7 +794,47 @@ Flags:
Global Flags:
--config string config file (default is $HOME/.config/qbt/.qbt.toml)
```

### Tracker

```text
Do various torrent category operations
Usage:
qbt torrent tracker [command]
Available Commands:
edit Edit torrent tracker
Flags:
-h, --help help for tracker
Global Flags:
--config string config file (default is $HOME/.config/qbt/.qbt.toml)
Use "qbt torrent tracker [command] --help" for more information about a command.
```

#### Tracker edit

```text
Edit tracker for torrents via hashes
Usage:
qbt torrent tracker edit [flags]
Examples:
qbt torrent tracker edit --old url.old/test --new url.com/test
Flags:
--dry-run Run without doing anything
-h, --help help for edit
--new string New tracker URL
--old string Old tracker URL to replace
Global Flags:
--config string config file (default is $HOME/.config/qbt/.qbt.toml)
```

## Version
Expand Down
1 change: 1 addition & 0 deletions cmd/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func RunTorrent() *cobra.Command {
command.AddCommand(RunTorrentReannounce())
command.AddCommand(RunTorrentRemove())
command.AddCommand(RunTorrentResume())
command.AddCommand(RunTorrentTracker())

return command
}
96 changes: 96 additions & 0 deletions cmd/torrent_tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cmd

import (
"fmt"
"log"
"os"
"strings"

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

"github.com/autobrr/go-qbittorrent"
"github.com/spf13/cobra"
)

// RunTorrentTracker cmd for torrent tracker operations
func RunTorrentTracker() *cobra.Command {
var command = &cobra.Command{
Use: "tracker",
Short: "Torrent tracker subcommand",
Long: `Do various torrent category operations`,
}

command.AddCommand(RunTorrentTrackerEdit())

return command
}

// RunTorrentTrackerEdit cmd for torrent tracker operations
func RunTorrentTrackerEdit() *cobra.Command {
var command = &cobra.Command{
Use: "edit",
Short: "Edit torrent tracker",
Long: `Edit tracker for torrents via hashes`,
Example: ` qbt torrent tracker edit --old url.old/test --new url.com/test`,
}

var (
dry bool
oldURL string
newURL string
)

command.Flags().BoolVar(&dry, "dry-run", false, "Run without doing anything")
command.Flags().StringVar(&oldURL, "old", "", "Old tracker URL to replace")
command.Flags().StringVar(&newURL, "new", "", "New tracker URL")

command.RunE = func(cmd *cobra.Command, args []string) error {
config.InitConfig()

qbtSettings := qbittorrent.Config{
Host: config.Qbit.Addr,
Username: config.Qbit.Login,
Password: config.Qbit.Password,
BasicUser: config.Qbit.BasicUser,
BasicPass: config.Qbit.BasicPass,
}

qb := qbittorrent.NewClient(qbtSettings)

ctx := cmd.Context()

if err := qb.LoginCtx(ctx); err != nil {
fmt.Fprintf(os.Stderr, "could not login to qbit: %q\n", err)
os.Exit(1)
}

if dry {
log.Printf("dry-run: successfully updated tracker on torrents\n")

return nil
} else {
torrents, err := qb.GetTorrentsCtx(ctx, qbittorrent.TorrentFilterOptions{})
if err != nil {
log.Fatalf("could not get torrents err: %q\n", err)
}

matches := 0

for _, torrent := range torrents {
if strings.Contains(torrent.Tracker, oldURL) {
if err := qb.EditTrackerCtx(ctx, torrent.Hash, torrent.Tracker, newURL); err != nil {
log.Fatalf("could not edit tracker for torrent: %s\n", torrent.Hash)
}

matches++
}
}

log.Printf("successfully updated tracker for (%d) torrents\n", matches)
}

return nil
}

return command
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/anacrolix/torrent v1.52.5
github.com/autobrr/go-qbittorrent v1.4.0
github.com/autobrr/go-qbittorrent v1.5.0
github.com/blang/semver v3.5.1+incompatible
github.com/dustin/go-humanize v1.0.1
github.com/magiconair/properties v1.8.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHG
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/autobrr/go-qbittorrent v1.4.0 h1:f8FhLB5IVprQ7qATVrJkQxT88gi1Soqz2wa+YCxmI88=
github.com/autobrr/go-qbittorrent v1.4.0/go.mod h1:z88B3+O/1/3doQABErvIOOxE4hjpmIpulu6XzDG/q78=
github.com/autobrr/go-qbittorrent v1.5.0 h1:UBl6w/bxxT5v1d9sGKp5Be4js4YNNumXw4hZNXXxPd8=
github.com/autobrr/go-qbittorrent v1.5.0/go.mod h1:z88B3+O/1/3doQABErvIOOxE4hjpmIpulu6XzDG/q78=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/benbjohnson/immutable v0.2.0/go.mod h1:uc6OHo6PN2++n98KHLxW8ef4W42ylHiQSENghE1ezxI=
Expand Down

0 comments on commit 023e947

Please sign in to comment.