feat: add seeder and leecher counts

This commit is contained in:
Jeffrey Serio 2023-04-16 16:45:38 -05:00
parent bcd21d2f73
commit 48ec91102c
5 changed files with 82 additions and 47 deletions

View File

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

9
go.mod
View File

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

8
go.sum
View File

@ -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=

34
main.go
View File

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

View File

@ -49,18 +49,18 @@
<table>
<tr>
<th>Name</th>
<th>Date Added</th>
<th>Activity Date</th>
<th>Total Size</th>
<th>Peers Connected</th>
<th>Peers Getting From Us</th>
<th>Leechers</th>
<th>Seeders</th>
</tr>
{{range .TorrentInfo}}
<tr>
<td>{{.Name}}</td>
<td>{{.DateAdded}}</td>
<td>{{.ActivityDate}}</td>
<td>{{.TotalSize}}</td>
<td>{{.PeersConnected}}</td>
<td>{{.PeersGettingFromUs}}</td>
<td>{{.Leechers}}</td>
<td>{{.Seeders}}</td>
</tr>
{{end}}
</table>