Skip to content

Commit

Permalink
v4.0 api
Browse files Browse the repository at this point in the history
  • Loading branch information
penwu committed Jan 14, 2023
1 parent 9b9733d commit 77b2f0f
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# VSCode local settings
.vscode
.idea

# Vendoring
vendor/
Expand Down
2 changes: 1 addition & 1 deletion controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
defaultPort = 9091
defaultRPCPath = "/transmission/rpc"
defaultTimeout = 30 * time.Second
defaultUserAgent = "github.com/hekmon/transmissionrpc"
defaultUserAgent = "github.com/penwyp/transmissionrpc"
)

// Client is the base object to interract with a remote transmission rpc endpoint.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/hekmon/transmissionrpc/v2
module github.com/penwyp/transmissionrpc/v2

go 1.13

Expand Down
5 changes: 2 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"sync"
Expand Down Expand Up @@ -101,13 +100,13 @@ func (c *Client) request(ctx context.Context, method string, arguments interface
// Debug
if c.debug {
var data []byte
if data, err = ioutil.ReadAll(resp.Body); err == nil {
if data, err = io.ReadAll(resp.Body); err == nil {
fmt.Fprintln(os.Stderr, string(data))
} else {
fmt.Fprintln(os.Stderr, err.Error())
}
resp.Body.Close()
resp.Body = ioutil.NopCloser(bytes.NewBuffer(data))
resp.Body = io.NopCloser(bytes.NewBuffer(data))
}
// Decode body
answer := answerPayload{
Expand Down
26 changes: 12 additions & 14 deletions torrent_accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,17 @@ func (t *Torrent) UnmarshalJSON(data []byte) (err error) {
// Shadow real type for regular unmarshalling
type RawTorrent Torrent
tmp := &struct {
ActivityDate *int64 `json:"activityDate"`
AddedDate *int64 `json:"addedDate"`
DateCreated *int64 `json:"dateCreated"`
DoneDate *int64 `json:"doneDate"`
EditDate *int64 `json:"editDate"`
PieceSize *int64 `json:"pieceSize"`
SecondsSeeding *int64 `json:"secondsSeeding"`
SizeWhenDone *int64 `json:"sizeWhenDone"`
StartDate *int64 `json:"startDate"`
TotalSize *int64 `json:"totalSize"`
Wanted []int64 `json:"wanted"` // boolean in number form
ActivityDate *int64 `json:"activityDate"`
AddedDate *int64 `json:"addedDate"`
DateCreated *int64 `json:"dateCreated"`
DoneDate *int64 `json:"doneDate"`
EditDate *int64 `json:"editDate"`
PieceSize *int64 `json:"pieceSize"`
SecondsSeeding *int64 `json:"secondsSeeding"`
SizeWhenDone *int64 `json:"sizeWhenDone"`
StartDate *int64 `json:"startDate"`
TotalSize *int64 `json:"totalSize"`
Wanted []bool `json:"wanted"` // boolean in number form
*RawTorrent
}{
RawTorrent: (*RawTorrent)(t),
Expand Down Expand Up @@ -281,10 +281,8 @@ func (t *Torrent) UnmarshalJSON(data []byte) (err error) {
if tmp.Wanted != nil {
t.Wanted = make([]bool, len(tmp.Wanted))
for index, value := range tmp.Wanted {
if value == 1 {
if value {
t.Wanted[index] = true
} else if value != 0 {
return fmt.Errorf("can't convert wanted index %d value '%d' as boolean", index, value)
}
}
}
Expand Down
155 changes: 155 additions & 0 deletions torrent_accessors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package transmissionrpc

import (
"encoding/json"
"log"
"testing"
)

func Test_xx(t *testing.T) {

body := `
{
"activityDate": 1668231638,
"addedDate": 1665413621,
"bandwidthPriority": 0,
"comment": "",
"corruptEver": 0,
"creator": "uTorrent/2210",
"dateCreated": 1452811090,
"desiredAvailable": 0,
"doneDate": 0,
"downloadDir": "/volume2/pt/\u5176\u4ed6/\u5b66\u4e60",
"downloadLimit": 1,
"downloadLimited": false,
"downloadedEver": 0,
"editDate": 0,
"error": 2,
"errorString": "Tracker HTTP response 502 (Bad Gateway",
"eta": -1,
"etaIdle": -1,
"fileStats": [
{
"bytesCompleted": 42087610,
"priority": 0,
"wanted": true
}
],
"files": [
{
"bytesCompleted": 42087610,
"length": 42087610,
"name": "\u300a\u4e09\u8054\u751f\u6d3b\u5468\u520a\u300b2016\u5e74\u7b2c01\u671f\uff08\u672a\u6765\u7b80\u53f2\uff09.PDF"
}
],
"hashString": "00155acc9a607671ecb63fe94e20a5202881c35f",
"haveUnchecked": 0,
"haveValid": 42087610,
"honorsSessionLimits": true,
"id": 4528,
"isFinished": false,
"isPrivate": true,
"isStalled": false,
"labels": [],
"leftUntilDone": 0,
"magnetLink": "magnet:?xt=urn:btih:00155acc9a607671ecb63fe94e20a5202881c35f&dn=%E3%80%8A%E4%B8%89%E8%81%94%E7%94%9F%E6%B4%BB%E5%91%A8%E5%88%8A%E3%80%8B2016%E5%B9%B4%E7%AC%AC01%E6%9C%9F%EF%BC%88%E6%9C%AA%E6%9D%A5%E7%AE%80%E5%8F%B2%EF%BC%89.PDF&tr=https%3A%2F%2Ftracker.m-team.cc%2Fannounce.php%3Fpasskey%3D1b376abe27739b012fb9b5a226452d13",
"manualAnnounceTime": -1,
"maxConnectedPeers": 50,
"metadataPercentComplete": 1,
"name": "\u300a\u4e09\u8054\u751f\u6d3b\u5468\u520a\u300b2016\u5e74\u7b2c01\u671f\uff08\u672a\u6765\u7b80\u53f2\uff09.PDF",
"peer-limit": 50,
"peers": [],
"peersConnected": 0,
"peersFrom": {
"fromCache": 0,
"fromDht": 0,
"fromIncoming": 0,
"fromLpd": 0,
"fromLtep": 0,
"fromPex": 0,
"fromTracker": 0
},
"peersGettingFromUs": 0,
"peersSendingToUs": 0,
"percentDone": 1,
"pieceCount": 643,
"pieceSize": 65536,
"pieces": "///////////////////////////////////////////////////////////////////////////////////////////////////////////g",
"priorities": [
0
],
"queuePosition": 4527,
"rateDownload": 0,
"rateUpload": 0,
"recheckProgress": 0,
"secondsDownloading": 1154,
"secondsSeeding": 1267393,
"seedIdleLimit": 30,
"seedIdleMode": 0,
"seedRatioLimit": 2,
"seedRatioMode": 0,
"sizeWhenDone": 42087610,
"startDate": 1673714118,
"status": 6,
"torrentFile": "/config/torrents/00155acc9a607671ecb63fe94e20a5202881c35f.torrent",
"totalSize": 42087610,
"trackerStats": [
{
"announce": "https://tracker.m-team.cc/announce.php?passkey=1b376abe27739b012fb9b5a226452d13",
"announceState": 1,
"downloadCount": -1,
"hasAnnounced": true,
"hasScraped": true,
"host": "tracker.m-team.cc:443",
"id": 4536,
"isBackup": false,
"lastAnnouncePeerCount": 0,
"lastAnnounceResult": "Tracker HTTP response 502 (Bad Gateway",
"lastAnnounceStartTime": 0,
"lastAnnounceSucceeded": false,
"lastAnnounceTime": 1673714327,
"lastAnnounceTimedOut": false,
"lastScrapeResult": "Tracker HTTP response 502 (Bad Gateway)",
"lastScrapeStartTime": 0,
"lastScrapeSucceeded": false,
"lastScrapeTime": 1673714511,
"lastScrapeTimedOut": false,
"leecherCount": -1,
"nextAnnounceTime": 1673715286,
"nextScrapeTime": 1673716330,
"scrape": "https://tracker.m-team.cc/scrape.php?passkey=1b376abe27739b012fb9b5a226452d13",
"scrapeState": 1,
"seederCount": -1,
"sitename": "m-team",
"tier": 0
}
],
"trackers": [
{
"announce": "https://tracker.m-team.cc/announce.php?passkey=1b376abe27739b012fb9b5a226452d13",
"id": 4536,
"scrape": "https://tracker.m-team.cc/scrape.php?passkey=1b376abe27739b012fb9b5a226452d13",
"sitename": "m-team",
"tier": 0
}
],
"uploadLimit": 100,
"uploadLimited": false,
"uploadRatio": 1.0521,
"uploadedEver": 44282145,
"wanted": [
true
],
"webseeds": [],
"webseedsSendingToUs": 0
}
`

item := Torrent{}

uErr := json.Unmarshal([]byte(body), &item)
if uErr != nil {
log.Fatal(uErr)
}
log.Println(item)
}

0 comments on commit 77b2f0f

Please sign in to comment.