diff --git a/frontend/src/appleMusicRoutes.js b/frontend/src/appleMusicRoutes.js index dd19e99..9fb0af1 100644 --- a/frontend/src/appleMusicRoutes.js +++ b/frontend/src/appleMusicRoutes.js @@ -8,6 +8,28 @@ */ const path = require( 'path' ); +const dialog = require( 'electron' ).dialog; + +const analyzeFile = ( filepath ) => { + return new Promise( ( resolve, reject ) => { + if ( filepath.includes( '.csv' ) ) { + // This will assume that line #1 will be song #1 in the file list + // (when sorted by name) + let results = {}; + let pos = 0; + fs.createReadStream( filepath ) + .pipe( csv() ) + .on( 'data', ( data ) => { + results[ pos ] = data; + pos += 1; + } ).on( 'end', () => { + resolve( results ); + } ); + } else if ( filepath.includes( '.json' ) ) { + resolve( JSON.parse( fs.readFileSync( filepath ) ) ); + } + } ); +} module.exports = ( app ) => { app.get( '/apple-music', ( req, res ) => { @@ -29,4 +51,22 @@ module.exports = ( app ) => { app.get( '/logo.png', ( req, res ) => { res.sendFile( path.join( __dirname + '/client/logo.png' ) ); } ); + + app.get( '/apple-music/getAdditionalData', ( req, res ) => { + const filepath = dialog.showOpenDialogSync( { + properties: [ 'openFile' ], + title: 'Open file with additional data on the songs', + filters: [ + { + name: 'CSV', extensions: [ '.csv' ], + name: 'JSON', extensions: [ '.json' ] + } + ] + } ); + analyzeFile( filepath ).then( analyzedFile => { + res.send( analyzeFile ); + } ).catch( err => { + res.status( 500 ).send( 'no csv / json file' ); + } ) + } ); } \ No newline at end of file diff --git a/frontend/src/client/appleMusic/index.js b/frontend/src/client/appleMusic/index.js index 426bb06..2396457 100644 --- a/frontend/src/client/appleMusic/index.js +++ b/frontend/src/client/appleMusic/index.js @@ -80,8 +80,11 @@ const app = Vue.createApp( { 'duration': Math.round( e.item.attributes.durationInMillis / 1000 ), 'filename': e.item.id, 'coverArtOrigin': 'api', - 'coverArtURL': e.item.attributes.artwork.url, } + let url = e.item.attributes.artwork.url; + url = url.replace( '{w}', e.item.attributes.artwork.width ); + url = url.replace( '{h}', e.item.attributes.artwork.height ); + this.songQueue[ item ][ 'coverArtURL' ] = url; } ); this.apiGetRequest( 'https://api.music.apple.com/v1/me/library/playlists', this.playlistHandler ); } ); @@ -133,7 +136,25 @@ const app = Vue.createApp( { this.musicKit.setQueue( { songs: tracks } ).then( () => { try { this.musicKit.play(); - this.songQueue = this.musicKit.player.queue.items; + const songQueue = this.musicKit.player.queue.items; + for ( let item in songQueue ) { + this.songQueue[ item ] = { + 'artist': songQueue[ item ].attributes.artistName, + 'title': songQueue[ item ].attributes.name, + 'year': songQueue[ item ].attributes.releaseDate, + // Think about bpm analysis + // 'bpm': metadata[ 'common' ][ 'bpm' ], + 'genre': songQueue[ item ].attributes.genreNames, + 'duration': Math.round( songQueue[ item ].attributes.durationInMillis / 1000 ), + 'filename': songQueue[ item ].id, + 'coverArtOrigin': 'api', + } + let url = songQueue[ item ].attributes.artwork.url; + url = url.replace( '{w}', songQueue[ item ].attributes.artwork.width ); + url = url.replace( '{h}', songQueue[ item ].attributes.artwork.height ); + this.songQueue[ item ][ 'coverArtURL' ] = url; + } + // TODO: Load additional data from file this.hasSelectedPlaylist = true; this.isPreparingToPlay = false; } catch( err ) {