Skip to content

Instantly share code, notes, and snippets.

@lifehome

lifehome/Probability-v4.js

Last active Oct 23, 2016
Embed
What would you like to do?
ES2015 written script, use with caution.
/* NOTICE: This file is written in ES2015.
* Please use the --harmony flag for nodejs environment.
*
* License: CC BY-SA 4.0 by Ivan Ip<m@lifeho.me>
*/
var iconv = require('iconv-lite'),
request = require('request'),
cheerio = require('cheerio'),
cheerioTableparser = require('cheerio-tableparser'),
selectr = '#containercontent > table:nth-child(15) > tbody > tr > td > table:nth-child(3) > tbody > tr:nth-child(22) > td > table:nth-child(1)',
probability = {};
module.exports = function(callback){
request({url:'http://www.hkww.org/weather/latesttc.html', encoding:null}, function (error, response, html) {
if (!error && response.statusCode == 200) {
var html = iconv.decode(html, "big5");
if(html.indexOf("There are currently no active warning bulletins.") > 0)
return callback({ info: 'No active warning bulletins.'});
var $ = cheerio.load(html);
// Let TableParser parse the table
cheerioTableparser($);
var rawdata = $(selectr).parsetable(true, true, true);
var data = Object.values(rawdata);
var title = rawdata[0][0];
var probability = {
'standBy_signal': {
// tau = +6
'+6-inforce': data[1][3],
'+6-cancel': data[1][4],
// tau = +12
'+12-inforce': data[2][3],
'+12-cancel': data[2][4],
// tau = +18
'+18-inforce': data[3][3],
'+18-cancel': data[3][4],
// tau = +24
'+24-inforce': data[4][3],
'+24-cancel': data[4][4],
// tau = +30
'+30-inforce': data[5][3],
'+30-cancel': data[5][4],
// tau = +36
'+36-inforce': data[6][3],
'+36-cancel': data[6][4],
// tau = +42
'+42-inforce': data[7][3],
'+42-cancel': data[7][4],
// tau = +48
'+48-inforce': data[8][3],
'+48-cancel': data[8][4]
},
'strongWind_signal': {
// tau = +6
'+6-inforce': data[1][6],
'+6-cancel': data[1][7],
// tau = +12
'+12-inforce': data[2][6],
'+12-cancel': data[2][7],
// tau = +18
'+18-inforce': data[3][6],
'+18-cancel': data[3][7],
// tau = +24
'+24-inforce': data[4][6],
'+24-cancel': data[4][7],
// tau = +30
'+30-inforce': data[5][6],
'+30-cancel': data[5][7],
// tau = +36
'+36-inforce': data[6][6],
'+36-cancel': data[6][7],
// tau = +42
'+42-inforce': data[7][6],
'+42-cancel': data[7][7],
// tau = +48
'+48-inforce': data[8][6],
'+48-cancel': data[8][7]
},
'Gale-or-Storm_signal': {
// tau = +6
'+6-inforce': data[1][9],
'+6-cancel': data[1][10],
// tau = +12
'+12-inforce': data[2][9],
'+12-cancel': data[2][10],
// tau = +18
'+18-inforce': data[3][9],
'+18-cancel': data[3][10],
// tau = +24
'+24-inforce': data[4][9],
'+24-cancel': data[4][10],
// tau = +30
'+30-inforce': data[5][9],
'+30-cancel': data[5][10],
// tau = +36
'+36-inforce': data[6][9],
'+36-cancel': data[6][10],
// tau = +42
'+42-inforce': data[7][9],
'+42-cancel': data[7][10],
// tau = +48
'+48-inforce': data[8][9],
'+48-cancel': data[8][10]
}
};
var info = title.concat(probability);
return callback(info);
} return callback({ info: 'No statistic data has been found.' });
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.