mirror of
https://github.com/simon-ding/polaris.git
synced 2026-05-29 05:57:46 +08:00
implement download feature
This commit is contained in:
@@ -2,6 +2,8 @@ package transmission
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/hekmon/transmissionrpc"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -22,7 +24,48 @@ type Client struct {
|
||||
c *transmissionrpc.Client
|
||||
}
|
||||
|
||||
func (c *Client) Download(magnet string) (*transmissionrpc.Torrent, error) {
|
||||
t, err := c.c.TorrentAdd(&transmissionrpc.TorrentAddPayload{Filename: &magnet})
|
||||
return t, err
|
||||
func (c *Client) Download(magnet, dir string) (*Torrent, error) {
|
||||
t, err := c.c.TorrentAdd(&transmissionrpc.TorrentAddPayload{
|
||||
Filename: &magnet,
|
||||
DownloadDir: &dir,
|
||||
})
|
||||
return &Torrent{
|
||||
t: t,
|
||||
c: c.c,
|
||||
}, err
|
||||
}
|
||||
|
||||
type Torrent struct {
|
||||
t *transmissionrpc.Torrent
|
||||
c *transmissionrpc.Client
|
||||
}
|
||||
|
||||
func (t *Torrent) Name() string {
|
||||
return *t.t.Name
|
||||
}
|
||||
|
||||
func (t *Torrent) Progress() int {
|
||||
if *t.t.IsFinished {
|
||||
return 100
|
||||
}
|
||||
return int(*t.t.PercentDone*100)
|
||||
}
|
||||
|
||||
func (t *Torrent) Stop() error {
|
||||
return t.c.TorrentStopIDs([]int64{*t.t.ID})
|
||||
}
|
||||
|
||||
func (t *Torrent) Start() error {
|
||||
return t.c.TorrentStartIDs([]int64{*t.t.ID})
|
||||
}
|
||||
|
||||
func (t *Torrent) Remove() error {
|
||||
return t.c.TorrentRemove(&transmissionrpc.TorrentRemovePayload{
|
||||
IDs: []int64{*t.t.ID},
|
||||
DeleteLocalData: true,
|
||||
})
|
||||
}
|
||||
|
||||
func (t *Torrent) Save() string {
|
||||
return strconv.Itoa(int(*t.t.ID))
|
||||
}
|
||||
Reference in New Issue
Block a user