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

Add Created, Started, and Finished time fields to Torrent #17

Merged
merged 2 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion rtorrent/rtorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rtorrent
import (
"fmt"
"net/http"
"time"

"github.com/mrobinsn/go-rtorrent/xmlrpc"
"github.com/pkg/errors"
Expand All @@ -29,6 +30,9 @@ type Torrent struct {
Label string
Completed bool
Ratio float64
Created time.Time
Started time.Time
Finished time.Time
}

// Status represents the status of a torrent
Expand Down Expand Up @@ -87,6 +91,12 @@ const (
DDownRate Field = "d.down.rate"
// DUpRate represents the upload rate of the "Downloading Item"
DUpRate Field = "d.up.rate"
// DCreationTime represents the date the torrent was created
DCreationTime Field = "d.creation_date"
// DFinishedTime represents the date the torrent finished downloading
DFinishedTime Field = "d.timestamp.finished"
// DStartedTime represents the date the torrent started downloading
DStartedTime Field = "d.timestamp.started"

// FPath represents the path of a "File Item"
FPath Field = "f.path"
Expand Down Expand Up @@ -313,7 +323,7 @@ func (r *RTorrent) UpRate() (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), DName.Query(), DSizeInBytes.Query(), DHash.Query(), DLabel.Query(), DBasePath.Query(), DIsActive.Query(), DComplete.Query(), DRatio.Query()}
args := []interface{}{"", string(view), DName.Query(), DSizeInBytes.Query(), DHash.Query(), DLabel.Query(), DBasePath.Query(), DIsActive.Query(), DComplete.Query(), DRatio.Query(), DCreationTime.Query(), DFinishedTime.Query(), DStartedTime.Query()}
results, err := r.xmlrpcClient.Call("d.multicall2", args...)
var torrents []Torrent
if err != nil {
Expand All @@ -330,6 +340,9 @@ func (r *RTorrent) GetTorrents(view View) ([]Torrent, error) {
Label: torrentData[3].(string),
Completed: torrentData[6].(int) > 0,
Ratio: float64(torrentData[7].(int)) / float64(1000),
Created: time.Unix(int64(torrentData[8].(int)), 0),
Finished: time.Unix(int64(torrentData[9].(int)), 0),
Started: time.Unix(int64(torrentData[10].(int)), 0),
})
}
}
Expand Down Expand Up @@ -376,6 +389,25 @@ func (r *RTorrent) GetTorrent(hash string) (Torrent, error) {
return t, errors.Wrap(err, "d.ratio XMLRPC call failed")
}
t.Ratio = float64(results.([]interface{})[0].(int)) / float64(1000)
// Created
results, err = r.xmlrpcClient.Call(string(DCreationTime), t.Hash)
if err != nil {
return t, errors.Wrap(err, fmt.Sprintf("%s XMLRPC call failed", string(DCreationTime)))
}
t.Created = time.Unix(int64(results.([]interface{})[0].(int)), 0)
// Finished
results, err = r.xmlrpcClient.Call(string(DFinishedTime), t.Hash)
if err != nil {
return t, errors.Wrap(err, fmt.Sprintf("%s XMLRPC call failed", string(DFinishedTime)))
}
t.Finished = time.Unix(int64(results.([]interface{})[0].(int)), 0)
// Started
results, err = r.xmlrpcClient.Call(string(DStartedTime), t.Hash)
if err != nil {
return t, errors.Wrap(err, fmt.Sprintf("%s XMLRPC call failed", string(DStartedTime)))
}
t.Created = time.Unix(int64(results.([]interface{})[0].(int)), 0)

return t, nil
}

Expand Down
36 changes: 18 additions & 18 deletions rtorrent/rtorrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestRTorrent(t *testing.T) {

t.Run("add", func(t *testing.T) {
t.Run("by url", func(t *testing.T) {
err := client.Add("https://releases.ubuntu.com/20.04/ubuntu-20.04.1-live-server-amd64.iso.torrent")
err := client.Add("https://torrent.ubuntu.com/releases/focal/release/live-server/ubuntu-20.04.2-live-server-arm64.iso.torrent")
require.NoError(t, err)

t.Run("get torrent", func(t *testing.T) {
Expand All @@ -82,11 +82,11 @@ func TestRTorrent(t *testing.T) {
}
require.NotEmpty(t, torrents)
require.Len(t, torrents, 1)
require.Equal(t, "36C67464C37A83478CEFF54932B5A9BDDEA636F3", torrents[0].Hash)
require.Equal(t, "ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Name)
require.Equal(t, "B185765BE59BC56AB29A4860A5820863BF0512AD", torrents[0].Hash)
require.Equal(t, "ubuntu-20.04.2-live-server-arm64.iso", torrents[0].Name)
require.Equal(t, "", torrents[0].Label)
require.Equal(t, 958398464, torrents[0].Size)
require.Equal(t, "/downloads/incoming/ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Path)
require.Equal(t, 1193754624, torrents[0].Size)
require.Equal(t, "/downloads/incoming/ubuntu-20.04.2-live-server-arm64.iso", torrents[0].Path)
require.False(t, torrents[0].Completed)

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

t.Run("by url (stopped)", func(t *testing.T) {
label := DLabel.SetValue("test-label")
err := client.AddStopped("http://releases.ubuntu.com/19.04/ubuntu-19.04-live-server-amd64.iso.torrent", label)
err := client.AddStopped("https://torrent.ubuntu.com/releases/focal/release/live-server/ubuntu-20.04.2-live-server-arm64.iso.torrent", label)
require.NoError(t, err)

t.Run("get torrent", func(t *testing.T) {
Expand All @@ -214,10 +214,10 @@ func TestRTorrent(t *testing.T) {
}
require.NotEmpty(t, torrents)
require.Len(t, torrents, 1)
require.Equal(t, "B7B0FBAB74A85D4AC170662C645982A862826455", torrents[0].Hash)
require.Equal(t, "ubuntu-19.04-live-server-amd64.iso", torrents[0].Name)
require.Equal(t, "B185765BE59BC56AB29A4860A5820863BF0512AD", torrents[0].Hash)
require.Equal(t, "ubuntu-20.04.2-live-server-arm64.iso", torrents[0].Name)
require.Equal(t, label.Value, torrents[0].Label)
require.Equal(t, 784334848, torrents[0].Size)
require.Equal(t, 1193754624, torrents[0].Size)
//no path yet since the torrent is stopped
require.Equal(t, "", torrents[0].Path)
require.False(t, torrents[0].Completed)
Expand Down Expand Up @@ -266,7 +266,7 @@ func TestRTorrent(t *testing.T) {
})

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

Expand All @@ -292,11 +292,11 @@ func TestRTorrent(t *testing.T) {
}
require.NotEmpty(t, torrents)
require.Len(t, torrents, 1)
require.Equal(t, "36C67464C37A83478CEFF54932B5A9BDDEA636F3", torrents[0].Hash)
require.Equal(t, "ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Name)
require.Equal(t, "B185765BE59BC56AB29A4860A5820863BF0512AD", torrents[0].Hash)
require.Equal(t, "ubuntu-20.04.2-live-server-arm64.iso", torrents[0].Name)
require.Equal(t, "", torrents[0].Label)
require.Equal(t, 958398464, torrents[0].Size)
require.Equal(t, "/downloads/incoming/ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Path)
require.Equal(t, 1193754624, torrents[0].Size)
require.Equal(t, "/downloads/incoming/ubuntu-20.04.2-live-server-arm64.iso", torrents[0].Path)
require.False(t, torrents[0].Completed)

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

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

Expand All @@ -358,10 +358,10 @@ func TestRTorrent(t *testing.T) {

require.NotEmpty(t, torrents)
require.Len(t, torrents, 1)
require.Equal(t, "36C67464C37A83478CEFF54932B5A9BDDEA636F3", torrents[0].Hash)
require.Equal(t, "ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Name)
require.Equal(t, "B185765BE59BC56AB29A4860A5820863BF0512AD", torrents[0].Hash)
require.Equal(t, "ubuntu-20.04.2-live-server-arm64.iso", torrents[0].Name)
require.Equal(t, label.Value, torrents[0].Label)
require.Equal(t, 958398464, torrents[0].Size)
require.Equal(t, 1193754624, torrents[0].Size)

t.Run("delete torrent", func(t *testing.T) {
err := client.Delete(torrents[0])
Expand Down
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ docker rm -f rutorrent
rm -rf tmp
mkdir tmp
docker run -d --name=rutorrent -v $(pwd)/tmp/data:/config -v $(pwd)/tmp/downloads:/downloads -e PGID=1000 -e PUID=1000 -p 80:80 -p 5000:5000 -p 51413:51413 -p 6881:6881/udp linuxserver/rutorrent
sleep 5
sleep 60
go test -v -race ./...