Files
music-agregator/internal/torrent/service.go
T
2026-05-06 22:53:55 +02:00

77 lines
1.8 KiB
Go

package torrent
import (
"fmt"
"github.com/rs/zerolog/log"
pb "homelab.lan/music-agregator/gen/music_agregator/torrent/v1"
"homelab.lan/music-agregator/internal/config"
)
type TorrentService struct {
client TorrentClient
token string
}
func NewTorrentService(cfg config.Config) (*TorrentService, error) {
var client TorrentClient
switch cfg.Torrent.ClientType {
case config.TorrentClientQbittorrent:
client = NewQbittorrentClient(cfg.Torrent.Url)
default:
return nil, fmt.Errorf("unknown torrent client type: %s", cfg.Torrent.ClientType)
}
token, err := client.Login(cfg.Torrent.Username, cfg.Torrent.Password)
if err != nil {
return nil, fmt.Errorf("torrent client login failed: %w", err)
}
log.Info().Str("client", string(cfg.Torrent.ClientType)).Msg("torrent client connected")
return &TorrentService{
client: client,
token: token,
}, nil
}
func (service *TorrentService) List(req *pb.ListRequest) (*pb.ListResponse, error) {
torrents, err := service.client.List()
if err != nil {
return nil, err
}
items := make([]*pb.ListItem, len(torrents))
for i, t := range torrents {
items[i] = &pb.ListItem{
Hash: t.Hash,
Name: t.Name,
Size: t.Size,
Progress: t.Progress,
Dlspeed: t.DlSpeed,
Upspeed: t.UpSpeed,
NumSeeds: t.NumSeeds,
NumLeechs: t.NumLeechs,
State: t.State,
Eta: t.ETA,
Ratio: t.Ratio,
Category: t.Category,
Tags: t.Tags,
AddedOn: t.AddedOn,
CompletionOn: t.CompletionOn,
SavePath: t.SavePath,
ContentPath: t.ContentPath,
Downloaded: t.Downloaded,
Uploaded: t.Uploaded,
Tracker: t.Tracker,
SeedingTime: t.SeedingTime,
AmountLeft: t.AmountLeft,
Availability: t.Availability,
}
}
return &pb.ListResponse{Items: items}, nil
}