Skip to content

Commit 74304ac

Browse files
committed
Update lidarr monitoring
Fix config Fix ReadEnv rebase
1 parent 2cca64b commit 74304ac

File tree

3 files changed

+82
-51
lines changed

3 files changed

+82
-51
lines changed

src/config/config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ type Lidarr struct {
6464
Scheme string `env:"LIDARR_SCHEME" env-default:"http"`
6565
URL string `env:"LIDARR_URL"`
6666
Filters Filters
67+
MonitorConfig LidarrMon
68+
}
69+
70+
type LidarrMon struct {
71+
Interval time.Duration `env:"SLSKD_MONITOR_INTERVAL" env-default:"1m"`
72+
Duration time.Duration `env:"SLSKD_MONITOR_DURATION" env-default:"15m"`
6773
}
6874

6975
type SubsonicConfig struct {
@@ -134,8 +140,7 @@ type Listenbrainz struct {
134140
SingleArtist bool `env:"SINGLE_ARTIST" env-default:"true"`
135141
}
136142

137-
func ReadEnv() {
138-
var cfg Config
143+
func (cfg *Config) ReadEnv() {
139144

140145
// Try to read from .env file first
141146
err := cleanenv.ReadConfig(cfg.Flags.CfgPath, cfg)

src/downloader/downloader.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ func NewDownloader(cfg *cfg.DownloadConfig, httpClient *util.HttpClient, filterL
3939
slskdClient.AddHeader()
4040
downloader = append(downloader, slskdClient)
4141
case "lidarr":
42-
downloader = append(downloader, NewLidarr(cfg.Lidarr, cfg.Discovery, cfg.DownloadDir, httpClient))
42+
lidarrClient := NewLidarr(cfg.Lidarr, cfg.DownloadDir)
43+
lidarrClient.AddHeader()
44+
downloader = append(downloader, lidarrClient)
4345
default:
4446
log.Fatalf("downloader '%s' not supported", service)
4547
}
@@ -56,7 +58,7 @@ func (c *DownloadClient) StartDownload(tracks *[]*models.Track) {
5658
if err := os.MkdirAll(c.Cfg.DownloadDir, 0755); err != nil {
5759
log.Fatalln(err)
5860
}
59-
61+
6062
for _, d := range c.Downloaders {
6163
var g errgroup.Group
6264
g.SetLimit(5)
@@ -178,7 +180,7 @@ func moveDownload(srcDir, destDir, trackPath, file string) error { // Move downl
178180

179181
if err = os.MkdirAll(destDir, os.ModePerm); err != nil {
180182
return fmt.Errorf("couldn't make download directory: %s", err.Error())
181-
}
183+
}
182184

183185
dstFile := filepath.Join(destDir, file)
184186
out, err := os.Create(dstFile)

src/downloader/lidarr.go

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
)
1616

1717
type Lidarr struct {
18+
Headers map[string]string
1819
DownloadDir string
1920
HttpClient *util.HttpClient
2021
Cfg cfg.Lidarr
@@ -173,14 +174,32 @@ type RootFolder struct {
173174
DefaultQualityProfileId int `json:"defaultQualityProfileId"`
174175
}
175176

176-
func NewLidarr(cfg cfg.Lidarr, discovery, downloadDir string, httpClient *util.HttpClient) Lidarr { // init downloader cfg for lidarr
177-
return Lidarr{
178-
DownloadDir: downloadDir,
179-
HttpClient: httpClient,
177+
func NewLidarr(cfg cfg.Lidarr, downloadDir string) *Lidarr { // init downloader cfg for lidarr
178+
return &Lidarr{
180179
Cfg: cfg,
180+
HttpClient: util.NewHttp(util.HttpClientConfig{Timeout: cfg.Timeout}),
181+
DownloadDir: downloadDir,
181182
}
182183
}
183184

185+
func (c *Lidarr) AddHeader() {
186+
if c.Headers == nil {
187+
c.Headers = make(map[string]string)
188+
}
189+
c.Headers["X-API-Key"] = c.Cfg.APIKey
190+
}
191+
192+
func (c *Lidarr) GetConf() (MonitorConfig, error) {
193+
return MonitorConfig{
194+
CheckInterval: c.Cfg.MonitorConfig.Interval,
195+
MonitorDuration: c.Cfg.MonitorConfig.Duration,
196+
MigrateDownload: c.Cfg.MigrateDL,
197+
ToDir: c.DownloadDir,
198+
FromDir: c.Cfg.LidarrDir,
199+
Service: "Lidarr",
200+
}, nil
201+
}
202+
184203
func (c Lidarr) QueryTrack(track *models.Track) error {
185204

186205
album, err := c.findBestAlbumMatch(track)
@@ -301,59 +320,54 @@ func (c Lidarr) findBestAlbumMatch(track *models.Track) (*Album, error) {
301320
return &topMatch, nil
302321
}
303322

304-
func (c Lidarr) getDownloadStatus() (DownloadStatus, error) {
305-
reqParams := "/api/v0/transfers/downloads"
306-
307-
body, err := c.HttpClient.MakeRequest("GET", c.Cfg.URL+reqParams, nil, nil)
308-
if err != nil {
309-
return nil, err
310-
}
311-
312-
var status DownloadStatus
313-
if err := util.ParseResp(body, &status); err != nil {
314-
return nil, err
315-
}
316-
return status, nil
317-
}
323+
func (c Lidarr) deleteSearch(ID string) error {
324+
reqParams := fmt.Sprintf("/api/v0/searches/%s", ID)
318325

319-
func (c Lidarr) MonitorDownloads(tracks []*models.Track) error {
320-
monitorCfg := MonitorConfig{
321-
CheckInterval: 1 * time.Minute,
322-
MonitorDuration: 15 * time.Minute,
323-
MigrateDownload: c.Cfg.MigrateDL,
324-
FromDir: c.Cfg.LidarrDir,
325-
ToDir: c.DownloadDir,
326-
}
327-
err := Monitor(
328-
tracks,
329-
c.getDownloadStatus,
330-
func(t *models.Track, id string) { c.cleanupTrack(t, id) },
331-
moveDownload,
332-
monitorCfg,
333-
)
326+
_, err := c.HttpClient.MakeRequest("DELETE", c.Cfg.URL+reqParams, nil, nil)
334327
if err != nil {
335328
return err
336329
}
337330
return nil
338331
}
339332

340-
func (c Lidarr) cleanupTrack(track *models.Track, fileID string) {
341-
if err := c.deleteSearch(track.ID); err != nil {
342-
debug.Debug(fmt.Sprintf("[slskd] failed to delete search request: %v", err))
343-
}
344-
if err := c.deleteDownload(track.MainArtistID, fileID); err != nil {
345-
debug.Debug(fmt.Sprintf("[slskd] failed to delete download: %v", err))
333+
func (c *Lidarr) GetDownloadStatus(tracks []*models.Track) (map[string]FileStatus, error) {
334+
reqParams := "/api/v0/transfers/downloads"
335+
fileStatuses := make(map[string]FileStatus, len(tracks))
336+
body, err := c.HttpClient.MakeRequest("GET", c.Cfg.URL+reqParams, nil, c.Headers)
337+
if err != nil {
338+
return nil, err
346339
}
347-
}
348340

349-
func (c Lidarr) deleteSearch(ID string) error {
350-
reqParams := fmt.Sprintf("/api/v0/searches/%s", ID)
341+
var statuses DownloadStatus
342+
if err := util.ParseResp(body, &statuses); err != nil {
343+
return nil, err
344+
}
345+
for _, status := range statuses {
346+
for _, track := range tracks {
347+
if status.Username != track.MainArtistID {
348+
continue
349+
}
351350

352-
_, err := c.HttpClient.MakeRequest("DELETE", c.Cfg.URL+reqParams, nil, nil)
353-
if err != nil {
354-
return err
351+
for _, dir := range status.Directories {
352+
for _, file := range dir.Files {
353+
if string(file.Name) == track.File {
354+
fileStatuses[track.File] = FileStatus{
355+
ID: file.ID,
356+
Size: file.Size,
357+
State: file.State,
358+
BytesTransferred: file.BytesTransferred,
359+
BytesRemaining: file.BytesRemaining,
360+
PercentComplete: file.PercentComplete,
361+
}
362+
}
363+
}
364+
}
365+
}
355366
}
356-
return nil
367+
if len(fileStatuses) != 0 {
368+
return fileStatuses, nil
369+
}
370+
return nil, fmt.Errorf("no files found to monitor")
357371
}
358372

359373
func (c Lidarr) deleteDownload(user, ID string) error {
@@ -371,3 +385,13 @@ func (c Lidarr) deleteDownload(user, ID string) error {
371385

372386
return nil
373387
}
388+
389+
func (c *Lidarr) Cleanup(track models.Track, fileID string) error {
390+
if err := c.deleteSearch(track.ID); err != nil {
391+
debug.Debug(fmt.Sprintf("[slskd] failed to delete search request: %v", err))
392+
}
393+
if err := c.deleteDownload(track.MainArtistID, fileID); err != nil {
394+
debug.Debug(fmt.Sprintf("[slskd] failed to delete download: %v", err))
395+
}
396+
return nil
397+
}

0 commit comments

Comments
 (0)