Skip to content

Commit 9dedda5

Browse files
committed
Import FLAC tracks into Lidarr manually
1 parent 227d467 commit 9dedda5

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/Unpackerr/unpackerr
22

33
go 1.26.0
44

5-
//nolint:gomoddirectives // we need to use our own iso9660 package until we fix the issue with the original package.
5+
// we need to use our own iso9660 package until we fix the issue with the original package.
66
replace github.com/kdomanski/iso9660 => github.com/Unpackerr/iso9660 v0.0.1
77

88
require (
@@ -22,7 +22,7 @@ require (
2222
golift.io/cnfg v0.2.5
2323
golift.io/cnfgfile v0.0.0-20240713024420-a5436d84eb48
2424
golift.io/rotatorr v0.0.0-20260217050959-f6ac6fc7b38e
25-
golift.io/starr v1.3.0
25+
golift.io/starr v1.3.1-0.20260220055600-a1399516cfeb
2626
golift.io/version v0.0.2
2727
golift.io/xtractr v0.3.1-0.20260219054943-e6f0434c2d7a
2828
gopkg.in/yaml.v3 v3.0.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ golift.io/cnfgfile v0.0.0-20240713024420-a5436d84eb48 h1:c7cJWRr0cUnFHKtq072esKz
248248
golift.io/cnfgfile v0.0.0-20240713024420-a5436d84eb48/go.mod h1:zHm9o8SkZ6Mm5DfGahsrEJPsogyR0qItP59s5lJ98/I=
249249
golift.io/rotatorr v0.0.0-20260217050959-f6ac6fc7b38e h1:FgfNgbg2EUhFzAWPycsbh1dYiFJNLJFDDkn+E298DFQ=
250250
golift.io/rotatorr v0.0.0-20260217050959-f6ac6fc7b38e/go.mod h1:l/fgYTDxyEw15tRLjAtc13M3is1SXMU4hAIE0tdduAQ=
251-
golift.io/starr v1.3.0 h1:dmIt27th+LWIPWHdiHXvDeG8H1h9TT+PQE1ID3DssFY=
252-
golift.io/starr v1.3.0/go.mod h1:W8A/49qhVfoU0HgZyJla4NKRCM5eUHuhSesc+buPIBU=
251+
golift.io/starr v1.3.1-0.20260220055600-a1399516cfeb h1:sRpFSdVj+mb3/ZG57GVDwEzWZlF0UdhGZqFtPrwgaxs=
252+
golift.io/starr v1.3.1-0.20260220055600-a1399516cfeb/go.mod h1:W8A/49qhVfoU0HgZyJla4NKRCM5eUHuhSesc+buPIBU=
253253
golift.io/udf v0.0.1 h1:kEcJVzqqR+IEWGMuPjuVPT9DzXRDukEgsizKAKn1LF8=
254254
golift.io/udf v0.0.1/go.mod h1:ndK7AlWOh+u+nW9tNsQR95dfHsfASG5Y3dMyzVqmPjw=
255255
golift.io/version v0.0.2 h1:i0gXRuSDHKs4O0sVDUg4+vNIuOxYoXhaxspftu2FRTE=

pkg/unpackerr/handlers.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ func (u *Unpackerr) checkExtractDone(now time.Time) {
215215
// handleXtractrCallback handles callbacks from the xtractr library for starr apps (not folders).
216216
// This takes the provided info and logs it then sends it the queue update method.
217217
func (u *Unpackerr) handleXtractrCallback(resp *xtractr.Response) {
218-
if item := u.Map[resp.X.Name]; resp.Done && item != nil {
218+
item := u.Map[resp.X.Name]
219+
if resp.Done && item != nil {
219220
u.updateMetrics(resp, item.App, item.URL)
220221
} else if item != nil {
221222
item.XProg.Archives = resp.Archives.Count() + resp.Extras.Count()
@@ -235,6 +236,11 @@ func (u *Unpackerr) handleXtractrCallback(resp *xtractr.Response) {
235236
resp.Archives.Count(), resp.Extras.Count(), len(resp.NewFiles), bytefmt.ByteSize(resp.Size))
236237
u.Debugf("Extraction Finished: %d files in path: %s", len(files), files)
237238
u.updateQueueStatus(&newStatus{Name: resp.X.Name, Status: EXTRACTED, Resp: resp}, now, true)
239+
240+
if item != nil && item.App == starr.Lidarr && item.SplitFlac &&
241+
extractionHasFlacFiles(resp.NewFiles) {
242+
go u.importSplitFlacTracks(item, u.lidarrServerByURL(item.URL))
243+
}
238244
}
239245
}
240246

pkg/unpackerr/lidarr.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package unpackerr
22

33
import (
44
"errors"
5+
"strings"
56
"time"
67

78
"golift.io/starr"
@@ -136,3 +137,72 @@ func (u *Unpackerr) haveLidarrQitem(name string) bool {
136137

137138
return false
138139
}
140+
141+
// lidarrServerByURL returns the Lidarr server config that matches the given URL, or nil.
142+
func (u *Unpackerr) lidarrServerByURL(url string) *LidarrConfig {
143+
for _, server := range u.Lidarr {
144+
if server.URL == url {
145+
return server
146+
}
147+
}
148+
149+
return nil
150+
}
151+
152+
// extractionHasFlacFiles returns true if any path in files has a .flac extension.
153+
// Used to only trigger manual import after a FLAC+CUE split, not for e.g. zip-of-mp3s.
154+
func extractionHasFlacFiles(files []string) bool {
155+
for _, p := range files {
156+
if strings.HasSuffix(strings.ToLower(p), ".flac") {
157+
return true
158+
}
159+
}
160+
161+
return false
162+
}
163+
164+
// importSplitFlacTracks runs in a goroutine after a Lidarr FLAC+CUE split extraction completes.
165+
// It asks Lidarr for the manual import list for the extract folder and sends the ManualImport command
166+
// so Lidarr imports the split track files.
167+
func (u *Unpackerr) importSplitFlacTracks(item *Extract, server *LidarrConfig) {
168+
if server == nil {
169+
u.Printf("[Lidarr] No Lidarr server found for manual import, this might be a bug: %s", item.Path)
170+
return
171+
}
172+
173+
downloadID, _ := item.IDs["downloadId"].(string)
174+
artistID, _ := item.IDs["artistId"].(int64)
175+
176+
params := &lidarr.ManualImportParams{
177+
Folder: item.Path,
178+
DownloadID: downloadID,
179+
ArtistID: artistID,
180+
FilterExistingFiles: false,
181+
ReplaceExistingFiles: true,
182+
}
183+
184+
outputs, err := server.ManualImport(params)
185+
if err != nil {
186+
u.Errorf("[Lidarr] Manual import list failed for %s: %v", item.Path, err)
187+
return
188+
}
189+
190+
if len(outputs) == 0 {
191+
u.Printf("[Lidarr] No files returned for manual import (folder: %s); import manually in Lidarr", item.Path)
192+
return
193+
}
194+
195+
cmd := lidarr.ManualImportCommandFromOutputs(outputs, true)
196+
if cmd == nil {
197+
u.Printf("[Lidarr] No importable files for manual import: %s", item.Path)
198+
return
199+
}
200+
201+
_, err = server.SendManualImportCommand(cmd)
202+
if err != nil {
203+
u.Errorf("[Lidarr] Manual import command failed for %s: %v", item.Path, err)
204+
return
205+
}
206+
207+
u.Printf("[Lidarr] Manual import triggered for %d files: %s", len(cmd.Files), item.Path)
208+
}

0 commit comments

Comments
 (0)