A self-hosted dashboard that puts all your feeds in one place
Go aggregatordashboarddockerfeed-readergohomelabhomepagemonitoringredditrssrss-readerself-hostedstartpageyoutube
Added qBittorrent
ThiemeHb46cd6601c83
diff --git a/internal/glance/static/css/widget-qbittorrent.css b/internal/glance/static/css/widget-qbittorrent.cssnew file mode 100644index 0000000..28f11f5--- /dev/null+++ b/internal/glance/static/css/widget-qbittorrent.css@@ -0,0 +1,3 @@+.qbittorrent-torrent + .qbittorrent-torrent {+ margin-top: 1.2rem;+}diff --git a/internal/glance/templates.go b/internal/glance/templates.goindex 699772d..14eb368 100644--- a/internal/glance/templates.go+++ b/internal/glance/templates.go@@ -24,6 +24,7 @@ var globalTemplateFunctions = template.FuncMap{"absInt": func(i int) int {return int(math.Abs(float64(i)))},+ "multiply": multiply,"formatPrice": func(price float64) string {return intl.Sprintf("%.2f", price)},@@ -86,3 +87,25 @@ func formatApproxNumber(count int) string {func dynamicRelativeTimeAttrs(t interface{ Unix() int64 }) template.HTMLAttr {return template.HTMLAttr(`data-dynamic-relative-time="` + strconv.FormatInt(t.Unix(), 10) + `"`)}++func multiply(a, b interface{}) float64 {+ var result float64++ switch v := a.(type) {+ case int:+ result = float64(v)+ case float64:+ result = v+ default:+ panic("Unsupported type for 'a', only int and float64 are supported")+ }++ switch v := b.(type) {+ case int:+ return result * float64(v)+ case float64:+ return result * v+ default:+ panic("Unsupported type for 'b', only int and float64 are supported")+ }+}diff --git a/internal/glance/templates/qbittorrent.html b/internal/glance/templates/qbittorrent.htmlnew file mode 100644index 0000000..51721e1--- /dev/null+++ b/internal/glance/templates/qbittorrent.html@@ -0,0 +1,28 @@+{{ template "widget-base.html" . }}++{{ define "widget-content" }}+<div class="qbittorrent-torrents">+ {{- if .Torrents }}+ {{- range .Torrents }}+ <div class="qbittorrent-torrent">+ <div class="qbittorrent-torrent-header">+ <span class="qbittorrent-torrent-name text-truncate">{{ .Name }}</span>+ </div>+ <div class="qbittorrent-torrent-progress flex items-center gap-10">+ <div class="progress-bar flex-1">+ <div class="progress-value" style="--percent: {{ multiply .Progress 100 }};"></div>+ </div>+ <span class="qbittorrent-progress-text color-highlight text-very-compact">{{ printf "%.1f" (multiply+ .Progress 100) }}+ <span class="color-base">%</span></span>+ </div>+ <div class="qbittorrent-torrent-info">+ <span class="qbittorrent-torrent-speed">{{ .Speed }}/s</span>+ </div>+ </div>+ {{- end }}+ {{- else }}+ <div class="text-center color-subdue">No active torrents</div>+ {{- end }}+</div>+{{ end }}diff --git a/internal/glance/widget-qbittorrent.go b/internal/glance/widget-qbittorrent.gonew file mode 100644index 0000000..1235d75--- /dev/null+++ b/internal/glance/widget-qbittorrent.go@@ -0,0 +1,153 @@+package glance++import (+ "context"+ "encoding/json"+ "errors"+ "fmt"+ "html/template"+ "io"+ "log/slog"+ "net/http"+ "net/http/cookiejar"+ "net/url"+ "sort"+ "strings"+ "time"+)++const (+ qBittorrentAPIPrefix = "/api/v2"+ qBittorrentLoginPath = qBittorrentAPIPrefix + "/auth/login"+ qBittorrentTorrentsPath = qBittorrentAPIPrefix + "/torrents/info"+)++var qbittorrentWidgetTemplate = mustParseTemplate("qbittorrent.html", "widget-base.html")++type qbittorrentWidget struct {+ widgetBase `yaml:",inline"`+ URL string `yaml:"url"`+ Username string `yaml:"username"`+ Password string `yaml:"password"`+ Limit int `yaml:"limit"`+ Torrents []qbittorrentTorrent `yaml:"-"`+ client *http.Client `yaml:"-"`+}++type qbittorrentTorrent struct {+ Name string `json:"name"`+ Progress float64 `json:"progress"`+ State string `json:"state"`+ Size int64 `json:"size"`+ Downloaded int64 `json:"downloaded"`+ Speed uint64 `json:"dlspeed"`+}++func (widget *qbittorrentWidget) initialize() error {+ widget.+ withTitle("qBittorrent").+ withTitleURL(widget.URL).+ withCacheDuration(time.Second * 5)++ if widget.URL == "" {+ return errors.New("URL is required")+ }++ if _, err := url.Parse(widget.URL); err != nil {+ return fmt.Errorf("invalid URL: %w", err)+ }++ if widget.Limit <= 0 {+ widget.Limit = 5+ }++ jar, err := cookiejar.New(nil)+ if err != nil {+ return fmt.Errorf("error creating cookie jar: %w", err)+ }++ widget.client = &http.Client{Jar: jar}++ if err := widget.login(); err != nil {+ return fmt.Errorf("login failed: %w", err)+ }++ return nil+}++func (widget *qbittorrentWidget) login() error {+ loginData := url.Values{}+ loginData.Set("username", widget.Username)+ loginData.Set("password", widget.Password)++ req, err := http.NewRequest("POST", widget.URL+qBittorrentLoginPath, strings.NewReader(loginData.Encode()))+ if err != nil {+ return fmt.Errorf("creating login request: %w", err)+ }++ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")+ req.Header.Set("Referer", widget.URL)++ resp, err := widget.client.Do(req)+ if err != nil {+ return fmt.Errorf("login request failed: %w", err)+ }+ defer func(Body io.ReadCloser) {+ _ = Body.Close()+ }(resp.Body)++ if resp.StatusCode != http.StatusOK {+ body, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("login failed with status %d: %s", resp.StatusCode, string(body))+ }++ return nil+}++func (widget *qbittorrentWidget) update(ctx context.Context) {+ slog.Info("Updating qBittorrent widget", "url", widget.URL)++ req, err := http.NewRequestWithContext(ctx, "GET", widget.URL+qBittorrentTorrentsPath, nil)+ if err != nil {+ widget.withError(fmt.Errorf("creating torrents request: %w", err))+ return+ }++ req.Header.Set("Referer", widget.URL)++ resp, err := widget.client.Do(req)+ if err != nil {+ widget.withError(fmt.Errorf("torrents request failed: %w", err))+ return+ }+ defer func(Body io.ReadCloser) {+ _ = Body.Close()+ }(resp.Body)++ if resp.StatusCode != http.StatusOK {+ body, _ := io.ReadAll(resp.Body)+ widget.withError(fmt.Errorf("torrents request failed with status %d: %s", resp.StatusCode, string(body)))+ return+ }++ var torrents []qbittorrentTorrent+ if err := json.NewDecoder(resp.Body).Decode(&torrents); err != nil {+ widget.withError(fmt.Errorf("decoding torrents response: %w", err))+ return+ }++ sort.Slice(torrents, func(i, j int) bool {+ return torrents[i].Progress > torrents[j].Progress+ })++ if len(torrents) > widget.Limit {+ torrents = torrents[:widget.Limit]+ }++ widget.Torrents = torrents+ widget.withError(nil)+}++func (widget *qbittorrentWidget) Render() template.HTML {+ return widget.renderTemplate(widget, qbittorrentWidgetTemplate)+}diff --git a/internal/glance/widget.go b/internal/glance/widget.goindex 7c30183..a46da66 100644--- a/internal/glance/widget.go+++ b/internal/glance/widget.go@@ -79,6 +79,8 @@ func newWidget(widgetType string) (widget, error) {w = &dockerContainersWidget{}case "server-stats":w = &serverStatsWidget{}+ case "qbittorrent":+ w = &qbittorrentWidget{}default:return nil, fmt.Errorf("unknown widget type: %s", widgetType)}