atomjack / dubtrackapi

An API for creating bots on Dubtrack.fm
22 stars 5 forks source link

How to request data? #4

Open ImGrumpy opened 8 years ago

ImGrumpy commented 8 years ago

I've got this working pretty well with the events that dubtrack is emitting... but is there a way to make requests, like getUser or something? I see them documented in the back-end API, should I just try with that or is there an easier way?

ImGrumpy commented 8 years ago

Well, nvm... I wrote https.get routine to query the API directly. Sorry, I was impatient. LOL

atomjack commented 8 years ago

Nice. I will probably implement something like that into the API soon, just been very busy lately.

jtrutwin-zz commented 8 years ago

@ImGrumpy c are to share some of your code please? :)

ImGrumpy commented 8 years ago

Sure, but I'm no expert coder, so if there are obvious dumb things here forgive me... but it works.

getUser routine:

function getUser(uid,callback) {
    var sresp = '';
    https.get('https://api.dubtrack.fm/user/' + uid, function (body) {
        body.on('data', function (chunk) {
            sresp += chunk;
        });
        body.on('end', function () {
            sresp = JSON.parse(sresp);
            callback(sresp.data);
        });
    });
}

Called as follows:

getUser(uid, function(data){
    console.log(JSON.stringify(data,null,4));
});
jtrutwin-zz commented 8 years ago

Thanks - I still can't get my bot to connect in the room so I'm not even able to do this... Not sure why I'm having so many issues....

jtrutwin-zz commented 8 years ago

@ImGrumpy did you figure out how to have the bot chat yet?

ImGrumpy commented 8 years ago

Yes, I have it functioning pretty well actually. To get it to connect and start speaking really wasn't that much of a change from the old PlugBotAPI. Most important change was the creds since it's now browser cookie based rather than using actual login creds.

jtrutwin-zz commented 8 years ago

Strange, I'm trying:

bot.on('ready', function(data) {
  console.log("Ready to go: ", data);
  // data contains the currentDJ (by name) and currentTrack (artist and track), and the list of users in the room (does not update on join/depart)

  bot.getEvents(function(events) {
    console.log("These are the Dubtrack events I respond to: ", events);
  });

  bot.chat("hello");
});

But I never see the "hello" in chat - did you do anything differently?

Thanks, sorry to be a pest...

atomjack commented 8 years ago

That should work, @jtrutwin. Maybe try wrapping the chat call in a setTimeout to give it a few seconds of a delay before it tries to chat? My bot in the Chillout Mixer is able to chat just fine. It's possible that the ready event fires before everything is truly ready.

atomjack commented 8 years ago

Also, @ImGrumpy, for the getUser call, feel free to fork this repository and add it yourself, and then do a pull request. All you really need to do is do prototype.DubtrackAPI.getUser = function(uid, callback) {, and then the rest of your code. Just make sure to doublecheck the value of callback, with if(typeof callback === 'function') before executing it. Then, you can call bot.getUser(uid, function(data) {....

ImGrumpy commented 8 years ago

@atomjack Embarrassingly enough, I have no idea what any of that forking and pulling means, sorry. I've created bots on TT.fm, Plug.dj and now Dubtrack.fm as a way of teaching myself Javascript, but I haven't actually been able to grasp how Github works yet... :frowning: Actually, that was the first time I'd written a callback function...

jtrutwin-zz commented 8 years ago

@atomjack hrm, tried a setTimeout with 10000 - still nothing.

Also tried this:

bot.on('chat-message', function(data) {
  console.log("got chat: ", data);

  if (data.message == ".ping") {
    bot.chat("pong!");
    console.log("pong!");
  }
});

Still nothing, though it shows in the console log. I'll keep digging...

atomjack commented 8 years ago

Hehe understood. Well, now's a great time to learn. What you can do is click the fork button on the main page of this API, and that will copy the repository over into your account. Then, do a git clone on that repository (replace mine that you pulled down). Then you can make your changes to the code, and when you are happy with them, you can use git to add the changes and push them up to your repository. Then you can issue a pull request (when you view your repository after you have pushed changes up, there should be a place to do a pull request). That will notify me, and I can pull your changes down to see if they are ok, and then easily merge them into mine. Read the docs on git and doing pull requests in github :)

jtrutwin-zz commented 8 years ago

@atomjack

I added debugs to DubtrackAPI.prototype.chat:

    DubtrackAPI.prototype.chat = function(msg) {
console.log("chat", msg);
      this.page.evaluate(function(msg) {
console.log("evalutate", msg);
        Dubtrack.room.chat._messageInputEl.val(msg);
        Dubtrack.room.chat.sendMessage();
      }, function() {
console.log("function", msg);

      }, msg);
    };

any time I call chat, it does not show the "evaluate" debug, it always shows the "function" debug tho - any ideas why?

chat new song!
function new song!

Thanks,

Josh

atomjack commented 8 years ago

Yeah, the "evaluate" console log is happening inside the virtual browser. It needs to be passed out into the api itself. You can do this in two different ways - either return some string or object from the virtual browser out into the api, like so:

    DubtrackAPI.prototype.chat = function(msg) {
console.log("chat", msg);
      this.page.evaluate(function(msg) {
console.log("evalutate", msg);
        Dubtrack.room.chat._messageInputEl.val(msg);
        Dubtrack.room.chat.sendMessage();
        return {
              msg: msg
        };
      }, function(data) {
console.log("function", msg);
console.log("from inside virtual browser: ", data);
      }, msg);
    };

I also created a function called debug that lives inside the virtual browser, during initialization, that sends specifically formatted text into the virtual browser's console. If you look for the part where the API is monitoring the virtual browser's console, you'll see how the API will then emit a debug event when it receives the correct text in a console message:

// this function lives inside the virtual browser, but it's probably not available anywhere but the block it's defined in
function debug(msg) {
                  console.log("DubtrackAPI: " + JSON.stringify({
                    event: 'debug',
                    data: msg
                  }));
                }

// this part is in the API itself, and catches the debug event (and all other events) that the virtual browser threw:
            page.set('onConsoleMessage', function(msg) {
              if(!msg.match(/^The page at/)) {
                //console.log("console message: ", msg);
              }
              var re = new RegExp("^DubtrackAPI: (.+)");
              if(msg.match(re)) {
                var obj = JSON.parse(RegExp.$1);
                self.emit(obj.event, obj.data);
              }
            });

Basically, the virtual browser is outtputing stuff beginning with "DubtrackAPI: ", followed by a string representation of a JSON object. The API then parses that string representation, and then emits its event. You could replicate the debug function from within the virtual browser just by doing

console.log("DubtrackAPI: " + JSON.stringify({
event: 'debug',
data: {
someKey: someValue
}
});