From 48ec91102cbae24b56439d73b4f7875bd5cf7751 Mon Sep 17 00:00:00 2001 From: Jeffrey Serio <23226432+hyperreal64@users.noreply.github.com> Date: Sun, 16 Apr 2023 16:45:38 -0500 Subject: [PATCH] feat: add seeder and leecher counts --- cgi-bin/main.go | 66 ++++++++++++++++++++++++++++++++----------------- go.mod | 9 ++++--- go.sum | 8 ++---- main.go | 34 ++++++++++++++++++------- template.html | 12 ++++----- 5 files changed, 82 insertions(+), 47 deletions(-) diff --git a/cgi-bin/main.go b/cgi-bin/main.go index 2ac1b72..8784844 100644 --- a/cgi-bin/main.go +++ b/cgi-bin/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "log" "os" @@ -8,7 +9,7 @@ import ( "time" "github.com/hekmon/cunits/v2" - "github.com/hekmon/transmissionrpc" + "github.com/hyperreal64/transmissionrpc" "github.com/olekukonko/tablewriter" ) @@ -59,12 +60,17 @@ func convertTime(seconds int64) string { } +type trackerStats struct { + LeecherCount int64 `json:"leecherCount"` + SeederCount int64 `json:"seederCount"` +} + type TorrentInfo struct { - Name string - DateAdded time.Time - TotalSize cunits.Bits - PeersConnected int64 - PeersGettingFromUs int64 + Name string + ActivityDate time.Time + TotalSize cunits.Bits + Leechers int64 + Seeders int64 } func renderTable(data [][]string) { @@ -83,7 +89,7 @@ func main() { log.Fatalln(err) } - stats, err := transmissionbt.SessionStats() + stats, err := transmissionbt.SessionStats(context.TODO()) if err != nil { log.Fatalln(err) } @@ -97,7 +103,6 @@ func main() { } fmt.Println("SESSION STATS") - fmt.Println() renderTable(sessionStats) fmt.Println() @@ -110,7 +115,6 @@ func main() { } fmt.Println("CURRENT STATS") - fmt.Println() renderTable(currentStats) fmt.Println() @@ -123,17 +127,37 @@ func main() { } fmt.Println("CUMULATIVE STATS") - fmt.Println() renderTable(cumulativeStats) fmt.Println() var torrentInfo = []TorrentInfo{} - torrents, err := transmissionbt.TorrentGet([]string{"name", "addedDate", "totalSize", "peersConnected", "peersGettingFromUs"}, nil) + var ( + leecherCount int64 + seederCount int64 + ) + + fmt.Println("TORRENT STATS") + torrents, err := transmissionbt.TorrentGet(context.TODO(), []string{ + "activityDate", + "name", + "totalSize", + "trackerStats", + }, nil) if err != nil { fmt.Fprintln(os.Stderr, err) } else { for _, torrent := range torrents { - torrentInfo = append(torrentInfo, TorrentInfo{Name: *torrent.Name, DateAdded: *torrent.AddedDate, TotalSize: *torrent.TotalSize, PeersConnected: *torrent.PeersConnected, PeersGettingFromUs: *torrent.PeersGettingFromUs}) + for _, stat := range torrent.TrackerStats { + leecherCount = stat.LeecherCount + seederCount = stat.SeederCount + } + torrentInfo = append(torrentInfo, TorrentInfo{ + Name: *torrent.Name, + ActivityDate: *torrent.ActivityDate, + TotalSize: *torrent.TotalSize, + Leechers: leecherCount, + Seeders: seederCount, + }) } } @@ -141,16 +165,14 @@ func main() { return torrentInfo[i].Name < torrentInfo[j].Name }) - fmt.Println("TORRENT INFO") - fmt.Println() - for _, v := range torrentInfo { - torrentInfoDatum := [][]string{ - {"Name", v.Name}, - {"Date Added", v.DateAdded.String()}, - {"Total Size", byteCountIEC(int64(v.TotalSize))}, - {"Peers Connected", fmt.Sprintf("%d", v.PeersConnected)}, - {"Peers Getting From Us", fmt.Sprintf("%d", v.PeersGettingFromUs)}, + for _, torrent := range torrentInfo { + torrentStats := [][]string{ + {"Name", torrent.Name}, + {"Activity Date", torrent.ActivityDate.String()}, + {"Total Size", byteCountIEC(int64(*&torrent.TotalSize))}, + {"Leechers", fmt.Sprintf("%d", torrent.Leechers)}, + {"Seeders", fmt.Sprintf("%d", torrent.Seeders)}, } - renderTable(torrentInfoDatum) + renderTable(torrentStats) } } diff --git a/go.mod b/go.mod index 6a8a86c..a7b9a58 100644 --- a/go.mod +++ b/go.mod @@ -2,12 +2,13 @@ module codeberg.org/hyperreal/go-transmission-stats go 1.19 -require github.com/hekmon/transmissionrpc v1.1.0 +require ( + github.com/hekmon/cunits/v2 v2.1.0 + github.com/hyperreal64/transmissionrpc v0.0.0-20230416162950-8e2d9f4b086e + github.com/olekukonko/tablewriter v0.0.5 +) require ( github.com/hashicorp/go-cleanhttp v0.5.2 // indirect - github.com/hekmon/cunits/v2 v2.1.0 // indirect - github.com/hekmon/transmissionrpc/v2 v2.0.1 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect - github.com/olekukonko/tablewriter v0.0.5 // indirect ) diff --git a/go.sum b/go.sum index c66bd98..9e7662c 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,9 @@ -github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= -github.com/hekmon/cunits/v2 v2.0.2/go.mod h1:9r1TycXYXaTmEWlAIfFV8JT+Xo59U96yUJAYHxzii2M= github.com/hekmon/cunits/v2 v2.1.0 h1:k6wIjc4PlacNOHwKEMBgWV2/c8jyD4eRMs5mR1BBhI0= github.com/hekmon/cunits/v2 v2.1.0/go.mod h1:9r1TycXYXaTmEWlAIfFV8JT+Xo59U96yUJAYHxzii2M= -github.com/hekmon/transmissionrpc v1.1.0 h1:58xY27x2JYxaMlIj7ycKnxqgCm3IjvTxfB7cHPLxOfs= -github.com/hekmon/transmissionrpc v1.1.0/go.mod h1:qkwhsyD/MQSlWvOE1AC92xajwEveAuGsOvTuOBZEuHc= -github.com/hekmon/transmissionrpc/v2 v2.0.1 h1:WkILCEdbNy3n/N/w7mi449waMPdH2AA1THyw7TfnN/w= -github.com/hekmon/transmissionrpc/v2 v2.0.1/go.mod h1:+s96Pkg7dIP3h2PT3fzhXPvNb3OdLryh5J8PIvQg3aA= +github.com/hyperreal64/transmissionrpc v0.0.0-20230416162950-8e2d9f4b086e h1:XfaGWhejEXIJj72hVEOHcUcouOJUsGap0AJ5vSrAQa4= +github.com/hyperreal64/transmissionrpc v0.0.0-20230416162950-8e2d9f4b086e/go.mod h1:urQVE/SXF2pDxQ5/Sy8xB1t4FjrOwNMHaPWTEAHEStA= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= diff --git a/main.go b/main.go index 282e8a9..7f8b4b7 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "html/template" "log" @@ -9,7 +10,7 @@ import ( "time" "github.com/hekmon/cunits/v2" - "github.com/hekmon/transmissionrpc" + "github.com/hyperreal64/transmissionrpc" ) func byteCountIEC(b int64) string { @@ -65,11 +66,11 @@ type SessionStat struct { } type TorrentInfo struct { - Name string - DateAdded time.Time - TotalSize cunits.Bits - PeersConnected int64 - PeersGettingFromUs int64 + Name string + ActivityDate time.Time + TotalSize cunits.Bits + Leechers int64 + Seeders int64 } type TorrentStatsPageData struct { @@ -86,7 +87,7 @@ func main() { log.Fatalln(err) } - stats, err := transmissionbt.SessionStats() + stats, err := transmissionbt.SessionStats(context.TODO()) if err != nil { log.Fatalln(err) } @@ -116,12 +117,27 @@ func main() { } var torrentInfo = []TorrentInfo{} - torrents, err := transmissionbt.TorrentGet([]string{"name", "addedDate", "totalSize", "peersConnected", "peersGettingFromUs"}, nil) + var ( + leecherCount int64 + seederCount int64 + ) + + torrents, err := transmissionbt.TorrentGet(context.TODO(), []string{"name", "activityDate", "totalSize", "trackerStats"}, nil) if err != nil { fmt.Fprintln(os.Stderr, err) } else { for _, torrent := range torrents { - torrentInfo = append(torrentInfo, TorrentInfo{Name: *torrent.Name, DateAdded: *torrent.AddedDate, TotalSize: *torrent.TotalSize, PeersConnected: *torrent.PeersConnected, PeersGettingFromUs: *torrent.PeersGettingFromUs}) + for _, stat := range torrent.TrackerStats { + leecherCount = stat.LeecherCount + seederCount = stat.SeederCount + } + torrentInfo = append(torrentInfo, TorrentInfo{ + Name: *torrent.Name, + ActivityDate: *torrent.ActivityDate, + TotalSize: *torrent.TotalSize, + Leechers: leecherCount, + Seeders: seederCount, + }) } } diff --git a/template.html b/template.html index 1dea825..411f871 100644 --- a/template.html +++ b/template.html @@ -49,18 +49,18 @@ - + - - + + {{range .TorrentInfo}} - + - - + + {{end}}
NameDate AddedActivity Date Total SizePeers ConnectedPeers Getting From UsLeechersSeeders
{{.Name}}{{.DateAdded}}{{.ActivityDate}} {{.TotalSize}}{{.PeersConnected}}{{.PeersGettingFromUs}}{{.Leechers}}{{.Seeders}}