var child_process = require("child_process"); var express = require('express'); var url = require('url'); var tagger = require('./tagger'); var app = express(); app.get('/artist/:artist', function (req, resp) { var artist = req.params.artist; function f(a){ resp.send(JSON.stringify(a)); } tagger.getArtist(artist,f); }); app.get('/artists', function (req, resp) { function f(a){ resp.send(JSON.stringify(a)); } tagger.getArtists(f); }); app.get('/artists/albums', function (req, resp) { function f(a){ // resp.send(JSON.stringify(a)); // console.log(a); resp.send(a); } tagger.getAllArtistWithAlbums(f); }); app.get('/albums', function (req, resp) { function f(a){ resp.send(JSON.stringify(a)); } tagger.getAlbums(f); }); app.get('/album/:album', function (req, resp) { var album = req.params.album; function f(a){ resp.send(JSON.stringify(a)); } tagger.getAlbum(album,f); }); app.get('/album/artist/:artist', function (req, resp) { var artist = req.params.artist; function f(a){ resp.send(JSON.stringify(a)); } tagger.getAlbumByArtist(artist,f); }); app.get('/song/:song', function (req, resp) { resp.writeHead(200, { "Connection": "keep-alive" , "Content-Type": "audio/ogg" , "Accept-Ranges": "bytes" // Helps Chrome }); var song = req.params.song; if (isNumeric(song)){ tagger.pathFromID(song, transcode); }else{ transcode(song); } function transcode(path){ console.log("Transcoding " + path); transcoder = child_process.spawn("/usr/bin/ffmpeg", [ "-i",path, "-c:a", "libvorbis", "-vn", "-b:a", "192k", "-f", "ogg", "-y", "-" ], {detached: false}); transcoder.stdout.pipe(resp); transcoder.stdout.on("data",function(data) { }); req.on("close", function () { shutStream("closed") }) req.on("end", function () { shutStream("ended") }); function shutStream(event) { if (typeof transcoder !== "undefined") transcoder.kill(); } } return true; }); var server = app.listen(1337, function () { console.log("listenin"); }); function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); }