plugged is a (v|f)ast JavaScript API for the plug.dj service.
npm install plugged
plugged is relatively easy to use. Most functions are exposed via events, thus it's easy to check for certain data.
To start with a simple bot, do this:
var Plugged = require("plugged");
var plugged = new Plugged();
// log into the service
plugged.login({ email: "[email protected]", password: "examplepassword" });
plugged.on(plugged.LOGIN_SUCCESS, function _loginSuccess() {
plugged.cacheChat(true);
plugged.connect("exampleroom");
});
plugged.on(plugged.JOINED_ROOM, function _joinedRoom() {
plugged.on(plugged.ADVANCE, function() {
//WOOT!
plugged.woot();
});
});
Most functionality is exposed via Events. The wiki describes how to use what events when.
Sometimes you need to call data from the server, for example if you want to get your current playlist, add a new media file or get a certain list of rooms based on a search string. All Server calls are described in the wiki
Some people might prefer taking the oauth route and use their fb login for plug. So this is possible since [email protected] as well. All you have to do is to replace the login object with this one:
...
plugged.login({
userID: "your ID here",
accessToken: "your access token here"
});
...
to keep the behaivour clear, if you enter both email and facebook login credentials plug will return an "malformed credentials" error.
To save some time you can restart your application without going through the whole login procedure. All you have to do is to save the cookie jar and the auth token and return them to plugged once you start your application again.
getting the necessary data:
plugged.getAuthToken(function (err, token) {
// save the token
});
plugged.getJar();
putting it back in:
var token = null;
var jar = null;
// read token and jar from DB, file, etc.
plugged.setJar(jar);
// the token has a higher priority
plugged.login({ ... }, token);
How you save it and what you want to do with it is up to you. There's a multitude of ways to save this and it's probably better that you do that since you know best how your application should behave and under which conditions like os, environment, etc.
Remember, both, the facebook token and the auth token are not meant forever. So you should keep this in mind while developing your application.