diff --git a/rtorrent/rtorrent.go b/rtorrent/rtorrent.go index e0fe946..e8b7146 100644 --- a/rtorrent/rtorrent.go +++ b/rtorrent/rtorrent.go @@ -3,6 +3,7 @@ package rtorrent import ( "fmt" "net/http" + "time" "github.com/mrobinsn/go-rtorrent/xmlrpc" "github.com/pkg/errors" @@ -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 @@ -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" @@ -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 { @@ -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), }) } } @@ -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 } diff --git a/rtorrent/rtorrent_test.go b/rtorrent/rtorrent_test.go index 257c26b..e779034 100644 --- a/rtorrent/rtorrent_test.go +++ b/rtorrent/rtorrent_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) @@ -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) @@ -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) { @@ -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) @@ -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]) diff --git a/rtorrent/testdata/ubuntu-20.04.1-live-server-amd64.iso.torrent b/rtorrent/testdata/ubuntu-20.04.1-live-server-amd64.iso.torrent deleted file mode 100644 index 882cf3b..0000000 Binary files a/rtorrent/testdata/ubuntu-20.04.1-live-server-amd64.iso.torrent and /dev/null differ diff --git a/rtorrent/testdata/ubuntu-20.04.2-live-server-arm64.iso.torrent b/rtorrent/testdata/ubuntu-20.04.2-live-server-arm64.iso.torrent new file mode 100644 index 0000000..2f1eeb3 Binary files /dev/null and b/rtorrent/testdata/ubuntu-20.04.2-live-server-arm64.iso.torrent differ diff --git a/test.sh b/test.sh index 174b2f7..d026334 100755 --- a/test.sh +++ b/test.sh @@ -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 ./...