@@ -65,7 +65,13 @@ type PlexSearch struct {
6565 Media []struct {
6666 ID int `json:"id"`
6767 Duration int `json:"duration"`
68- File string `json:"file"`
68+ Part []struct {
69+ ID int `json:"id"`
70+ Key string `json:"key"`
71+ Duration int `json:"duration"`
72+ File string `json:"file"`
73+ Size int `json:"size"`
74+ } `json:"Part"`
6975 AudioChannels int `json:"audioChannels"`
7076 AudioCodec string `json:"audioCodec"`
7177 Container string `json:"container"`
@@ -325,34 +331,38 @@ func (c *Plex) getServer() error {
325331 return nil
326332}
327333
328- func getPlexSong (track * models.Track , searchResults PlexSearch ) (string , error ) { // match track with Plex search result
334+ func getPlexSong (track * models.Track , searchResults PlexSearch ) (string , error ) {
335+ loweredArtist := strings .ToLower (track .MainArtist )
336+
329337 for _ , result := range searchResults .MediaContainer .SearchResult {
330338 md := result .Metadata
331339 if md .Type != "track" {
332340 continue
333341 }
334342
335- // Direct match on title and artist/album
336343 titleMatch := strings .EqualFold (md .Title , track .Title ) || strings .EqualFold (md .Title , track .CleanTitle )
337344 albumMatch := strings .EqualFold (md .ParentTitle , track .Album )
338- artistMatch := strings .Contains (strings .ToLower (md .OriginalTitle ), strings . ToLower ( track . MainArtist )) || strings .Contains (strings .ToLower (md .GrandparentTitle ), strings . ToLower ( track . MainArtist ) )
345+ artistMatch := strings .Contains (strings .ToLower (md .OriginalTitle ), loweredArtist ) || strings .Contains (strings .ToLower (md .GrandparentTitle ), loweredArtist )
339346
340347 if titleMatch && (albumMatch || artistMatch ) {
341348 return md .Key , nil
342349 }
343350
344- // Duration and filename fallback
345- if len (md .Media ) > 0 {
346- media := md .Media [0 ]
347- pathMatch := strings .Contains (strings .ToLower (media .File ), strings .ToLower (track .File ))
348- durationDiff := util .Abs (media .Duration - track .Duration ) < 10000
349- if durationDiff && pathMatch {
350- return md .Key , nil
351- }
351+ if track .File == "" || len (md .Media ) == 0 || len (md .Media [0 ].Part ) == 0 {
352+ continue
353+ }
354+
355+ media := md .Media [0 ]
356+ pathMatch := strings .Contains (strings .ToLower (media .Part [0 ].File ), strings .ToLower (track .File ))
357+ durationMatch := util .Abs (media .Duration - track .Duration ) < 10000 // duration within 10s
358+
359+ if durationMatch && pathMatch {
360+ return md .Key , nil
352361 }
353362 }
363+
354364 debug .Debug (fmt .Sprintf ("full search result: %v" , searchResults .MediaContainer .SearchResult ))
355- return "" , fmt .Errorf ("failed to find '%s' by '%s' in %s album " , track .Title , track .Artist , track .Album )
365+ return "" , fmt .Errorf ("failed to find '%s' by '%s' in '%s' " , track .Title , track .Artist , track .Album )
356366}
357367
358368func (c * Plex ) addtoPlaylist (tracks []* models.Track ) {
0 commit comments