Skip to content

Commit

Permalink
Merge pull request #12 from mrobinsn/single-get
Browse files Browse the repository at this point in the history
Single Get
  • Loading branch information
Michael Robinson authored Aug 18, 2019
2 parents 9500743 + 855f469 commit 3d733b4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
## Features
- Get IP, Name, Up/Down totals
- Get torrents within a view
- Get torrent by hash
- Get files for torrents
- Set the label on a torrent
- Add a torrent by URL or by metadata
Expand Down
43 changes: 43 additions & 0 deletions rtorrent/rtorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,49 @@ func (r *RTorrent) GetTorrents(view View) ([]Torrent, error) {
return torrents, nil
}

// GetTorrent returns the torrent identified by the given hash
func (r *RTorrent) GetTorrent(hash string) (Torrent, error) {
var t Torrent
t.Hash = hash
// Name
results, err := r.xmlrpcClient.Call("d.name", t.Hash)
if err != nil {
return t, errors.Wrap(err, "d.name XMLRPC call failed")
}
t.Name = results.([]interface{})[0].(string)
// Size
results, err = r.xmlrpcClient.Call("d.size_bytes", t.Hash)
if err != nil {
return t, errors.Wrap(err, "d.size_bytes XMLRPC call failed")
}
t.Size = results.([]interface{})[0].(int)
// Label
results, err = r.xmlrpcClient.Call("d.custom1", t.Hash)
if err != nil {
return t, errors.Wrap(err, "d.custom1 XMLRPC call failed")
}
t.Label = results.([]interface{})[0].(string)
// Path
results, err = r.xmlrpcClient.Call("d.base_path", t.Hash)
if err != nil {
return t, errors.Wrap(err, "d.base_path XMLRPC call failed")
}
t.Path = results.([]interface{})[0].(string)
// Completed
results, err = r.xmlrpcClient.Call("d.complete", t.Hash)
if err != nil {
return t, errors.Wrap(err, "d.complete XMLRPC call failed")
}
t.Completed = results.([]interface{})[0].(int) > 0
// Ratio
results, err = r.xmlrpcClient.Call("d.ratio", t.Hash)
if err != nil {
return t, errors.Wrap(err, "d.ratio XMLRPC call failed")
}
t.Ratio = float64(results.([]interface{})[0].(int)) / float64(1000)
return t, nil
}

// Delete removes the torrent
func (r *RTorrent) Delete(t Torrent) error {
_, err := r.xmlrpcClient.Call("d.erase", t.Hash)
Expand Down
29 changes: 19 additions & 10 deletions rtorrent/rtorrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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.10/ubuntu-18.10-live-server-amd64.iso.torrent")
err := client.Add("http://releases.ubuntu.com/19.04/ubuntu-19.04-live-server-amd64.iso.torrent")
require.NoError(t, err)

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

t.Run("get files", func(t *testing.T) {
Expand All @@ -101,6 +101,15 @@ func TestRTorrent(t *testing.T) {
}
})

t.Run("single get", func(t *testing.T) {
torrent, err := client.GetTorrent(torrents[0].Hash)
require.NoError(t, err)
require.NotEmpty(t, torrent.Hash)
require.NotEmpty(t, torrent.Name)
require.NotEmpty(t, torrent.Path)
require.NotEmpty(t, torrent.Size)
})

t.Run("change label", func(t *testing.T) {
err := client.SetLabel(torrents[0], "TestLabel")
require.NoError(t, err)
Expand Down Expand Up @@ -182,7 +191,7 @@ func TestRTorrent(t *testing.T) {
})

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

Expand All @@ -208,11 +217,11 @@ func TestRTorrent(t *testing.T) {
}
require.NotEmpty(t, torrents)
require.Len(t, torrents, 1)
require.Equal(t, "8C4ADBF9EBE66F1D804FB6A4FB9B74966C3AB609", torrents[0].Hash)
require.Equal(t, "ubuntu-18.10-live-server-amd64.iso", torrents[0].Name)
require.Equal(t, "B7B0FBAB74A85D4AC170662C645982A862826455", torrents[0].Hash)
require.Equal(t, "ubuntu-19.04-live-server-amd64.iso", torrents[0].Name)
require.Equal(t, "", torrents[0].Label)
require.Equal(t, 923795456, torrents[0].Size)
require.Equal(t, "/downloads/incoming/ubuntu-18.10-live-server-amd64.iso", torrents[0].Path)
require.Equal(t, 784334848, torrents[0].Size)
require.Equal(t, "/downloads/incoming/ubuntu-19.04-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.

0 comments on commit 3d733b4

Please sign in to comment.