Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var app = require('express')();
- var server = require('http').Server(app);
- var io = require('socket.io')(server);
- var SC = require('soundcloud-nodejs-api-wrapper');
- /***********************************************************************************************************************
- * Start the server
- **********************************************************************************************************************/
- server.listen(process.env.PORT || 5000);
- app.get('/', function (req, res) {
- res.sendFile(__dirname + '/index.html');
- });
- /***********************************************************************************************************************
- * Init soundcloud wrapper and login to my account
- **********************************************************************************************************************/
- //Stuffs to store song data into
- var index = 0;
- var currentSong = 0;
- var song = {
- title: [],
- artist: [],
- id: [],
- duration: [],
- url: []
- };
- //TOP SECRT PLS NO STEAL
- var credentials = {
- client_id : 'ID',
- client_secret : 'SECRET',
- username : 'amedeekirk@yahoo.com',
- password : 'PASSWORD'
- };
- //provide the api with top secrt stuffs (pls no steal)
- var sc = new SC(credentials);
- var client = sc.client();
- //Where the magic happens...
- client.exchange_token(function(err, result) {
- //Get the access token
- var access_token = arguments[3].access_token;
- console.log('Full API auth response was:');
- console.log(arguments);
- //Creates a new client object using access token we got
- var clientnew = sc.client({access_token : access_token});
- //feed the client object my playlist and store info on each song in the list
- clientnew.get('/playlists/326613197', null, function(err, result) {
- if (err) console.error(err);
- result.tracks.forEach(function (track) {
- song.id[index] = track.id;
- song.title[index] = track.title;
- song.artist[index] = track.user.username;
- song.duration[index] = track.duration;
- song.url[index] = track.permalink_url;
- index++;
- });
- newSong();
- //Choose next random track & send info to all clients
- function newSong() {
- currentSong = Math.floor(Math.random() * (song.id.length + 1));
- var startTime = Date.now();
- //Emits to all connected clients
- socket.broadcast.emit(currentSongDetails(startTime))
- }
- //Current song id and timestamp for client
- function currentSongDetails(startTime) {
- var songInfo = '{' +
- '"title":' + song.title[currentSong] + ',' +
- '"artist":' + song.artist[currentSong] +',' +
- '"id":' + song.id[currentSong] +',' +
- '"timeElapsed":' + timeElapsed(startTime) +',' +
- '"url":' + song.url[currentSong] +
- '}';
- console.log(songInfo);
- return songInfoObj = JSON.parse(songInfo);
- }
- //Gets the time elapsed since the start of the song
- function timeElapsed(startTime) {
- return Date.now() - startTime;
- }
- });
- });
- /***********************************************************************************************************************
- * Init Socket.io connections
- **********************************************************************************************************************/
- //Run when user connects
- io.on('connection', function (socket) {
- console.log("User Connected");
- socket.emit('initPosition', currentSongDetails());
- socket.on('confirmed', function (data) {
- console.log(data);
- });
- //Lets us know when a user disconnects
- socket.on('disconnect', function () {
- io.emit('user disconnected');
- console.log("User disconnected")
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement