go-transmission-stats/main.go

169 lines
4.7 KiB
Go
Raw Normal View History

2023-03-25 05:14:51 +01:00
package main
import (
"fmt"
"html/template"
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-03-25 05:14:51 +01:00
"github.com/hekmon/transmissionrpc"
)
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 SessionStat struct {
Label string
Value string
}
type TorrentInfo struct {
2023-03-26 00:19:02 +01:00
Name string
DateAdded time.Time
TotalSize cunits.Bits
PeersConnected int64
PeersGettingFromUs int64
2023-03-25 05:14:51 +01:00
}
type TorrentStatsPageData struct {
Date string
SessionStats []SessionStat
CurrentStats []SessionStat
CumulativeStats []SessionStat
TorrentInfo []TorrentInfo
}
func main() {
2023-03-25 23:33:51 +01:00
transmissionbt, err := transmissionrpc.New(os.Getenv("RPC_HOST"), os.Getenv("RPC_USER"), os.Getenv("RPC_PASSWD"), 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()
if err != nil {
2023-03-26 13:35:45 +02:00
log.Fatalln(err)
2023-03-25 05:14:51 +01:00
}
sessionStats := []SessionStat{
{Label: "Active torrent count", Value: fmt.Sprintf("%d", stats.ActiveTorrentCount)},
{Label: "Download speed", Value: fmt.Sprintf("%s/sec", byteCountIEC(stats.DownloadSpeed))},
{Label: "Upload speed", Value: fmt.Sprintf("%s/sec", byteCountIEC(stats.UploadSpeed))},
{Label: "Paused torrent count", Value: fmt.Sprintf("%d", stats.PausedTorrentCount)},
{Label: "Torrent count", Value: fmt.Sprintf("%d", stats.TorrentCount)},
2023-03-25 05:14:51 +01:00
}
currentStats := []SessionStat{
{Label: "Uploaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.UploadedBytes))},
{Label: "Downloaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.DownloadedBytes))},
{Label: "Files added", Value: fmt.Sprintf("%d", stats.CurrentStats.FilesAdded)},
{Label: "Session count", Value: fmt.Sprintf("%d", stats.CurrentStats.SessionCount)},
{Label: "Time active", Value: convertTime(stats.CurrentStats.SecondsActive)},
2023-03-25 05:14:51 +01:00
}
cumulativeStats := []SessionStat{
{Label: "Uploaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.UploadedBytes))},
{Label: "Downloaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.DownloadedBytes))},
{Label: "Files added", Value: fmt.Sprintf("%d", stats.CumulativeStats.FilesAdded)},
{Label: "Session count", Value: fmt.Sprintf("%d", stats.CumulativeStats.SessionCount)},
{Label: "Time active", Value: convertTime(stats.CumulativeStats.SecondsActive)},
2023-03-25 05:14:51 +01:00
}
var torrentInfo = []TorrentInfo{}
2023-03-26 00:19:02 +01:00
torrents, err := transmissionbt.TorrentGet([]string{"name", "addedDate", "totalSize", "peersConnected", "peersGettingFromUs"}, nil)
2023-03-25 05:14:51 +01:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
for _, torrent := range torrents {
2023-03-26 00:19:02 +01:00
torrentInfo = append(torrentInfo, TorrentInfo{Name: *torrent.Name, DateAdded: *torrent.AddedDate, TotalSize: *torrent.TotalSize, PeersConnected: *torrent.PeersConnected, PeersGettingFromUs: *torrent.PeersGettingFromUs})
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
})
data := TorrentStatsPageData{
Date: time.Now().Format(time.UnixDate),
SessionStats: sessionStats,
CurrentStats: currentStats,
CumulativeStats: cumulativeStats,
TorrentInfo: torrentInfo,
}
2023-03-26 13:35:45 +02:00
htmlTemplate, err := template.ParseFiles("template.html")
if err != nil {
log.Fatalln(err)
}
htmlFile, err := os.Create(os.Getenv("HTML_FILE"))
if err != nil {
log.Fatalln(err)
}
defer htmlFile.Close()
if err = htmlTemplate.Execute(htmlFile, data); err != nil {
log.Fatalln(err)
}
gmiTemplate, err := template.ParseFiles("template.gmi")
if err != nil {
log.Fatalln(err)
}
gmiFile, err := os.Create(os.Getenv("GMI_FILE"))
if err != nil {
log.Fatalln(err)
}
if err = gmiTemplate.Execute(gmiFile, data); err != nil {
log.Fatalln(err)
}
2023-03-25 05:14:51 +01:00
}