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 package main
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -8,7 +9,7 @@ import (
"time" "time"
"github.com/hekmon/cunits/v2" "github.com/hekmon/cunits/v2"
"github.com/hekmon/transmissionrpc" "github.com/hyperreal64/transmissionrpc"
"github.com/olekukonko/tablewriter" "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 { type TorrentInfo struct {
Name string Name string
DateAdded time.Time ActivityDate time.Time
TotalSize cunits.Bits TotalSize cunits.Bits
PeersConnected int64 Leechers int64
PeersGettingFromUs int64 Seeders int64
} }
func renderTable(data [][]string) { func renderTable(data [][]string) {
@ -83,7 +89,7 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
stats, err := transmissionbt.SessionStats() stats, err := transmissionbt.SessionStats(context.TODO())
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -97,7 +103,6 @@ func main() {
} }
fmt.Println("SESSION STATS") fmt.Println("SESSION STATS")
fmt.Println()
renderTable(sessionStats) renderTable(sessionStats)
fmt.Println() fmt.Println()
@ -110,7 +115,6 @@ func main() {
} }
fmt.Println("CURRENT STATS") fmt.Println("CURRENT STATS")
fmt.Println()
renderTable(currentStats) renderTable(currentStats)
fmt.Println() fmt.Println()
@ -123,17 +127,37 @@ func main() {
} }
fmt.Println("CUMULATIVE STATS") fmt.Println("CUMULATIVE STATS")
fmt.Println()
renderTable(cumulativeStats) renderTable(cumulativeStats)
fmt.Println() fmt.Println()
var torrentInfo = []TorrentInfo{} 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 { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} else { } else {
for _, torrent := range torrents { 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 return torrentInfo[i].Name < torrentInfo[j].Name
}) })
fmt.Println("TORRENT INFO") for _, torrent := range torrentInfo {
fmt.Println() torrentStats := [][]string{
for _, v := range torrentInfo { {"Name", torrent.Name},
torrentInfoDatum := [][]string{ {"Activity Date", torrent.ActivityDate.String()},
{"Name", v.Name}, {"Total Size", byteCountIEC(int64(*&torrent.TotalSize))},
{"Date Added", v.DateAdded.String()}, {"Leechers", fmt.Sprintf("%d", torrent.Leechers)},
{"Total Size", byteCountIEC(int64(v.TotalSize))}, {"Seeders", fmt.Sprintf("%d", torrent.Seeders)},
{"Peers Connected", fmt.Sprintf("%d", v.PeersConnected)},
{"Peers Getting From Us", fmt.Sprintf("%d", v.PeersGettingFromUs)},
} }
renderTable(torrentInfoDatum) renderTable(torrentStats)
} }
} }

9
go.mod
View File

@ -2,12 +2,13 @@ module codeberg.org/hyperreal/go-transmission-stats
go 1.19 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 ( require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect 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/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 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= 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 h1:k6wIjc4PlacNOHwKEMBgWV2/c8jyD4eRMs5mR1BBhI0=
github.com/hekmon/cunits/v2 v2.1.0/go.mod h1:9r1TycXYXaTmEWlAIfFV8JT+Xo59U96yUJAYHxzii2M= github.com/hekmon/cunits/v2 v2.1.0/go.mod h1:9r1TycXYXaTmEWlAIfFV8JT+Xo59U96yUJAYHxzii2M=
github.com/hekmon/transmissionrpc v1.1.0 h1:58xY27x2JYxaMlIj7ycKnxqgCm3IjvTxfB7cHPLxOfs= github.com/hyperreal64/transmissionrpc v0.0.0-20230416162950-8e2d9f4b086e h1:XfaGWhejEXIJj72hVEOHcUcouOJUsGap0AJ5vSrAQa4=
github.com/hekmon/transmissionrpc v1.1.0/go.mod h1:qkwhsyD/MQSlWvOE1AC92xajwEveAuGsOvTuOBZEuHc= github.com/hyperreal64/transmissionrpc v0.0.0-20230416162950-8e2d9f4b086e/go.mod h1:urQVE/SXF2pDxQ5/Sy8xB1t4FjrOwNMHaPWTEAHEStA=
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/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= 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/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=

34
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"html/template" "html/template"
"log" "log"
@ -9,7 +10,7 @@ import (
"time" "time"
"github.com/hekmon/cunits/v2" "github.com/hekmon/cunits/v2"
"github.com/hekmon/transmissionrpc" "github.com/hyperreal64/transmissionrpc"
) )
func byteCountIEC(b int64) string { func byteCountIEC(b int64) string {
@ -65,11 +66,11 @@ type SessionStat struct {
} }
type TorrentInfo struct { type TorrentInfo struct {
Name string Name string
DateAdded time.Time ActivityDate time.Time
TotalSize cunits.Bits TotalSize cunits.Bits
PeersConnected int64 Leechers int64
PeersGettingFromUs int64 Seeders int64
} }
type TorrentStatsPageData struct { type TorrentStatsPageData struct {
@ -86,7 +87,7 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
stats, err := transmissionbt.SessionStats() stats, err := transmissionbt.SessionStats(context.TODO())
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -116,12 +117,27 @@ func main() {
} }
var torrentInfo = []TorrentInfo{} 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 { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} else { } else {
for _, torrent := range torrents { 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> <table>
<tr> <tr>
<th>Name</th> <th>Name</th>
<th>Date Added</th> <th>Activity Date</th>
<th>Total Size</th> <th>Total Size</th>
<th>Peers Connected</th> <th>Leechers</th>
<th>Peers Getting From Us</th> <th>Seeders</th>
</tr> </tr>
{{range .TorrentInfo}} {{range .TorrentInfo}}
<tr> <tr>
<td>{{.Name}}</td> <td>{{.Name}}</td>
<td>{{.DateAdded}}</td> <td>{{.ActivityDate}}</td>
<td>{{.TotalSize}}</td> <td>{{.TotalSize}}</td>
<td>{{.PeersConnected}}</td> <td>{{.Leechers}}</td>
<td>{{.PeersGettingFromUs}}</td> <td>{{.Seeders}}</td>
</tr> </tr>
{{end}} {{end}}
</table> </table>