Permalink
Browse files

Started reworking the project in yplayer, a cli player for youtube

  • Loading branch information...
96AA48 committed Jun 4, 2015
1 parent 4e79c3d commit 3a9732171f771071d0ba411928504454a6f04892
Showing with 26 additions and 19 deletions.
  1. +1 −0 .gitignore
  2. +25 −19 app.js
View
@@ -2,3 +2,4 @@ node_modules
grooveshark-cli
*sublime*
npm-debug.log
apikey
View
44 app.js
@@ -1,6 +1,7 @@
#!/usr/bin/env node
var color = require("colors");
var GS = require("grooveshark-streaming");
var search = require('youtube-search');
var download = require('youtube-dl');
var _ = require("underscore");
var fs = require("fs");
@@ -17,8 +18,8 @@ _.each(args, function (item) {
});
function help() {
process.stdout.write("gplayer v1.0.9, by: Bram \"#96AA48\" van der Veen\n\n");
process.stdout.write("Usage : gplayer [options] <-s song>\n");
process.stdout.write("yplayer v0.0.1, by: Bram \"#96AA48\" van der Veen\n\n");
process.stdout.write("Usage : yplayer [options] <-s song>\n");
var options = [
["-s <song>, --song <song>", "Song to listen to"],
["[-o], [--offline]", "\tOffline mode/listen to cached songs"],
@@ -35,26 +36,31 @@ function offline() {
}
function lookup(query) {
http.get(link(query), function (res) {
var b = ""; res.on("data", function (data) {b+=data});
res.on("end", function () {
b = JSON.parse(b);for (i = 0; i < b.length; i++) {process.stdout.write("[".cyan + i + "] ".cyan + (b[i].SongName + " - " + b[i].ArtistName).bold + "\n"); if (i==b.length-1) process.stdout.write("\n");}
readline.question("What song do you want to play? #", function (input) {if (parseInt(input) != NaN) {
GS.Grooveshark.getStreamingUrl(b[input].SongID, function (err, streamUrl) {
var filename = "/home/" + process.env["USER"] + "/Music/" + b[input].SongName + " - " + b[input].ArtistName + ".mp3";
if (!fs.existsSync(filename)) http.get(streamUrl, function(res) {res.on("data", function (data){if (fs.existsSync(filename)) {fs.appendFileSync(filename, data);}else {fs.writeFileSync(filename, data);}});res.on("end", function () {play(filename.split("/")[filename.split("/").length - 1])});});
else play(filename);
});
}});
});
search(query, {key : fs.readFileSync('apikey').toString()}, function (err, results) {
if (err) console.warn(err);
console.log(results);
for (i = 0; i < results.length; i++) {
// process.stdout.write("[".cyan + i + "] ".cyan + (results[i].SongName + " - " + b[i].ArtistName).bold + "\n")
}
});
// http.get(link(query), function (res) {
// var b = ""; res.on("data", function (data) {b+=data});
// res.on("end", function () {
// b = JSON.parse(b);for (i = 0; i < b.length; i++) {process.stdout.write("[".cyan + i + "] ".cyan + (b[i].SongName + " - " + b[i].ArtistName).bold + "\n"); if (i==b.length-1) process.stdout.write("\n");}
// readline.question("What song do you want to play? #", function (input) {if (parseInt(input) != NaN) {
// GS.Grooveshark.getStreamingUrl(b[input].SongID, function (err, streamUrl) {
// var filename = "/home/" + process.env["USER"] + "/Music/" + b[input].SongName + " - " + b[input].ArtistName + ".mp3";
// if (!fs.existsSync(filename)) http.get(streamUrl, function(res) {res.on("data", function (data){if (fs.existsSync(filename)) {fs.appendFileSync(filename, data);}else {fs.writeFileSync(filename, data);}});res.on("end", function () {play(filename.split("/")[filename.split("/").length - 1])});});
// else play(filename);
// });
// }});
// });
// });
}
function play(file) {
var player = mplayer("mplayer", ["-ao","alsa", "/home/" + process.env["USER"] + "/Music/" + file]); var isfiltered = false;
var player = mplayer("mplayer", ["-ao","alsa", "/home/" + process.env["USER"] + "/Music/" + file]); var isfiltered = false;
player.stdout.on("data", function (data) { if (data.toString().substr(0,2) == "A:" && !isfiltered) { player.stdout.pipe(process.stdout); isfiltered = true;}});
process.stdin.pipe(player.stdin);
process.stdin.pipe(player.stdin);
player.on("error", function (data) {process.stdout.write("There was an error playing your song, maybe you need to install mplayer?\n");process.exit(0);});
}
function link(query) {return "http://tinysong.com/s/" + query + "?format=json&limit=20&key=0131065fac026c65c87e3658dfa66b88";};

0 comments on commit 3a97321

Please sign in to comment.