Browse Source

Add (crude) playlist, redirect after url given

Josh Bicking 7 years ago
parent
commit
c8d03572e6
2 changed files with 30 additions and 12 deletions
  1. 29 12
      routes/index.js
  2. 1 0
      views/index.hbs

+ 29 - 12
routes/index.js

@@ -9,19 +9,24 @@ router.use(expressValidator());
 var child_process = require('child_process');
 
 var Player = require('player');
-
-/* Example player code
-var player = new Player('./downloads/Doodlebob - Bring Me to Life.mp3');
-// play now and callback when playend 
-player.play(function(err, player){
-    console.log('playend!');
-});
-player.play();
-*/
+var player = new Player();
 
 /* GET home page. */
 router.get('/', function(req, res, next) {
-  res.render('index');
+    var playlist;
+    if(player.list.length) {
+	playlist = '<ul>';
+	for(var i=0; i < player.list.length; i++) {
+	    playlist +=
+		'<li>' +
+		player.list[i].replace(/^\.\/downloads\//, '') .replace(/\.mp3$/, '') +
+		'</li>';
+	}
+	playlist += '</ul>';
+    } else {
+	playlist = '<p>No songs in playlist.</p>';
+    }
+    res.render('index', { playlist: playlist });
 });
 
 router.get('/youtube', function(req, res, next) {
@@ -56,8 +61,20 @@ router.post('/youtube', function(req, res) {
 	});
     } else {
 	var youtube_dl = child_process.spawn('/usr/bin/youtube-dl', ['-x', '--audio-format', 'mp3', '-o', 'downloads/%(title)s.%(ext)s', video]);
-	// TODO eval video
-	res.render('index');
+	youtube_dl.on('close', (code) => {
+	    console.log("Done getting " + video);
+	    var error;
+	    youtube_dl.stderr.on('data', (data) => {
+		error = data;
+		console.log(`Error getting video: ${data}`);
+		// TODO give error to user when they return to /
+	    });
+
+	    if(!error) {
+		var youtube_dl_get_title = child_process.spawnSync('/usr/bin/youtube-dl', ['--get-title', video]);
+		player.add('./downloads/' + youtube_dl_get_title.stdout.toString().replace('\n', '') + ".mp3");
+	    }
+	res.redirect('/');
     }
 });
 

+ 1 - 0
views/index.hbs

@@ -1,4 +1,5 @@
 <h1>LAN Jukebox</h1>
 <a href="/youtube">Add a Youtube video</a><br/>
 <a href="/file">Add a music file</a><br/>
+{{{playlist}}}
 <p>Allan put music player here</p>