alaingilbert / Turntable-API

Allows you to create bots for turntable.fm
http://alaingilbert.github.com/Turntable-API/
MIT License
317 stars 97 forks source link

Getting One Piece Of Data From JSON List #212

Closed mnafricano closed 11 years ago

mnafricano commented 11 years ago

Okay, so, I'm not too good with Node JS, I know JavaScript, but only to a certain extent. With that said, I was having trouble creating a bot that scans through the data returned with directoryRooms();. Can someone tell me what's wrong with the code I have here. Thank you for your help, it's much appreciated.

bot.on('ready', function (data) {
    bot.directoryRooms(function (data) {
        console.log('Get a directory of rooms.');
        console.log('------------------------------------');
        console.log(JSON.stringify(data, null, ' '));
        if (data.rooms.listeners > 5) {
            bot.roomDeregister();
            setTimeout(function() {
                bot.roomRegister(data.rooms.roomid);
            }, 3E3);
        }
    });
});
alaingilbert commented 11 years ago

directoryRooms returns an array of rooms. So you have to make a loop that goes through them. And data.rooms.listeners does not exists. You have to choose one room, and then check the listeners. ex: data.rooms[7].listeners

mnafricano commented 11 years ago

Oh, thank you! That data.rooms[7].listeners example completely answered my question. I think I figured out how to code this now. Could you tell me if this :arrow_down: looks right?

var Bot = require('ttapi');
var AUTH = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var USERID = '51a8cba0aaa5cd629554d9dd';
var ROOMID = '51a8caf1aaa5cd629554d9d4';
var bot = new Bot(AUTH, USERID, ROOMID);

// Runtime Variables
var roomNumber = 1;

bot.on('ready', function (data) {
    bot.directoryRooms(function (data) {
        console.log('Get a directory of rooms.');
        console.log('------------------------------------');
        console.log(JSON.stringify(data, null, ' '));
        if (data.rooms[roomNumber].listeners > 5) {
            bot.roomDeregister();
            setTimeout(function() {
                bot.roomRegister(data.rooms[roomNumber].roomid);
            }, 3E3);
        } else {
            roomNumber++;
        }
    });
});
alaingilbert commented 11 years ago

No you have to run a loop like I did here : https://github.com/alaingilbert/Turntable-API/issues/206#issuecomment-18465970

Cause right now, the ready event is triggered (once) execute the directoryRooms... increment the roomNumber, but it will end there, because the ready event is not going to be triggered again.

mnafricano commented 11 years ago

Thank you, once again, haha. @alaingilbert