Last active
August 29, 2015 14:14
-
-
Save melvincarvalho/b7a7087e0f7146113fb6 to your computer and use it in GitHub Desktop.
log gitter to file and ldp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var https = require('https'); | |
var fs = require('fs'); | |
var roomId = process.env.ROOM_ID; | |
var token = process.env.TOKEN; | |
var heartbeat = " \n"; | |
console.log('logging room : ' + roomId); | |
var gitter = { | |
hostname: 'stream.gitter.im', | |
port: 443, | |
path: '/v1/rooms/' + roomId + '/chatMessages', | |
method: 'GET', | |
headers: {'Authorization': 'Bearer ' + token} | |
}; | |
var ldp = { | |
hostname: 'gitter.rww.io', | |
port: 443, | |
method: 'PUT', | |
headers: {'Content-Type': 'text/turtle'} | |
}; | |
var req = https.request(gitter, function(res) { | |
res.on('data', function(chunk) { | |
var msg = chunk.toString(); | |
if (msg !== heartbeat) { | |
console.log('Message: ' + msg); | |
var message = JSON.parse(msg); | |
// create dir | |
var datadir = './log/'; | |
var today = new Date().toISOString().substring(0,10); | |
if (!fs.existsSync(datadir)){ | |
console.log('Creating dir: ' + datadir); | |
fs.mkdirSync(datadir); | |
} | |
datadir += roomId + '/'; | |
if (!fs.existsSync(datadir)){ | |
console.log('Creating dir: ' + datadir); | |
fs.mkdirSync(datadir); | |
} | |
datadir += today + '/' ; | |
if (!fs.existsSync(datadir)){ | |
console.log('Creating dir: ' + datadir); | |
fs.mkdirSync(datadir); | |
} | |
// create turtle | |
var turtle = '<' + message['id'] + '#this> '; | |
turtle += '<https://schema.rww.io/gitter#id> "'+ message['id'] +'" ; '; | |
turtle += '<https://schema.rww.io/gitter#text> """'+ message['text'] +'""" ; '; | |
turtle += '<https://schema.rww.io/gitter#username> "'+ message['fromUser']['username'] +'" ; '; | |
turtle += '<https://schema.rww.io/gitter#sent> "'+ message['sent'] +'" . '; | |
console.log(turtle); | |
// write file | |
var out = datadir + message['id'] + '.json'; | |
console.log('Writing to: ' + out); | |
fs.writeFileSync(out, msg); | |
out = datadir + message['id']; | |
console.log('Writing to: ' + out); | |
fs.writeFileSync(out, turtle); | |
// put file to ldp | |
ldp.path = "/log/" + roomId + '/' + today + '/' + message['id']; | |
var put = https.request(ldp, function(res) { | |
console.log('STATUS: ' + res.statusCode); | |
console.log('HEADERS: ' + JSON.stringify(res.headers)); | |
res.on('data', function (chunk) { | |
console.log('BODY: ' + chunk); | |
}); | |
}); | |
put.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
put.write(turtle); | |
put.end(); | |
} | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log('Something went wrong: ' + e.message); | |
}); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment