Created
December 18, 2014 19:21
-
-
Save pherris/dd6597db1e370b477673 to your computer and use it in GitHub Desktop.
Node parse HTML file and publish to Flowdock
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 htmlparser = require("htmlparser"); | |
var fs = require('fs'); | |
var sys = require("sys"); | |
var _ = require('lodash'); | |
var prettyjson = require('prettyjson'); | |
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var requestify = require('requestify'); | |
var app = express(); | |
app.use(bodyParser.text({ limit: '10mb', type: '*/x-www-form-urlencoded' })); | |
var handler = new htmlparser.DefaultHandler(function (error, dom) { | |
if (error) { | |
console.log(error); | |
} else { | |
//all good | |
} | |
}); | |
var parser = new htmlparser.Parser(handler); | |
var publish = { | |
flowdock: function(text) { | |
console.log('post', text); | |
var trimmedText = text.substr(1, text.length).substr(0, text.length -2); //trim off the leading { and traling } | |
requestify.post('https://api.flowdock.com/v1/messages/chat/c14d50f98d17fb7687f190c226419102', { //TFP back chanel | |
content: text, | |
external_user_name: 'jenkinsci' | |
}) | |
.then(function(response) { | |
// Get the response body (JSON parsed or jQuery object for XMLs) | |
//console.log(response.getBody()); | |
console.log('sent to flowdock'); | |
}).catch(function (error) { | |
// Handle any error from all above steps | |
console.log(error); | |
}); | |
} | |
}; | |
// handle POST, body is HTML file | |
app.post('/:build/:almWarVersion', function(req, res) { | |
parser.parseComplete(req.body); | |
var ar = _.find(_.find(_.find(handler.dom, { 'raw': 'html' }).children, { 'raw': 'body' }).children[0].children, { 'raw': "script type=\"text/javascript\"" }); | |
//evil | |
eval(ar.children[0].raw.substr(0, ar.children[0].raw.indexOf("}]}]');")+7)); //creates summaries var | |
var jsonObject = {}; | |
for (metric in summaries) { | |
var summary = summaries[metric], header, columns; | |
jsonObject[summary.name] = { | |
description: summary.text, | |
count: 0 | |
// , | |
// changes: [] | |
}; | |
for (changeIndex in summary.table) { | |
var change = {}; | |
for (index in summary.table[changeIndex]) { | |
change[summary.columns[index].title] = summary.table[changeIndex][index]; | |
} | |
// jsonObject[summary.name].changes.push(change); | |
jsonObject[summary.name].count++; | |
} | |
} | |
var toReport = JSON.stringify(jsonObject, undefined, 4); | |
toReport = 'ALM CSRT METRICS \n' + | |
(req.params.build ? 'Report: http://almci/job/gui-perf/job/alm-gui-perf-stats/' + req.params.build + '/artifact/csrt-report.html\n' : '' ) + | |
toReport; | |
publish.flowdock(toReport); | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.end(toReport); | |
}); | |
//server listen | |
port = 3000; | |
var server = app.listen(port, function () { | |
var host = server.address().address | |
var port = server.address().port | |
console.log('Example app listening at http://%s:%s', host, port) | |
}); | |
console.log('Listening at http://localhost:' + port) | |
// fs.readFile('csrt-report.html', {encoding: 'utf-8'}, function(err, data) { | |
// if (err) { | |
// return console.log('error: ', err); | |
// } | |
// parser.parseComplete(data); | |
// var ar = _.find(_.find(_.find(handler.dom, { 'raw': 'html' }).children, { 'raw': 'body' }).children[0].children, { 'raw': "script type=\"text/javascript\"" }); | |
// //evil | |
// eval(ar.children[0].raw.substr(0, ar.children[0].raw.indexOf("}]}]');")+7)); //creates summaries var | |
// var jsonObject = {}; | |
// for (metric in summaries) { | |
// var summary = summaries[metric], header, columns; | |
// jsonObject[summary.name] = { | |
// description: summary.text, | |
// count: 0 | |
// // , | |
// // changes: [] | |
// }; | |
// for (changeIndex in summary.table) { | |
// var change = {}; | |
// for (index in summary.table[changeIndex]) { | |
// change[summary.columns[index].title] = summary.table[changeIndex][index]; | |
// } | |
// // jsonObject[summary.name].changes.push(change); | |
// jsonObject[summary.name].count++; | |
// } | |
// } | |
// console.log(prettyjson.render(jsonObject)); | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment