Browse Source

ugly code

master
Ambrose Chua 7 years ago
parent
commit
060403aeff
  1. 3
      .gitignore
  2. 210
      index.js
  3. 146
      index.js.pipesha
  4. 19
      package.json

3
.gitignore

@ -0,0 +1,3 @@
music/
node_modules/
.idea/

210
index.js

@ -0,0 +1,210 @@
var fs = require("fs");
var path = require("path");
var mm = require("musicmetadata");
var express = require("express");
var art = {
artists: {}
};
var tracks = {
artists: {}
};
var music = {
artists: {}
};
var promise = new Promise(function (resolve, reject) {
fs.readdir(path.join(__dirname, "music"), function (err, files) {
if (err || !files) {
resolve();
return;
}
var promises = [];
files.forEach(function (artist) {
if (artist.charAt(0) == ".") {
return;
}
art.artists[artist] = {
albums: {},
picture: []
};
tracks.artists[artist] = {
albums: {}
};
music.artists[artist] = {
albums: {}
};
var promise = new Promise(function (resolve, reject) {
fs.readdir(path.join(__dirname, "music", artist), function (err, files) {
if (err || !files) {
resolve();
return;
}
var promises = [];
files.forEach(function (album) {
if (album.charAt(0) == ".") {
return;
}
art.artists[artist].albums[album] = {
tracks: [],
picture: []
};
tracks.artists[artist].albums[album] = {
tracks: []
};
music.artists[artist].albums[album] = {
tracks: []
};
var promise = new Promise(function (resolve, reject) {
fs.readdir(path.join(__dirname, "music", artist, album), function (err, files) {
if (err || !files) {
resolve();
return;
}
var promises = [];
files.forEach(function (title) {
if (title.charAt(0) == ".") {
return;
}
var fn = path.join(__dirname, "music", artist, album, title);
var promise = new Promise(function (resolve, reject) {
fs.stat(fn, function (err, stat) {
if (err || !stat) {
resolve();
return;
}
if (stat.isFile()) {
if (title.match(/(.mp3|.wav|.m4a)/)) {
mm(fs.createReadStream(fn), function (err, metadata) {
if (err || !metadata) {
console.log(err);
}
else {
var number = parseInt(title.split(" ")[0], 10);
if (isNaN(number)) {
number = music.artists[artist].albums[album].tracks.length + 1;
}
metadata.number = number;
metadata.file = "/tracks/" + artist + "/" + album + "/" + title;
music.artists[artist].albums[album].tracks[number - 1] = metadata;
tracks.artists[artist].albums[album].tracks[number - 1] = {
filename: fn,
file: metadata.file
};
art.artists[artist].albums[album].tracks[number - 1] = {
picture: []
};
art.artists[artist].albums[album].tracks[number - 1].picture = metadata.picture;
if (!art.artists[artist].albums[album].picture[0] && metadata.picture[0]) {
art.artists[artist].albums[album].picture = metadata.picture;
}
if (!art.artists[artist].picture[0] && metadata.picture[0]) {
art.artists[artist].picture = metadata.picture;
}
music.artists[artist].albums[album].tracks[number - 1].picture = undefined;
}
resolve();
});
}
else if (title.match(/(.jpg|.png)/)) {
fs.readFile(fn, function (err, data) {
if (!err) {
var fns = fn.split(".");
art.artists[artist].albums[album].picture = [{
format: fns[fns.length - 1],
data: data
}];
art.artists[artist].picture = art.artists[artist].albums[album].picture;
}
resolve();
});
}
else {
resolve();
}
}
else {
resolve();
}
});
});
promises.push(promise);
});
Promise.all(promises).then(function () {
music.artists[artist].albums[album].tracks.forEach(function (track, i) {
if ((!art.artists[artist].albums[album].tracks[i].picture[0]) && art.artists[artist].albums[album].picture[0]) {
art.artists[artist].albums[album].tracks[i].picture = art.artists[artist].albums[album].picture;
}
});
if (music.artists[artist].albums[album].tracks.length < 1) {
delete music.artists[artist].albums[album];
}
resolve();
});
});
});
promises.push(promise);
});
Promise.all(promises).then(function () {
if (music.artists[artist].albums.length < 1) {
delete music.artists[artist];
}
resolve();
});
});
});
promises.push(promise);
});
Promise.all(promises).then(resolve);
});
});
promise.then(function () {
try {
for (var artist in music.artists) {
for (var album in music.artists[artist].albums) {
for (var track in music.artists[artist].albums[album].tracks) {
music.artists[artist].albums[album].tracks[track].picture = "/art/" + artist + "/" + album + "/" + (parseInt(track, 10) + 1);
}
music.artists[artist].albums[album].picture = "/art/" + artist + "/" + album;
}
music.artists[artist].picture = "/art/" + artist;
}
}
catch (e) {
console.error(e);
}
console.log("done");
});
var app = express();
app.get("/", function (req, res) {
res.end(JSON.stringify(music));
});
app.get("/art/:artist", function (req, res) {
res.end(art.artists[req.params.artist].picture[0].data);
});
app.get("/art/:artist/:album", function (req, res) {
res.end(art.artists[req.params.artist].albums[req.params.album].picture[0].data);
});
app.get("/art/:artist/:album/:track", function (req, res) {
res.end(art.artists[req.params.artist].albums[req.params.album].tracks[parseInt(req.params.track, 10) - 1].picture[0].data);
});
app.get("/tracks/:artist/:album/:track", function (req, res, next) {
if (parseInt(req.params.track, 10) == req.params.track) {
res.sendFile(tracks.artists[req.params.artist].albums[req.params.album].tracks[parseInt(req.params.track, 10) - 1].filename);
}
else {
next();
}
});
app.use("/tracks", express.static("music"));
app.listen(8080);

146
index.js.pipesha

@ -0,0 +1,146 @@
var fs = require("fs");
var path = require("path");
var mm = require("musicmetadata");
var express = require("express");
var tracks = {};
var art = {};
var music = {
artists: {}
};
var promise = new Promise(function (resolve, reject) {
fs.readdir(path.join(__dirname, "music"), function (err, files) {
var promises = [];
files.forEach(function (artist) {
art.artists[artist] = {
albums: {},
picture: []
};
music.artists[artist] = {
albums: {}
};
var promise = new Promise(function (resolve, reject) {
fs.readdir(path.join(__dirname, "music", artist), function (err, files) {
var promises = [];
files.forEach(function (album) {
art.artists[artist].albums[album] = {
tracks: [],
picture: []
};
music.artists[artist].albums[album] = {
tracks: []
};
var promise = new Promise(function (resolve, reject) {
fs.readdir(path.join(__dirname, "music", artist, album), function (err, files) {
var promises = [];
files.forEach(function (title) {
var fn = path.join(__dirname, "music", artist, album, title);
var promise = new Promise(function (resolve, reject) {
fs.stat(fn, function (err, stat) {
if (stat.isFile()) {
if (fn.match(/(.mp3|.wav)/) && fn.charAt(0) != ".") {
mm(fs.createReadStream(fn), function (err, metadata) {
if (err) {
console.log(err);
resolve();
}
else {
var number = parseInt(title.split(" ")[0], 10);
if (isNaN(number)) {
number = music.artists[artist].albums[album].tracks.length;
}
// Art
/*art.artists[artist].albums[album].tracks[number - 1] = {
picture: []
};
art.artists[artist].albums[album].tracks[number - 1].picture = metadata.picture;
if (!art.artists[artist].albums[album].picture[0] && metadata.picture[0]) {
art.artists[artist].albums[album].picture = metadata.picture;
}
if (!art.artists[artist].picture[0] && metadata.picture[0]) {
art.artists[artist].picture = metadata.picture;
}*/
var sha = crypto.createHash("sha1");
fs.createReadStream(fn).pipe(sha).on("end", function () {
metadata.number = number;
metadata.file = "/tracks/" + sha.read();
tracks[sha.read()] = new Buffer();
fs.createReadStream(fn).pipe(tracks[sha.read()]);
music.artists[artist].albums[album].tracks[number - 1] = metadata;
resolve();
});
}
});
}
else if (fn.match(/(.jpg|.png)/)) {
fs.readFile(fn, function (err, data) {
if (!err) {
var fns = fn.split(".");
art.artists[artist].albums[album].picture = [{
format: fns[fns.length - 1],
data: data
}];
art.artists[artist].picture = art.artists[artist].albums[album].picture;
}
resolve();
});
}
else {
resolve();
}
}
else {
resolve();
}
});
});
promises.push(promise);
});
Promise.all(promises).then(function () {
music.artists[artist].albums[album].tracks.forEach(function (track, i) {
if ((!art.artists[artist].albums[album].tracks[i].picture[0]) && art.artists[artist].albums[album].picture[0]) {
art.artists[artist].albums[album].tracks[i].picture = art.artists[artist].albums[album].picture;
}
});
if (music.artists[artist].albums[album].tracks.length < 1) {
music.artists[artist].albums[album] = undefined;
}
resolve();
});
});
});
promises.push(promise);
});
Promise.all(promises).then(resolve);
});
});
promises.push(promise);
});
Promise.all(promises).then(resolve);
});
});
promise.then(function () {
console.log("done");
for (var artist in music.artists) {
for (var album in music.artists[artist].albums) {
for (var track in music.artists[artist].albums[album].tracks) {
}
//music.artists[artist].albums[album].picture = ""
}
}
});
var app = express();
app.get("/", function (req, res) {
res.end(JSON.stringify(music));
});

19
package.json

@ -0,0 +1,19 @@
{
"name": "Undefined-server",
"version": "0.0.1",
"description": "commongoods",
"scripts": {
"start": "node index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/ambrosechua/Undefined-server.git"
},
"dependencies": {
"bluebird": "^2.10.2",
"body-parser": "^1.12.4",
"express": "^4.12.4",
"musicmetadata": "^2.0.1"
},
"private": true
}
Loading…
Cancel
Save