0

I am using an API for a Twitch.tv streaming bot called DeepBot.

Here is the link to it on github https://github.com/DeepBot-API/client-websocket

My goal is to create a text document listing all the information pulled from the bot using the command api|get_users|. The bot's response is always a json object. How can I take the json object from the bot and save it as a text file?

Edit: My code

var WebSocket = require('ws');
var ws = new WebSocket('ws://Ip and Port/');
ws.on('open', function () {
    console.log('sending API registration');
    ws.send('api|register|SECRET');
});

    ws.on('close', function close() {
    console.log('disconnected');
});
ws.on('message', function (message) {
    console.log('Received: ' + message);
    });

ws.on('open', function () {
    ws.send('api|get_users|');
});  
Shane Parrott
  • 17
  • 1
  • 4
  • 2
    Only with use of server-side languages. What have you googled, tried? – nicael Dec 19 '15 at 21:08
  • @nicael why only php (and not c# / java / python, etc) – Eric Dec 19 '15 at 21:09
  • @radpin made an edit, just PHP has sticked to my mind in this case. – nicael Dec 19 '15 at 21:09
  • 2
    Please put **some** information here, as well as what you have tried. A JSON is already a string object, in theory depending on the technology you are using PowerShell would be more than enough with invoke-webrequest -uri "$site" | out-file ".\Result.txt" BUT the current lack of effort or information in the question makes this a poor question.. – Austin T French Dec 19 '15 at 21:17
  • If you want to do this in JavaScript you should use a server-side implementation like Node.js. – kYuZz Dec 19 '15 at 21:17
  • @AustinFrench I have not tried anything that was practical or made sense to me. I will add the code I have used so far. – Shane Parrott Dec 19 '15 at 23:40

2 Answers2

1

Well that depends on how your setup is? You posted this under javascript. So I guess you are either:

  • using a browser, to make the websocket connection, in with case there is no direct way to save a file on the client. But in HTML5 you can store key,value pairs with local storage.
  • using node js (server side javascript) in witch case the code is as below:
  • some other setup, that I can't guess. in witch case you might tell a little more about it?

In browser with HTML5 capabilities:

// where msg is an object returned from the API
localStorage.setItem('Some key', JSON.stringify(msg));

In Node JS

var fs = require("fs"); // Has to be installed first with “npm install fs”

// where msg is an object returned from the API
fs.writeFile("some-file.json", JSON.stringify(msg), function (err) {
  if (err) throw err;
});

Edit: OK, Thanks for clearing it up. I believe Blag's solution is the way to go.

Good luck with your project!

Simon Rigét
  • 2,757
  • 5
  • 30
  • 33
0

If it's for a client side JS save :

Create a file in memory for user to download, not through server

and

Convert JS object to JSON string

Is what you need. ( I don't test it, but it'll look like this : )

var j = {"name":"binchen"};
var s = JSON.stringify(j); 
window.location = 'data:text/plain;charset=utf-8,'+encodeURIComponent(s);
Community
  • 1
  • 1
Blag
  • 5,818
  • 2
  • 22
  • 45