updates that break stuff temporarily

This commit is contained in:
2023-11-05 17:36:20 +01:00
parent 950cb3d471
commit c280d4357c
9 changed files with 406 additions and 90 deletions

View File

@@ -4,10 +4,10 @@ const path = require( 'path' );
const cors = require( 'cors' );
const fs = require( 'fs' );
const bodyParser = require( 'body-parser' );
const musicMetadata = require( 'music-metadata' );
const dialog = require( 'electron' ).dialog;
const session = require( 'express-session' );
app.use( bodyParser.urlencoded( { extended: false } ) );
app.use( bodyParser.json() );
app.use( cors() );
@@ -17,9 +17,6 @@ app.use( session( {
resave: false,
} ) );
let indexedData = {};
let coverArtIndex = {};
const allowedFileTypes = [ '.mp3', '.wav', '.flac' ];
let connectedClients = {};
let changedStatus = [];
@@ -123,59 +120,11 @@ app.get( '/openSongs', ( req, res ) => {
app.get( '/indexDirs', ( req, res ) => {
if ( req.query.dir ) {
// TODO: Load from json file
if ( indexedData[ req.query.dir ] ) {
res.send( indexedData[ req.query.dir ] );
} else {
fs.readdir( req.query.dir, { encoding: 'utf-8' }, ( err, dat ) => {
if ( err ) {
res.status( 404 ).send( 'ERR_DIR_NOT_FOUND' );
return;
};
( async() => {
// TODO: Check for songlist.csv or songlist.json file and use the data provided there for each song to override
// what was found automatically. If no song title was found in songlist or metadata, use filename
// TODO: Also save found information to those files and don't rerun checks if data is present
let files = {};
for ( let file in dat ) {
if ( allowedFileTypes.includes( dat[ file ].slice( dat[ file ].indexOf( '.' ), dat[ file ].length ) ) ) {
try {
let metadata = await musicMetadata.parseFile( req.query.dir + '/' + dat[ file ] );
files[ req.query.dir + '/' + dat[ file ] ] = {
'artist': metadata[ 'common' ][ 'artist' ],
'title': metadata[ 'common' ][ 'title' ],
'year': metadata[ 'common' ][ 'year' ],
'bpm': metadata[ 'common' ][ 'bpm' ],
'genre': metadata[ 'common' ][ 'genre' ],
'duration': Math.round( metadata[ 'format' ][ 'duration' ] ),
'isLossless': metadata[ 'format' ][ 'lossless' ],
'sampleRate': metadata[ 'format' ][ 'sampleRate' ],
'bitrate': metadata[ 'format' ][ 'bitrate' ],
'numberOfChannels': metadata[ 'format' ][ 'numberOfChannels' ],
'container': metadata[ 'format' ][ 'container' ],
'filename': req.query.dir + '/' + dat[ file ],
}
if ( metadata[ 'common' ][ 'picture' ] ) {
files[ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = true;
if ( req.query.coverart == 'true' ) {
coverArtIndex[ req.query.dir + '/' + dat[ file ] ] = metadata[ 'common' ][ 'picture' ] ? metadata[ 'common' ][ 'picture' ][ 0 ][ 'data' ] : undefined;
}
} else {
files[ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = false;
}
} catch ( err ) {
console.error( err );
files[ req.query.dir + '/' + dat[ file ] ] = 'ERROR';
}
} else if ( dat[ file ].slice( dat[ file ].indexOf( '.' ), dat[ file ].length ) === '.csv' ) {
} else if ( dat[ file ].slice( dat[ file ].indexOf( '.' ), dat[ file ].length ) === '.json' ) {
}
}
indexedData[ req.query.dir ] = files;
res.send( files );
} )();
} );
res.send( files );
}
} else {
res.status( 400 ).send( 'ERR_REQ_INCOMPLETE' );
@@ -202,9 +151,6 @@ app.get( '/getSongFile', ( req, res ) => {
}
} );
// TODO: Add get lyrics route later
// 'lyrics': metadata[ 'common' ][ 'lyrics' ],
app.use( ( request, response, next ) => {
response.sendFile( path.join( __dirname + '' ) )

View File

@@ -236,8 +236,6 @@ createApp( {
analyser.getByteFrequencyData( dataArray );
let prevSpectrum = null;
let threshold = 10; // Adjust as needed
// TODO: Make sure it works as it should
// TODO: Make pos update also occur on 10 sec time jump
this.beatDetected = false;
this.micAnalyzer = setInterval( () => {
analyser.getByteFrequencyData( dataArray );

View File

@@ -270,6 +270,8 @@ export default {
} else if ( action === 'forward10' ) {
if ( musicPlayer.currentTime < ( musicPlayer.duration - 10 ) ) {
musicPlayer.currentTime = musicPlayer.currentTime + 10;
this.playbackPos = musicPlayer.currentTime;
this.sendUpdate( 'pos' );
} else {
if ( this.repeatMode !== 'one' ) {
this.control( 'next' );
@@ -283,10 +285,17 @@ export default {
clearInterval( this.progressTracker );
this.playbackPos = 0;
musicPlayer.currentTime = 0;
this.sendUpdate( 'pos' );
} else if ( action === 'next' ) {
this.$emit( 'update', { 'type': 'next' } );
} else if ( action === 'previous' ) {
this.$emit( 'update', { 'type': 'previous' } );
if ( this.playbackPos > 3 ) {
this.playbackPos = 0;
musicPlayer.currentTime = 0;
this.sendUpdate( 'pos' );
} else {
this.$emit( 'update', { 'type': 'previous' } );
}
} else if ( action === 'shuffleOff' ) {
this.$emit( 'update', { 'type': 'shuffleOff' } );
this.isShuffleEnabled = false;
@@ -337,6 +346,26 @@ export default {
this.$emit( 'update', { 'type': 'fancyView', 'status': true } );
this.isShowingFancyView = true;
},
},
created() {
document.addEventListener( 'keydown', ( e ) => {
if ( e.key === ' ' ) {
e.preventDefault();
if ( !this.isPlaying ) {
this.control( 'play' );
} else {
this.control( 'pause' );
}
} else if ( e.key === 'ArrowRight' ) {
e.preventDefault();
this.control( 'next' );
} else if ( e.key === 'ArrowLeft' ) {
e.preventDefault();
this.control( 'previous' );
} else {
console.log( e.key );
}
} );
}
}
</script>

View File

@@ -0,0 +1,5 @@
{
"teamID": "",
"keyID": "",
"storefront": "us"
}

View File

@@ -7,6 +7,25 @@
*
*/
module.exports.fetch = () => {
// TODO: Implement
const MusicKit = require( 'node-musickit-api' );
const fs = require( 'fs' );
const path = require( 'path' );
// TODO: deploy non-secret version
const settings = JSON.parse( fs.readFileSync( path.join( __dirname + '/config/apple-music-api.config.secret.json' ) ) );
const music = new MusicKit( {
key: fs.readFileSync( path.join( __dirname + '/config/apple_private_key.p8' ) ).toString(),
teamId: settings.teamID, // This is your developer account's team ID
keyId: settings.keyID // This is the keys ID
} );
module.exports.fetch = ( type, searchQuery, callback ) => {
music.search( settings.storefront, type, searchQuery, ( err, data ) => {
if ( err ) {
callback( err, null );
return;
}
callback( null, data );
} );
}

99
frontend/src/indexer.js Normal file
View File

@@ -0,0 +1,99 @@
const fs = require( 'fs' );
const imageFetcher = require( './imageFetcher.js' );
const musicMetadata = require( 'music-metadata' );
const allowedFileTypes = [ '.mp3', '.wav', '.flac' ];
let indexedData = {};
let coverArtIndex = {};
module.exports.index = ( req ) => {
return new Promise( ( reject, resolve ) => {
fs.readdir( req.query.dir, { encoding: 'utf-8' }, ( err, dat ) => {
if ( err ) {
res.status( 404 ).send( 'ERR_DIR_NOT_FOUND' );
return;
};
( async() => {
// TODO: Check for songlist.csv or songlist.json file and use the data provided there for each song to override
// what was found automatically. If no song title was found in songlist or metadata, use filename
// TODO: Also save found information to those files and don't rerun checks if data is present
if ( dat.includes( 'songlist.csv' ) || dat.includes( 'songlist.json' ) ) {
} else {
}
} )();
} );
} );
}
const parseExistingData = () => {
}
const parseDir = async ( dat, req ) => {
let files = {};
for ( let file in dat ) {
if ( allowedFileTypes.includes( dat[ file ].slice( dat[ file ].indexOf( '.' ), dat[ file ].length ) ) ) {
try {
let metadata = await musicMetadata.parseFile( req.query.dir + '/' + dat[ file ] );
files[ req.query.dir + '/' + dat[ file ] ] = {
'artist': metadata[ 'common' ][ 'artist' ],
'title': metadata[ 'common' ][ 'title' ],
'year': metadata[ 'common' ][ 'year' ],
'bpm': metadata[ 'common' ][ 'bpm' ],
'genre': metadata[ 'common' ][ 'genre' ],
'duration': Math.round( metadata[ 'format' ][ 'duration' ] ),
'isLossless': metadata[ 'format' ][ 'lossless' ],
'sampleRate': metadata[ 'format' ][ 'sampleRate' ],
'bitrate': metadata[ 'format' ][ 'bitrate' ],
'numberOfChannels': metadata[ 'format' ][ 'numberOfChannels' ],
'container': metadata[ 'format' ][ 'container' ],
'filename': req.query.dir + '/' + dat[ file ],
}
if ( metadata[ 'common' ][ 'picture' ] ) {
files[ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = true;
if ( req.query.coverart == 'true' ) {
coverArtIndex[ req.query.dir + '/' + dat[ file ] ] = metadata[ 'common' ][ 'picture' ] ? metadata[ 'common' ][ 'picture' ][ 0 ][ 'data' ] : undefined;
}
} else {
if ( req.query.coverart == 'true' ) {
imageFetcher.fetch( 'songs', metadata[ 'common' ][ 'artist' ] + ' ' + metadata[ 'common' ][ 'title' ], ( err, data ) => {
if ( err ) {
indexedData[ req.query.dir ][ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = false;
files[ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = false;
return;
}
if ( data.results.songs ) {
if ( data.results.songs.data ) {
let url = data.results.songs.data[ 0 ].attributes.artwork.url;
url = url.replace( '{w}', data.results.songs.data[ 0 ].attributes.artwork.width );
url = url.replace( '{h}', data.results.songs.data[ 0 ].attributes.artwork.height );
console.log( url );
} else {
indexedData[ req.query.dir ][ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = false;
files[ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = false;
}
} else {
indexedData[ req.query.dir ][ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = false;
files[ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = false;
}
} );
files[ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = true;
} else {
files[ req.query.dir + '/' + dat[ file ] ][ 'hasCoverArt' ] = false;
}
}
} catch ( err ) {
console.error( err );
files[ req.query.dir + '/' + dat[ file ] ] = 'ERROR';
}
} else if ( dat[ file ].slice( dat[ file ].indexOf( '.' ), dat[ file ].length ) === '.csv' ) {
} else if ( dat[ file ].slice( dat[ file ].indexOf( '.' ), dat[ file ].length ) === '.json' ) {
}
}
indexedData[ req.query.dir ] = files;
return files;
}