@@ -128,7 +128,7 @@ async function parse(url: string) {
128128 artist : artists ,
129129 score : isNaN ( score ) ? - 1 : score ,
130130 ratings : isNaN ( ratings ) ? - 1 : ratings ,
131- discNumber : discIndex ,
131+ discNumber : discIndex + 1 ,
132132 url : trackURL ,
133133 } ;
134134
@@ -190,14 +190,14 @@ async function parse(url: string) {
190190 score : criticScore ,
191191 ratings : criticRatingsCount ,
192192 } ,
193- }
194- Album . format = format
195- Album . year = year
196- Album . label = labels
197- Album . url = url
198- Album . verified = verified
199- Album . tracks = Tracks
200- Album . valid = true
193+ } ;
194+ Album . format = format ;
195+ Album . year = year ;
196+ Album . label = labels ;
197+ Album . url = url ;
198+ Album . verified = verified ;
199+ Album . tracks = Tracks ;
200+ Album . valid = true ;
201201
202202 console . log ( `[aotyfy] api response:` , Album ) ;
203203 return Album ;
@@ -214,7 +214,7 @@ export async function getAPI(meta: AOTYFY._Meta, firstIteration: boolean = true,
214214 * caching albums in local storage
215215 */
216216 const SAID = extractStr ( / ^ s p o t i f y : a l b u m : ( .+ ) $ / , meta . album . uri ) ;
217- const Storage = Settings . storage . get ( )
217+ const Storage = Settings . storage . get ( ) ;
218218
219219 if ( SAID && Storage && ! force ) {
220220 const cachedAOTYID = Storage [ SAID ] ;
@@ -305,9 +305,7 @@ export async function getAPI(meta: AOTYFY._Meta, firstIteration: boolean = true,
305305 } ) ) ;
306306
307307 // find the most similar artist
308- const mostSimilarByArtist = releasesWithSimilarity . reduce ( ( best , current ) =>
309- current . artistSimilarity > best . artistSimilarity ? current : best ,
310- ) ;
308+ const mostSimilarByArtist = releasesWithSimilarity . reduce ( ( best , current ) => ( current . artistSimilarity > best . artistSimilarity ? current : best ) ) ;
311309
312310 console . debug ( `[AOTYfy] most similar artist: ${ mostSimilarByArtist . artist } (${ mostSimilarByArtist . artistSimilarity } )` ) ;
313311
@@ -324,9 +322,7 @@ export async function getAPI(meta: AOTYFY._Meta, firstIteration: boolean = true,
324322
325323 // if artist has multiple albums, find the most similar album
326324 if ( artistReleases . length > 1 ) {
327- const bestAlbumMatch = artistReleases . reduce ( ( best , current ) =>
328- current . albumSimilarity ! > best . albumSimilarity ! ? current : best ,
329- ) ;
325+ const bestAlbumMatch = artistReleases . reduce ( ( best , current ) => ( current . albumSimilarity ! > best . albumSimilarity ! ? current : best ) ) ;
330326 console . debug ( `[aotyfy] multiple albums from artist, best match: ${ bestAlbumMatch . title } ` ) ;
331327 aotyURL = bestAlbumMatch . url ;
332328 }
@@ -372,7 +368,7 @@ export async function getAPI(meta: AOTYFY._Meta, firstIteration: boolean = true,
372368
373369 console . debug ( "[aotyfy] cached release: " , SAID , "->" , aotyId ) ;
374370 } else {
375- console . warn ( `[aotyfy] can't extract 'aotyId': ${ aotyId } ` )
371+ console . warn ( `[aotyfy] can't extract 'aotyId': ${ aotyId } ` ) ;
376372 }
377373 }
378374
@@ -384,22 +380,35 @@ export async function getAPI(meta: AOTYFY._Meta, firstIteration: boolean = true,
384380 */
385381export function getTrack ( meta : AOTYFY . _Meta , album : AOTYFY . _Album ) : AOTYFY . _Track | null {
386382 if ( ! album . tracks || album . tracks . length === 0 ) {
387- console . warn ( "[aotyfy] release doesn't contain any tracks" )
383+ console . warn ( "[aotyfy] release doesn't contain any tracks" ) ;
388384 return null ;
389385 }
390386
391- const discIndex = ( meta . track . disc ?? 1 ) - 1 ;
387+ const normalize = ( s : string ) =>
388+ decodeURIComponent ( s )
389+ . toLowerCase ( )
390+ . replace ( / [ ^ \w \s ] / g, "" )
391+ . trim ( ) ;
392+
393+ const isLocal = meta . track . uri . startsWith ( "spotify:local" ) ;
394+
395+ if ( isLocal ) {
396+ return album . tracks . find ( ( t ) => normalize ( t . title ) === normalize ( meta . track . title ) ) ?? null ;
397+ }
398+
399+ const discNumber = meta . track . disc || 1 ;
392400 const trackIndex = meta . track . number - 1 ;
393401
394- const discs = album . tracks . reduce < Record < number , typeof album . tracks > > ( ( acc , t : AOTYFY . _Track ) => {
402+ const discs = album . tracks . reduce < Record < number , typeof album . tracks > > ( ( acc , t ) => {
395403 ( acc [ t . discNumber ] ??= [ ] ) . push ( t ) ;
396404 return acc ;
397405 } , { } ) ;
398406
399- const discTracks = discs [ discIndex ] ;
407+ const discTracks = discs [ discNumber ] ;
408+
400409 if ( ! discTracks || ! discTracks [ trackIndex ] ) {
401- throw new APIError ( "unable to find this track" )
410+ return album . tracks . find ( ( t ) => normalize ( t . title ) === normalize ( meta . track . title ) ) ?? null ;
402411 }
403412
404- return discTracks [ trackIndex ] ?? album . tracks . find ( ( t ) => t . title . toLowerCase ( ) === meta . track . title . toLowerCase ( ) ) ;
413+ return discTracks [ trackIndex ] ;
405414}
0 commit comments