Add cgi-bin

This commit is contained in:
Jeffrey Serio 2023-04-01 17:23:09 -05:00
parent a034b66023
commit 638f628798
3 changed files with 151 additions and 1 deletions

143
cgi-bin/main.go Normal file
View File

@ -0,0 +1,143 @@
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
}
func main() {
transmissionbt, err := transmissionrpc.New(os.Getenv("RPC_HOST"), os.Getenv("RPC_USER"), os.Getenv("RPC_PASSWD"), nil)
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)},
}
sessionStatsTable := tablewriter.NewWriter(os.Stdout)
sessionStatsTable.SetAlignment(tablewriter.ALIGN_LEFT)
sessionStatsTable.AppendBulk(sessionStats)
sessionStatsTable.Render()
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)},
}
currentStatsTable := tablewriter.NewWriter(os.Stdout)
currentStatsTable.SetAlignment(tablewriter.ALIGN_LEFT)
currentStatsTable.AppendBulk(currentStats)
currentStatsTable.Render()
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)},
}
cumulativeStatsTable := tablewriter.NewWriter(os.Stdout)
cumulativeStatsTable.SetAlignment(tablewriter.ALIGN_LEFT)
cumulativeStatsTable.AppendBulk(cumulativeStats)
cumulativeStatsTable.Render()
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
})
torrentStatsTable := tablewriter.NewWriter(os.Stdout)
torrentStatsTable.SetHeader([]string{"Name", "Date Added", "Total Size", "Peers Connected", "Peers Getting From Us"})
torrentStatsTable.SetAlignment(tablewriter.ALIGN_LEFT)
for _, v := range torrentInfo {
torrentStatsTable.Append([]string{v.Name, fmt.Sprintf("%s", v.DateAdded), fmt.Sprintf("%d", v.TotalSize), fmt.Sprintf("%d", v.PeersConnected), fmt.Sprintf("%d", v.PeersGettingFromUs)})
}
torrentStatsTable.Render()
}

5
go.mod
View File

@ -2,9 +2,12 @@ module codeberg.org/hyperreal/go-transmission-stats
go 1.19
require github.com/hekmon/transmissionrpc v1.1.0
require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hekmon/cunits/v2 v2.1.0 // indirect
github.com/hekmon/transmissionrpc v1.1.0
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
)

4
go.sum
View File

@ -8,3 +8,7 @@ github.com/hekmon/transmissionrpc v1.1.0 h1:58xY27x2JYxaMlIj7ycKnxqgCm3IjvTxfB7c
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/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=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=