Recoded the get route, still needs some work

This commit is contained in:
Arne van Iterson 2019-09-20 23:04:06 +02:00
parent c0f53c6d2c
commit fd29ace545
7 changed files with 123 additions and 37 deletions

View File

@ -12,7 +12,7 @@ var http = require('http');
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
var port = normalizePort(process.env.PORT || '673');
app.set('port', port);
/**

View File

@ -2,10 +2,12 @@
{
"id": 0,
"name": "The Razors Edge",
"artist": 0,
"artist": [
0
],
"image": "ACDC/Razorsedge.jpg",
"songs": [
0, 1
"song": [
0, 1, 34
]
}
]

View File

@ -2,11 +2,11 @@
{
"id": 0,
"name": "AC/DC",
"albums": [
"album": [
0
],
"songs": [
0, 1
"song": [
0, 1, 34
]
}
]

View File

@ -2,17 +2,37 @@
{
"id": 0,
"name": "Thunderstruck",
"artist": 0,
"album": 0,
"artist": [
0
],
"album": [
0
],
"path": "ACDC/ACDC - Thunderstruck.mp3",
"liked": true
},
{
"id": 1,
"name": "Moneytalks",
"artist": 0,
"album": 0,
"artist": [
0
],
"album": [
0
],
"path": "ACDC/ACDC - Moneytalks.mp3",
"liked": false
},
{
"id": 34,
"name": "Highway to Hell",
"artist": [
0
],
"album": [
0
],
"path": "ACDC/ACDC - Highway to Hell.mp3",
"liked": false
}
]

8
package-lock.json generated
View File

@ -539,6 +539,14 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
},
"node-id3": {
"version": "0.1.11",
"resolved": "https://registry.npmjs.org/node-id3/-/node-id3-0.1.11.tgz",
"integrity": "sha512-lNBa5k0oxLyWaRBsMABJcctDHoK7T2qGg44c3KbAZiH/7IHx7a2Uo4f2RFBqeC0XvD1dIQrJjjtb49iE0MQ8dA==",
"requires": {
"iconv-lite": "^0.4.15"
}
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",

View File

@ -11,6 +11,7 @@
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"node-id3": "^0.1.11",
"pug": "^2.0.4",
"youtube-mp3-downloader": "^0.6.3"
}

View File

@ -6,6 +6,12 @@ var urlParse = require("body-parser");
var id3 = require('node-id3');
var router = express.Router();
router.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var YD = new YoutubeMp3Downloader({
"ffmpegPath": path.join(__dirname + "/../bin/ffmpeg"),
"outputPath": path.join(__dirname + "/../music"),
@ -20,6 +26,7 @@ router.get('/', (_req, res, _next) => {
res.render('index', { title: 'Express' });
});
// Download route to download youtube video's
router.get('/download', (req, res, _next) => {
YD.download(req.query.url);
@ -32,37 +39,80 @@ router.get('/download', (req, res, _next) => {
});
});
router.get('/get/:type/:id', (req, res, _next) => {
const type = req.params.type;
const id = req.params.id;
let db = {};
if (type === 'song') {
db = require(rootdir + 'database/songs.json');
} else if (type === 'album') {
db = require(rootdir + "database/albums.json");
} else if (type === 'artist') {
db = require(rootdir + "database/artists.json");
}
function dbGet(type, id) {
let db = {
song: require(rootdir + 'database/songs.json'),
album: require(rootdir + "database/albums.json"),
artist: require(rootdir + "database/artists.json")
};
let result;
if (id === 'all') {
res.json({result: db})
result = db[type];
} else {
const arg = id.split(",");
if (arg.length > 1) {
let result = [];
arg.forEach(element => {
result.push(db[Number(element)]);
id = parseInt(id);
db[type].forEach(element => {
if (id == element.id) {
result = element;
}
});
res.json({ result: result });
} else {
res.json({ result: db[id] });
}
}
return result;
}
router.get('/get/:type/:id', (req, res, _next) => {
// Get variables from url
const type = req.params.type;
const id = req.params.id;
// Check if all info is requested
if (id === 'all') {
res.json({ result: dbGet(type, id) });
} else {
// Split arguments and reset result
const arg = id.split(",");
resultArray = [];
// TODO
// OK, for some reason this script works absolutely fine when run the fist time,
// However, if you run any other get url or refresh the page it will either throw a circulation error or return a bunch of null values
// I have no idea what is causing this but i am absolutely fucking done with it right now
// If you read this and you know how to fix it, please change it
// For each array in argument, make object in resultArray
for (let index = 0; index < arg.length; index++) {
resultArray[index] = dbGet(type, arg[index]);
// Add song info if id is given
if (typeof resultArray[index].song !== 'undefined') {
for (let song = 0; song < resultArray[index].song.length; song++) {
resultArray[index].song[song] = dbGet('song', resultArray[index].song[song]);
}
}
// Add artist info if id is given
if (typeof resultArray[index].artist !== 'undefined') {
for (let artist = 0; artist < resultArray[index].artist.length; artist++) {
resultArray[index].artist[artist] = dbGet('artist', resultArray[index].artist[artist]);
}
}
// Add album info if id is given
if (typeof resultArray[index].album !== 'undefined') {
for (let album = 0; album < resultArray[index].album.length; album++) {
resultArray[index].album[album] = dbGet('album', resultArray[index].album[album]);
}
}
}
// Print result
res.json({ result: resultArray });
}
});
// Play route to stream track
router.get('/play/:track', (req, res, _next) => {
var key = req.params.track;
@ -106,12 +156,14 @@ router.get('/play/:track', (req, res, _next) => {
readStream.pipe(res);
});
// Image route to get album image
router.get('/image/:path', (req, res, _next) => {
const path = '/music/' + req.params.path
res.sendFile(path, { root: rootdir });
});
// Search route to search for a query in the db
router.get('/search/:query', (req, res, _next) => {
const query = req.params.query;
let result = [];
@ -135,13 +187,16 @@ router.get('/search/:query', (req, res, _next) => {
});
router.use(urlParse.urlencoded({extended : true}));
// Analyse route to scan .mp3 ID3 tags
router.get('/analyse/*', (req, res, _next) => {
let file = req.params[0];
let file = '/' + req.params[0];
if (fs.existsSync(file)) {
var result = id3.read(file);
result.image.base64 = result.image.imageBuffer.toString('base64');
if (result.image.imageBuffer) {
result.image.base64 = result.image.imageBuffer.toString('base64');
}
} else {
var result = 'File not found'
var result = `File ${file} not found`;
}
res.json({ result: result });
});