Files
transmission-telegram/internal/formatter/formatter.go

104 lines
3.7 KiB
Go

package formatter
import (
"fmt"
"regexp"
"time"
"github.com/dustin/go-humanize"
"github.com/pyed/transmission"
"transmission-telegram/pkg/utils"
)
// FormatTorrentShort formats a torrent in short format: <ID> Name
func FormatTorrentShort(torrent *transmission.Torrent) string {
return fmt.Sprintf("<%d> %s", torrent.ID, torrent.Name)
}
// FormatTorrentDetailed formats a torrent with detailed information
func FormatTorrentDetailed(torrent *transmission.Torrent) string {
torrentName := utils.EscapeMarkdown(torrent.Name)
return fmt.Sprintf("`<%d>` *%s*\n%s *%s* of *%s* (*%.1f%%*) ↓ *%s* ↑ *%s* R: *%s*\n\n",
torrent.ID, torrentName, torrent.TorrentStatus(),
humanize.Bytes(torrent.Have()),
humanize.Bytes(torrent.SizeWhenDone),
torrent.PercentDone*100,
humanize.Bytes(torrent.RateDownload),
humanize.Bytes(torrent.RateUpload),
torrent.Ratio())
}
// FormatTorrentPaused formats a paused torrent
func FormatTorrentPaused(torrent *transmission.Torrent) string {
return fmt.Sprintf("<%d> %s\n%s (%.1f%%) DL: %s UL: %s R: %s\n\n",
torrent.ID, torrent.Name, torrent.TorrentStatus(),
torrent.PercentDone*100,
humanize.Bytes(torrent.DownloadedEver),
humanize.Bytes(torrent.UploadedEver),
torrent.Ratio())
}
// FormatTorrentChecking formats a checking/verifying torrent
func FormatTorrentChecking(torrent *transmission.Torrent) string {
return fmt.Sprintf("<%d> %s\n%s (%.1f%%)\n\n",
torrent.ID, torrent.Name, torrent.TorrentStatus(),
torrent.PercentDone*100)
}
// FormatTorrentInfo formats detailed torrent information
func FormatTorrentInfo(torrent *transmission.Torrent, trackers string) string {
torrentName := utils.EscapeMarkdown(torrent.Name)
return fmt.Sprintf("`<%d>` *%s*\n%s *%s* of *%s* (*%.1f%%*) ↓ *%s* ↑ *%s* R: *%s*\nDL: *%s* UP: *%s*\nAdded: *%s*, ETA: *%s*\nTrackers: `%s`",
torrent.ID, torrentName, torrent.TorrentStatus(),
humanize.Bytes(torrent.Have()),
humanize.Bytes(torrent.SizeWhenDone),
torrent.PercentDone*100,
humanize.Bytes(torrent.RateDownload),
humanize.Bytes(torrent.RateUpload),
torrent.Ratio(),
humanize.Bytes(torrent.DownloadedEver),
humanize.Bytes(torrent.UploadedEver),
time.Unix(torrent.AddedDate, 0).Format(time.Stamp),
torrent.ETA(),
trackers)
}
// FormatTorrentInfoStopped formats torrent info when live updates are stopped
func FormatTorrentInfoStopped(torrent *transmission.Torrent, trackers string) string {
torrentName := utils.EscapeMarkdown(torrent.Name)
return fmt.Sprintf("`<%d>` *%s*\n%s *%s* of *%s* (*%.1f%%*) ↓ *-* ↑ *-* R: *%s*\nDL: *%s* UP: *%s*\nAdded: *%s*, ETA: *-*\nTrackers: `%s`",
torrent.ID, torrentName, torrent.TorrentStatus(),
humanize.Bytes(torrent.Have()),
humanize.Bytes(torrent.SizeWhenDone),
torrent.PercentDone*100,
torrent.Ratio(),
humanize.Bytes(torrent.DownloadedEver),
humanize.Bytes(torrent.UploadedEver),
time.Unix(torrent.AddedDate, 0).Format(time.Stamp),
trackers)
}
// FormatTorrentActiveStopped formats active torrent when live updates are stopped
func FormatTorrentActiveStopped(torrent *transmission.Torrent) string {
torrentName := utils.EscapeMarkdown(torrent.Name)
return fmt.Sprintf("`<%d>` *%s*\n%s *%s* of *%s* (*%.1f%%*) ↓ *-* ↑ *-* R: *%s*\n\n",
torrent.ID, torrentName, torrent.TorrentStatus(),
humanize.Bytes(torrent.Have()),
humanize.Bytes(torrent.SizeWhenDone),
torrent.PercentDone*100,
torrent.Ratio())
}
// ExtractTrackers extracts tracker domains from torrent trackers
func ExtractTrackers(torrent *transmission.Torrent, trackerRegex *regexp.Regexp) string {
var trackers string
for _, tracker := range torrent.Trackers {
sm := trackerRegex.FindSubmatch([]byte(tracker.Announce))
if len(sm) > 1 {
trackers += string(sm[1]) + " "
}
}
return trackers
}