go-transmission-stats/cgi-bin/main.go

157 lines
4.3 KiB
Go
Raw Normal View History

2023-04-02 00:23:09 +02:00
package main
import (
"fmt"
"log"
"os"
"sort"
"time"
"github.com/hekmon/cunits/v2"
"github.com/hekmon/transmissionrpc"
"github.com/olekukonko/tablewriter"
)
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)
}
}
type TorrentInfo struct {
Name string
DateAdded time.Time
TotalSize cunits.Bits
PeersConnected int64
PeersGettingFromUs int64
}
2023-04-05 22:52:26 +02:00
func renderTable(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.AppendBulk(data)
table.Render()
}
2023-04-02 00:23:09 +02:00
func main() {
2023-04-02 04:21:31 +02:00
fmt.Println("20 text/gemini\r")
fmt.Println("```")
2023-04-05 22:52:26 +02:00
transmissionbt, err := transmissionrpc.New("", "", "", nil)
2023-04-02 00:23:09 +02:00
if err != nil {
log.Fatalln(err)
}
stats, err := transmissionbt.SessionStats()
if err != nil {
log.Fatalln(err)
}
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-04-02 04:21:31 +02:00
fmt.Println("SESSION STATS")
fmt.Println()
2023-04-05 22:52:26 +02:00
renderTable(sessionStats)
2023-04-02 04:21:31 +02:00
fmt.Println()
2023-04-02 00:23:09 +02:00
currentStats := [][]string{
{"Uploaded bytes", fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.UploadedBytes))},
{"Downloaded bytes", fmt.Sprintf("%s", 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-04-02 04:21:31 +02:00
fmt.Println("CURRENT STATS")
fmt.Println()
2023-04-05 22:52:26 +02:00
renderTable(currentStats)
2023-04-02 04:21:31 +02:00
fmt.Println()
2023-04-02 00:23:09 +02:00
cumulativeStats := [][]string{
{"Uploaded bytes", fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.UploadedBytes))},
{"Downloaded bytes", fmt.Sprintf("%s", 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-04-02 04:21:31 +02:00
fmt.Println("CUMULATIVE STATS")
fmt.Println()
2023-04-05 22:52:26 +02:00
renderTable(cumulativeStats)
2023-04-02 04:21:31 +02:00
fmt.Println()
2023-04-02 00:23:09 +02:00
var torrentInfo = []TorrentInfo{}
torrents, err := transmissionbt.TorrentGet([]string{"name", "addedDate", "totalSize", "peersConnected", "peersGettingFromUs"}, 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})
}
}
sort.Slice(torrentInfo, func(i, j int) bool {
return torrentInfo[i].Name < torrentInfo[j].Name
})
2023-04-02 04:21:31 +02:00
fmt.Println("TORRENT INFO")
fmt.Println()
2023-04-02 00:23:09 +02:00
for _, v := range torrentInfo {
2023-04-05 22:52:26 +02:00
torrentInfoDatum := [][]string{
{"Name", v.Name},
{"Date Added", v.DateAdded.String()},
{"Total Size", fmt.Sprintf("%d", int64(v.TotalSize))},
{"Peers Connected", fmt.Sprintf("%d", v.PeersConnected)},
{"Peers Getting From Us", fmt.Sprintf("%d", v.PeersGettingFromUs)},
}
renderTable(torrentInfoDatum)
2023-04-02 00:23:09 +02:00
}
}