implement download feature

This commit is contained in:
Simon Ding
2024-07-10 17:35:16 +08:00
parent d7d5c72518
commit e500aaed1e
43 changed files with 4903 additions and 227 deletions

View File

@@ -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))
}