Add support for Gemini files

This commit is contained in:
Jeffrey Serio 2023-03-26 06:35:45 -05:00
parent d5b6595e6f
commit 7f2d82bf18
3 changed files with 62 additions and 5 deletions

View File

@ -8,6 +8,8 @@ For other people using this, change the stylesheet in `template.html` to your pr
export RPC_HOST="<ip address of Transmission server>"
export RPC_USER="<rpc username>"
export RPC_PASSWORD="<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=<your user>
Group=<your user's group>
WorkingDirectory=/home/user/go-transmission-stats
Environment="RPC_HOST=<ip address of Transmission server>"
Environment="RPC_USER=<rpc username>"
Environment="RPC_PASSWORD=<rpc password>"
Environment="HTML_FILE=/path/to/html/output/file.html"
Environment="GMI_FILE=/path/to/gemini/output/file.gmi"
```
Create the systemd timer:

35
main.go
View File

@ -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)
}
}

26
template.gmi Normal file
View File

@ -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}}