go-transmission-stats/main.go

177 lines
4.5 KiB
Go
Raw Normal View History

2023-07-19 16:59:18 +02:00
package main
2023-03-25 05:14:51 +01:00
import (
"context"
2023-03-25 05:14:51 +01:00
"fmt"
2023-03-26 13:35:45 +02:00
"log"
2023-03-25 05:14:51 +01:00
"os"
"sort"
"time"
2023-03-26 00:19:02 +01:00
"github.com/hekmon/cunits/v2"
2023-07-19 18:35:00 +02:00
"github.com/hyperreal64/transmissionrpc"
2023-05-18 01:19:34 +02:00
"github.com/olekukonko/tablewriter"
2023-03-25 05:14:51 +01:00
)
func byteCountIEC(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
func convertTime(seconds int64) string {
switch {
case seconds > 86400:
day := seconds / 86400
remainder := seconds % 86400
hour := remainder / 3600
remainder = remainder % 3600
minutes := remainder / 60
remainder = remainder % 60
return fmt.Sprintf("%d days, %d hours, %d minutes, %d seconds", day, hour, minutes, remainder)
case seconds < 86400 && seconds > 3600:
hour := seconds / 3600
remainder := seconds % 3600
minutes := remainder / 60
remainder = remainder % 60
return fmt.Sprintf("%d hours, %d minutes, %d seconds", hour, minutes, remainder)
case seconds < 3600 && seconds > 60:
minutes := seconds / 60
remainder := seconds % 60
return fmt.Sprintf("%d minutes, %d seconds", minutes, remainder)
default:
return fmt.Sprintf("%d seconds", seconds)
}
}
2023-05-18 01:19:34 +02:00
type trackerStats struct {
LeecherCount int64 `json:"leecherCount"`
SeederCount int64 `json:"seederCount"`
2023-03-25 05:14:51 +01:00
}
type TorrentInfo struct {
2023-04-16 23:45:38 +02:00
Name string
ActivityDate time.Time
TotalSize cunits.Bits
Leechers int64
Seeders int64
2023-03-25 05:14:51 +01:00
}
2023-05-18 01:19:34 +02:00
func renderTable(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.AppendBulk(data)
table.Render()
2023-03-25 05:14:51 +01:00
}
func main() {
2023-05-18 01:19:34 +02:00
fmt.Println("20 text/gemini\r")
fmt.Println("```")
2023-07-25 02:46:49 +02:00
transmissionbt, err := transmissionrpc.New(os.Getenv("TRANSMISSION_RPC_URL"), os.Getenv("TRANSMISSION_RPC_USER"), os.Getenv("TRANSMISSION_RPC_PASSWORD"), nil)
2023-03-25 05:14:51 +01:00
if err != nil {
2023-03-26 13:35:45 +02:00
log.Fatalln(err)
2023-03-25 05:14:51 +01:00
}
stats, err := transmissionbt.SessionStats(context.TODO())
2023-03-25 05:14:51 +01:00
if err != nil {
2023-03-26 13:35:45 +02:00
log.Fatalln(err)
2023-03-25 05:14:51 +01:00
}
2023-05-18 01:19:34 +02:00
sessionStats := [][]string{
{"Active torrent count", fmt.Sprintf("%d", stats.ActiveTorrentCount)},
{"Download speed", fmt.Sprintf("%s/sec", byteCountIEC(stats.DownloadSpeed))},
{"Upload speed", fmt.Sprintf("%s/sec", byteCountIEC(stats.UploadSpeed))},
{"Paused torrent count", fmt.Sprintf("%d", stats.PausedTorrentCount)},
{"Torrent count", fmt.Sprintf("%d", stats.TorrentCount)},
2023-03-25 05:14:51 +01:00
}
2023-05-18 01:19:34 +02:00
fmt.Println("SESSION STATS")
renderTable(sessionStats)
fmt.Println()
currentStats := [][]string{
{"Uploaded bytes", byteCountIEC(stats.CurrentStats.UploadedBytes)},
{"Downloaded bytes", byteCountIEC(stats.CurrentStats.DownloadedBytes)},
{"Files added", fmt.Sprintf("%d", stats.CurrentStats.FilesAdded)},
{"Session count", fmt.Sprintf("%d", stats.CurrentStats.SessionCount)},
{"Time active", convertTime(stats.CurrentStats.SecondsActive)},
2023-03-25 05:14:51 +01:00
}
2023-05-18 01:19:34 +02:00
fmt.Println("CURRENT STATS")
renderTable(currentStats)
fmt.Println()
cumulativeStats := [][]string{
{"Uploaded bytes", byteCountIEC(stats.CumulativeStats.UploadedBytes)},
{"Downloaded bytes", byteCountIEC(stats.CumulativeStats.DownloadedBytes)},
{"Files added", fmt.Sprintf("%d", stats.CumulativeStats.FilesAdded)},
{"Session count", fmt.Sprintf("%d", stats.CumulativeStats.SessionCount)},
{"Time active", convertTime(stats.CumulativeStats.SecondsActive)},
2023-03-25 05:14:51 +01:00
}
2023-05-18 01:19:34 +02:00
fmt.Println("CUMULATIVE STATS")
renderTable(cumulativeStats)
fmt.Println()
2023-03-25 05:14:51 +01:00
var torrentInfo = []TorrentInfo{}
2023-04-16 23:45:38 +02:00
var (
leecherCount int64
seederCount int64
)
2023-05-18 01:19:34 +02:00
fmt.Println("TORRENT STATS")
torrents, err := transmissionbt.TorrentGet(context.TODO(), []string{
2023-05-18 01:19:34 +02:00
"activityDate",
"name",
"totalSize",
"trackerStats",
}, nil)
2023-03-25 05:14:51 +01:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
for _, torrent := range torrents {
2023-04-16 23:45:38 +02:00
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,
})
2023-03-25 05:14:51 +01:00
}
}
sort.Slice(torrentInfo, func(i, j int) bool {
2023-03-26 00:19:02 +01:00
return torrentInfo[i].Name < torrentInfo[j].Name
2023-03-25 05:14:51 +01:00
})
2023-05-18 01:19:34 +02:00
torrentStats := [][]string{
{"Name", "Activity Date", "Total Size", "Leechers", "Seeders"},
2023-03-25 05:14:51 +01:00
}
2023-05-18 01:19:34 +02:00
for _, torrent := range torrentInfo {
torrentStats = append(torrentStats, []string{torrent.Name, torrent.ActivityDate.String(), torrent.TotalSize.GiBString(), fmt.Sprintf("%d", torrent.Leechers), fmt.Sprintf("%d", torrent.Seeders)})
2023-03-26 13:35:45 +02:00
}
2023-05-18 01:19:34 +02:00
renderTable(torrentStats)
2023-03-25 05:14:51 +01:00
}