Skip to content
This repository has been archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
Merge pull request #8 from mrobinsn/update
Browse files Browse the repository at this point in the history
Update for latest rtorrent API
  • Loading branch information
Michael Robinson authored Mar 27, 2019
2 parents 228777c + 45fe042 commit 9202379
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 77 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ USAGE:
go-rtorrent [global options] command [command options] [arguments...]
VERSION:
0.4.0
1.0.0
AUTHOR(S):
Michael Robinson <[email protected]>
Michael Robinson <[email protected]>
COMMANDS:
get-ip retrieves the IP for this rTorrent instance
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/mrobinsn/go-rtorrent

require (
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
github.com/urfave/cli v1.20.0
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
9 changes: 2 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"fmt"
"os"

"github.com/apex/log"
"github.com/mrobinsn/go-rtorrent/rtorrent"
"github.com/pkg/errors"
"github.com/urfave/cli"
)

var (
name = "rTorrent XMLRPC CLI"
version = "0.5.0"
version = "1.0.0"
app = initApp()
conn *rtorrent.RTorrent

Expand Down Expand Up @@ -92,7 +91,7 @@ func initApp() *cli.App {

func main() {
if err := app.Run(os.Args); err != nil {
log.WithError(err).Error("failure")
fmt.Println(err)
}
}

Expand Down Expand Up @@ -155,10 +154,6 @@ func getFiles(c *cli.Context) error {
if err != nil {
return errors.Wrap(err, "failed to get files")
}
log.WithFields(log.Fields{
"torrent_hash": hash,
"num": len(files),
}).Info("found files")
for _, file := range files {
fmt.Println(file.Pretty())
}
Expand Down
36 changes: 18 additions & 18 deletions rtorrent/rtorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,27 @@ func (r *RTorrent) WithHTTPClient(client *http.Client) *RTorrent {

// Add adds a new torrent by URL
func (r *RTorrent) Add(url string) error {
_, err := r.xmlrpcClient.Call("load_start", url)
_, err := r.xmlrpcClient.Call("load.start", "", url)
if err != nil {
return errors.Wrap(err, "load_start XMLRPC call failed")
return errors.Wrap(err, "load.start XMLRPC call failed")
}
return nil
}

// AddTorrent adds a new torrent by the torrent files data
func (r *RTorrent) AddTorrent(data []byte) error {
_, err := r.xmlrpcClient.Call("load_raw_start", data)
_, err := r.xmlrpcClient.Call("load.raw_start", "", data)
if err != nil {
return errors.Wrap(err, "load_raw_start XMLRPC call failed")
return errors.Wrap(err, "load.raw_start XMLRPC call failed")
}
return nil
}

// IP returns the IP reported by this RTorrent instance
func (r *RTorrent) IP() (string, error) {
result, err := r.xmlrpcClient.Call("get_ip")
result, err := r.xmlrpcClient.Call("network.bind_address")
if err != nil {
return "", errors.Wrap(err, "get_ip XMLRPC call failed")
return "", errors.Wrap(err, "network.bind_address XMLRPC call failed")
}
if ips, ok := result.([]interface{}); ok {
result = ips[0]
Expand All @@ -107,9 +107,9 @@ func (r *RTorrent) IP() (string, error) {

// Name returns the name reported by this RTorrent instance
func (r *RTorrent) Name() (string, error) {
result, err := r.xmlrpcClient.Call("get_name")
result, err := r.xmlrpcClient.Call("system.hostname")
if err != nil {
return "", errors.Wrap(err, "get_name XMLRPC call failed")
return "", errors.Wrap(err, "system.hostname XMLRPC call failed")
}
if names, ok := result.([]interface{}); ok {
result = names[0]
Expand All @@ -122,9 +122,9 @@ func (r *RTorrent) Name() (string, error) {

// DownTotal returns the total downloaded metric reported by this RTorrent instance (bytes)
func (r *RTorrent) DownTotal() (int, error) {
result, err := r.xmlrpcClient.Call("get_down_total")
result, err := r.xmlrpcClient.Call("throttle.global_down.total")
if err != nil {
return 0, errors.Wrap(err, "get_down_total XMLRPC call failed")
return 0, errors.Wrap(err, "throttle.global_down.total XMLRPC call failed")
}
if totals, ok := result.([]interface{}); ok {
result = totals[0]
Expand All @@ -137,9 +137,9 @@ func (r *RTorrent) DownTotal() (int, error) {

// UpTotal returns the total uploaded metric reported by this RTorrent instance (bytes)
func (r *RTorrent) UpTotal() (int, error) {
result, err := r.xmlrpcClient.Call("get_up_total")
result, err := r.xmlrpcClient.Call("throttle.global_up.total")
if err != nil {
return 0, errors.Wrap(err, "get_up_total XMLRPC call failed")
return 0, errors.Wrap(err, "throttle.global_up.total XMLRPC call failed")
}
if totals, ok := result.([]interface{}); ok {
result = totals[0]
Expand All @@ -152,11 +152,11 @@ func (r *RTorrent) UpTotal() (int, error) {

// GetTorrents returns all of the torrents reported by this RTorrent instance
func (r *RTorrent) GetTorrents(view View) ([]Torrent, error) {
args := []interface{}{string(view), "d.get_name=", "d.get_size_bytes=", "d.get_hash=", "d.get_custom1=", "d.get_base_path=", "d.is_active=", "d.get_complete=", "d.get_ratio="}
results, err := r.xmlrpcClient.Call("d.multicall", args...)
args := []interface{}{"", string(view), "d.name=", "d.size_bytes=", "d.hash=", "d.custom1=", "d.base_path=", "d.is_active=", "d.complete=", "d.ratio="}
results, err := r.xmlrpcClient.Call("d.multicall2", args...)
var torrents []Torrent
if err != nil {
return torrents, errors.Wrap(err, "d.multicall XMLRPC call failed")
return torrents, errors.Wrap(err, "d.multicall2 XMLRPC call failed")
}
for _, outerResult := range results.([]interface{}) {
for _, innerResult := range outerResult.([]interface{}) {
Expand Down Expand Up @@ -186,7 +186,7 @@ func (r *RTorrent) Delete(t Torrent) error {

// GetFiles returns all of the files for a given `Torrent`
func (r *RTorrent) GetFiles(t Torrent) ([]File, error) {
args := []interface{}{t.Hash, 0, "f.get_path=", "f.get_size_bytes="}
args := []interface{}{t.Hash, 0, "f.path=", "f.size_bytes="}
results, err := r.xmlrpcClient.Call("f.multicall", args...)
var files []File
if err != nil {
Expand All @@ -208,8 +208,8 @@ func (r *RTorrent) GetFiles(t Torrent) ([]File, error) {
func (r *RTorrent) SetLabel(t Torrent, newLabel string) error {
t.Label = newLabel
args := []interface{}{t.Hash, newLabel}
if _, err := r.xmlrpcClient.Call("d.set_custom1", args...); err != nil {
return errors.Wrap(err, "d.set_custom1 XMLRPC call failed")
if _, err := r.xmlrpcClient.Call("d.custom1.set", args...); err != nil {
return errors.Wrap(err, "d.custom1.set XMLRPC call failed")
}
return nil
}
20 changes: 10 additions & 10 deletions rtorrent/rtorrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestRTorrent(t *testing.T) {

t.Run("add", func(t *testing.T) {
t.Run("by url", func(t *testing.T) {
err := client.Add("http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso.torrent")
err := client.Add("http://releases.ubuntu.com/18.10/ubuntu-18.10-live-server-amd64.iso.torrent")
require.NoError(t, err)

t.Run("get torrent", func(t *testing.T) {
Expand All @@ -71,11 +71,11 @@ func TestRTorrent(t *testing.T) {
}
require.NotEmpty(t, torrents)
require.Len(t, torrents, 1)
require.Equal(t, "DDEE5CB75C12F3165EF79A12A5CD6158BEF029AD", torrents[0].Hash)
require.Equal(t, "ubuntu-18.04-live-server-amd64.iso", torrents[0].Name)
require.Equal(t, "8C4ADBF9EBE66F1D804FB6A4FB9B74966C3AB609", torrents[0].Hash)
require.Equal(t, "ubuntu-18.10-live-server-amd64.iso", torrents[0].Name)
require.Equal(t, "", torrents[0].Label)
require.Equal(t, 845152256, torrents[0].Size)
require.Equal(t, "/downloads/incoming/ubuntu-18.04-live-server-amd64.iso", torrents[0].Path)
require.Equal(t, 923795456, torrents[0].Size)
require.Equal(t, "/downloads/incoming/ubuntu-18.10-live-server-amd64.iso", torrents[0].Path)
require.False(t, torrents[0].Completed)

t.Run("get files", func(t *testing.T) {
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestRTorrent(t *testing.T) {
})

t.Run("with data", func(t *testing.T) {
b, err := ioutil.ReadFile("testdata/ubuntu-18.04-live-server-amd64.iso.torrent")
b, err := ioutil.ReadFile("testdata/ubuntu-18.10-live-server-amd64.iso.torrent")
require.NoError(t, err)
require.NotEmpty(t, b)

Expand All @@ -169,11 +169,11 @@ func TestRTorrent(t *testing.T) {
}
require.NotEmpty(t, torrents)
require.Len(t, torrents, 1)
require.Equal(t, "DDEE5CB75C12F3165EF79A12A5CD6158BEF029AD", torrents[0].Hash)
require.Equal(t, "ubuntu-18.04-live-server-amd64.iso", torrents[0].Name)
require.Equal(t, "8C4ADBF9EBE66F1D804FB6A4FB9B74966C3AB609", torrents[0].Hash)
require.Equal(t, "ubuntu-18.10-live-server-amd64.iso", torrents[0].Name)
require.Equal(t, "", torrents[0].Label)
require.Equal(t, 845152256, torrents[0].Size)
require.Equal(t, "/downloads/incoming/ubuntu-18.04-live-server-amd64.iso", torrents[0].Path)
require.Equal(t, 923795456, torrents[0].Size)
require.Equal(t, "/downloads/incoming/ubuntu-18.10-live-server-amd64.iso", torrents[0].Path)
require.False(t, torrents[0].Completed)

t.Run("get files", func(t *testing.T) {
Expand Down
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 9202379

Please sign in to comment.