@@ -136,44 +136,25 @@ func (c *Slskd) QueryTrack(track *models.Track) error {
136136 return fmt .Errorf ("search not completed for %s, skipping track" , trackDetails )
137137 }
138138
139- results , err := c .searchResults (ID )
139+ track .ID = ID
140+ return nil
141+ }
142+
143+ func (c * Slskd ) GetTrack (track * models.Track ) error {
144+ results , err := c .searchResults (track .ID )
140145 if err != nil {
141146 return err
142147 }
143148 files , err := c .CollectFiles (* track , results )
144149 if err != nil {
145150 return err
146151 }
147- file , err := c .filterFiles (files )
152+ filterFiles , err := c .filterFiles (files )
148153 if err != nil {
149154 return err
150155 }
151-
152- track .ID = ID
153- track .File = file .Name
154- track .Size = file .Size
155- track .MainArtistID = file .Username
156-
157- return nil
158- }
159-
160- func (c * Slskd ) GetTrack (track * models.Track ) error {
161- reqParams := fmt .Sprintf ("/api/v0/transfers/downloads/%s" , track .MainArtistID )
162- payload := []DownloadPayload {
163- {
164- Filename : track .File ,
165- Size : track .Size ,
166- },
167- }
168-
169- DLpayload , err := json .Marshal (payload )
170- if err != nil {
171- return fmt .Errorf ("failed to marshal payload: %s" , err .Error ())
172- }
173-
174- _ , err = c .HttpClient .MakeRequest ("POST" , c .Cfg .URL + reqParams , bytes .NewBuffer (DLpayload ), c .Headers )
175- if err != nil {
176- return fmt .Errorf ("failed to queue download: %s" , err .Error ())
156+ if err := c .queueDownload (filterFiles , track ); err != nil {
157+ return err
177158 }
178159 return nil
179160}
@@ -285,10 +266,12 @@ func (c Slskd) CollectFiles(track models.Track, searchResults SearchResults) ([]
285266 }
286267}
287268
288- func (c Slskd ) filterFiles (files []File ) (File , error ) { // return first file that passes checks
289- for _ , extension := range c .Cfg .Filters .Extensions { // looping the Extensions list allows for priority based filtering (i.e flac before mp3 etc...)
269+ func (c Slskd ) filterFiles (files []File ) ([]File , error ) {
270+ var filtered []File
271+
272+ for _ , ext := range c .Cfg .Filters .Extensions {
290273 for _ , file := range files {
291- if file .Extension != extension { // if extension not matched, skip file
274+ if file .Extension != ext {
292275 continue
293276 }
294277
@@ -300,10 +283,46 @@ func (c Slskd) filterFiles(files []File) (File, error) { // return first file th
300283 continue
301284 }
302285
303- return file , nil
286+ filtered = append (filtered , file )
287+ if len (filtered ) >= c .Cfg .DownloadAttempts {
288+ return filtered , nil
289+ }
290+ }
291+ }
292+
293+ if len (filtered ) == 0 {
294+ return nil , fmt .Errorf ("no files found that match filters" )
295+ }
296+ return filtered , nil
297+ }
298+
299+ func (c Slskd ) queueDownload (files []File , track * models.Track ) error {
300+ for i , file := range files {
301+ reqParams := fmt .Sprintf ("/api/v0/transfers/downloads/%s" , file .Username )
302+ payload := []DownloadPayload {
303+ {
304+ Filename : file .Name ,
305+ Size : file .Size ,
306+ },
307+ }
308+
309+ DLpayload , err := json .Marshal (payload )
310+ if err != nil {
311+ return fmt .Errorf ("failed to marshal payload: %s" , err .Error ())
304312 }
313+
314+ _ , err = c .HttpClient .MakeRequest ("POST" , c .Cfg .URL + reqParams , bytes .NewBuffer (DLpayload ), c .Headers )
315+ if err != nil {
316+ debug .Debug (fmt .Sprintf ("[%d/%d] failed to queue download for '%s': %s" , i + 1 , len (files ), file .Name , err .Error ()))
317+ continue
318+ }
319+ track .MainArtistID = file .Username
320+ track .Size = file .Size
321+ track .File = file .Name
322+
323+ return nil
305324 }
306- return File {}, fmt .Errorf ("no files found that match filters" )
325+ return fmt .Errorf ("couldn't download track: %s - %s" , track . CleanTitle , track . Artist )
307326}
308327
309328
0 commit comments