From 7f2d82bf188638fd63ead00f24083c5dce5615a9 Mon Sep 17 00:00:00 2001 From: Jeffrey Serio <23226432+hyperreal64@users.noreply.github.com> Date: Sun, 26 Mar 2023 06:35:45 -0500 Subject: [PATCH] Add support for Gemini files --- README.md | 6 +++++- main.go | 35 +++++++++++++++++++++++++++++++---- template.gmi | 26 ++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 template.gmi diff --git a/README.md b/README.md index 7356f08..1d074cd 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ For other people using this, change the stylesheet in `template.html` to your pr export RPC_HOST="" export RPC_USER="" export RPC_PASSWORD="" +export HTML_FILE="/path/to/html/output/file.html" +export GMI_FILE="/path/to/gemini/output/file.gmi" ``` My personal use case for this involves running the Go binary `go-transmission-stats` on a systemd timer that triggers the corresponding service every 12 hours. @@ -33,13 +35,15 @@ Description=go-transmission-stats service [Service] Type=oneshot -ExecStart=sh -c '/usr/local/bin/go-transmission-stats > /home/user/public/html/torrentstats.html' +ExecStart=/usr/local/bin/go-transmission-stats User= Group= WorkingDirectory=/home/user/go-transmission-stats Environment="RPC_HOST=" Environment="RPC_USER=" Environment="RPC_PASSWORD=" +Environment="HTML_FILE=/path/to/html/output/file.html" +Environment="GMI_FILE=/path/to/gemini/output/file.gmi" ``` Create the systemd timer: diff --git a/main.go b/main.go index 125dfea..a36d65d 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "html/template" + "log" "os" "sort" "time" @@ -82,12 +83,12 @@ type TorrentStatsPageData struct { func main() { transmissionbt, err := transmissionrpc.New(os.Getenv("RPC_HOST"), os.Getenv("RPC_USER"), os.Getenv("RPC_PASSWD"), nil) if err != nil { - panic(err) + log.Fatalln(err) } stats, err := transmissionbt.SessionStats() if err != nil { - panic(err) + log.Fatalln(err) } sessionStats := []SessionStat{ @@ -138,6 +139,32 @@ func main() { TorrentInfo: torrentInfo, } - t, err := template.ParseFiles("template.html") - err = t.Execute(os.Stdout, data) + 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) + } } diff --git a/template.gmi b/template.gmi new file mode 100644 index 0000000..e602459 --- /dev/null +++ b/template.gmi @@ -0,0 +1,26 @@ +# Torrent Stats +As of {{.Date}} + +## Session Stats +{{range .SessionStats}} +{{.Label}}: {{.Value}} +{{end}} + +## Current Stats +{{range .CurrentStats}} +{{.Label}}: {{.Value}} +{{end}} + +## Cumulative Stats +{{range .CumulativeStats}} +{{.Label}}: {{.Value}} +{{end}} + +## Torrent Info +{{range .TorrentInfo}} +Name: {{.Name}} +Date Added: {{.DateAdded}} +Total Size: {{.TotalSize}} +Peers Connected: {{.PeersConnected}} +Peers Getting From Us: {{.PeersGettingFromUs}} +{{end}}