Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var app = require('express')();
  2. var server = require('http').Server(app);
  3. var io = require('socket.io')(server);
  4. var SC = require('soundcloud-nodejs-api-wrapper');
  5.  
  6. /***********************************************************************************************************************
  7.  * Start the server
  8.  **********************************************************************************************************************/
  9. server.listen(process.env.PORT || 5000);
  10.  
  11. app.get('/', function (req, res) {
  12.     res.sendFile(__dirname + '/index.html');
  13. });
  14.  
  15. /***********************************************************************************************************************
  16.  * Init soundcloud wrapper and login to my account
  17.  **********************************************************************************************************************/
  18. //Stuffs to store song data into
  19. var index = 0;
  20. var currentSong = 0;
  21. var song = {
  22.     title: [],
  23.     artist: [],
  24.     id: [],
  25.     duration: [],
  26.     url: []
  27. };
  28.  
  29. //TOP SECRT PLS NO STEAL
  30. var credentials = {
  31.     client_id : 'ID',
  32.     client_secret : 'SECRET',
  33.     username : 'amedeekirk@yahoo.com',
  34.     password : 'PASSWORD'
  35. };
  36.  
  37. //provide the api with top secrt stuffs (pls no steal)
  38. var sc = new SC(credentials);
  39. var client = sc.client();
  40.  
  41. //Where the magic happens...
  42. client.exchange_token(function(err, result) {
  43.     //Get the access token
  44.     var access_token = arguments[3].access_token;
  45.     console.log('Full API auth response was:');
  46.     console.log(arguments);
  47.  
  48.     //Creates a new client object using access token we got
  49.     var clientnew = sc.client({access_token : access_token});
  50.  
  51.     //feed the client object my playlist and store info on each song in the list
  52.     clientnew.get('/playlists/326613197', null, function(err, result) {
  53.         if (err) console.error(err);
  54.  
  55.         result.tracks.forEach(function (track) {
  56.             song.id[index] = track.id;
  57.             song.title[index] = track.title;
  58.             song.artist[index] = track.user.username;
  59.             song.duration[index] = track.duration;
  60.             song.url[index] = track.permalink_url;
  61.             index++;
  62.         });
  63.  
  64.         newSong();
  65.  
  66.         //Choose next random track & send info to all clients
  67.         function newSong() {
  68.             currentSong = Math.floor(Math.random() * (song.id.length + 1));
  69.             var startTime = Date.now();
  70.             //Emits to all connected clients
  71.             socket.broadcast.emit(currentSongDetails(startTime))
  72.         }
  73.  
  74.         //Current song id and timestamp for client
  75.         function currentSongDetails(startTime) {
  76.             var songInfo = '{' +
  77.                 '"title":' + song.title[currentSong] + ',' +
  78.                 '"artist":' + song.artist[currentSong] +',' +
  79.                 '"id":' + song.id[currentSong] +',' +
  80.                 '"timeElapsed":' + timeElapsed(startTime) +',' +
  81.                 '"url":' + song.url[currentSong] +
  82.                 '}';
  83.             console.log(songInfo);
  84.             return songInfoObj = JSON.parse(songInfo);
  85.         }
  86.  
  87.         //Gets the time elapsed since the start of the song
  88.         function timeElapsed(startTime) {
  89.              return Date.now() - startTime;
  90.         }
  91.     });
  92. });
  93.  
  94. /***********************************************************************************************************************
  95.  * Init Socket.io connections
  96.  **********************************************************************************************************************/
  97. //Run when user connects
  98. io.on('connection', function (socket) {
  99.     console.log("User Connected");
  100.     socket.emit('initPosition', currentSongDetails());
  101.     socket.on('confirmed', function (data) {
  102.         console.log(data);
  103.     });
  104.  
  105.     //Lets us know when a user disconnects
  106.     socket.on('disconnect', function () {
  107.         io.emit('user disconnected');
  108.         console.log("User disconnected")
  109.     });
  110.  
  111. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement