From 85136e47b91d1d8b1936b737f5fc0783153063c9 Mon Sep 17 00:00:00 2001 From: William Bruno Date: Tue, 10 Feb 2015 00:31:01 -0200 Subject: [PATCH 1/6] Refatored to Sabesp.js --- .gitignore | 3 +- index.js | 91 +++------------------------------------------------ lib/Sabesp.js | 78 +++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- 4 files changed, 87 insertions(+), 88 deletions(-) create mode 100644 lib/Sabesp.js diff --git a/.gitignore b/.gitignore index b512c09..93f1361 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +npm-debug.log diff --git a/index.js b/index.js index 454dc54..f06deb8 100644 --- a/index.js +++ b/index.js @@ -1,97 +1,16 @@ (function () { 'use strict'; var express = require('express'), - request = require('request'), - cheerio = require('cheerio'), - app = express(); + app = express(), + Sabesp = require('./lib/Sabesp'); // Heroku port settings app.set('port', (process.env.PORT || 8080)); app.use(express.static(__dirname)); app.get('/', function (req, res) { - - // Our Sabesp url - var url = 'http://www2.sabesp.com.br/mananciais/DivulgacaoSiteSabesp.aspx'; - - // TODO: Improve this mess - function getSistemaName(name) { - name = name.split(/[./]+/)[1]; - - switch (name) { - case 'sistemaCantareira': - return 'Cantareira'; - case 'sistemaAltoTiete': - return 'Alto Tietê'; - case 'sistemaGuarapiranga': - return 'Guarapiranga'; - case 'sistemaCotia': - return 'Alto Cotia'; - case 'sistemaRioGrande': - return 'Rio Grande'; - case 'sistemaRioClaro': - return 'Rio Claro'; - } - } - - request(url, function (error, response, html) { - if (!error) { - var $ = cheerio.load(html); - - var json = []; - - $('#tabDados').filter(function () { - - var data = $(this); - - /*=========================== - * TODO: Improve this loops - ===========================*/ - - // Fetch each images on context - data.find('img').each(function (i, elem) { - json[i] = { - name : getSistemaName(elem.attribs.src), - data : [] - }; - }); - - // Fetch each td with content "volume armazenado" - data.find('td:contains(volume armazenado)').each(function (i, elem) { - json[i].data.push({ - key : 'volume armazenado', - value : $(elem).next().text() - }); - }); - - // Fetch each td with content "pluviometria do dia" - data.find('td:contains(pluviometria do dia)').each(function (i, elem) { - json[i].data.push({ - key : 'pluviometria do dia', - value : $(elem).next().text() - }); - }); - - // Fetch each td with content "pluviometria acumulada no mês" - data.find('td:contains(pluviometria acumulada no mês)').each(function (i, elem) { - json[i].data.push({ - key : 'pluviometria acumulada no mês', - value : $(elem).next().text() - }); - }); - - // Fetch each td with content "média histórica do mês" - data.find('td:contains(média histórica do mês)').each(function (i, elem) { - json[i].data.push({ - key : 'média histórica do mês', - value : $(elem).next().text() - }); - }); - - }); - - res.json(json); - } + Sabesp.fetch().then(function(resolve, reject) { + res.json(resolve); }); }); @@ -99,4 +18,4 @@ console.log('Magic happens on port: ' + app.get('port')); }); -})(); \ No newline at end of file +})(); diff --git a/lib/Sabesp.js b/lib/Sabesp.js new file mode 100644 index 0000000..6abc174 --- /dev/null +++ b/lib/Sabesp.js @@ -0,0 +1,78 @@ +(function () { + 'use strict'; + var request = require('request'), + cheerio = require('cheerio'), + Promise = require('promise'); + + // Our Sabesp url + var url = 'http://www2.sabesp.com.br/mananciais/DivulgacaoSiteSabesp.aspx'; + + var dams = { + "sistemaCantareira": "Cantareira", + "sistemaAltoTiete": "Alto Tietê", + "sistemaGuarapiranga": "Guarapiranga", + "sistemaCotia": "Alto Cotia", + "sistemaRioGrande": "Rio Grande", + "sistemaRioClaro": "Rio Claro" + }; + + function _getDamName(str) { + var name = str.split(/[./]+/)[1]; + return dams[name]; + } + + function _buildJSON(json, data, $, cssSelector, key) { + data.find(cssSelector).each(function (i, elem) { + json[i].data.push({ + key : key, + value : $(elem).next().text() + }); + }); + } + + function _parserHTML(html) { + var json = [], + $ = cheerio.load(html); + + $('#tabDados').filter(function () { + var data = $(this); + + // Fetch each images on context + data.find('img').each(function (i, elem) { + json[i] = { + name : _getDamName(elem.attribs.src), + data : [] + }; + }); + + // Fetch each td with content "volume armazenado" + _buildJSON(json, data, $, 'td:contains(volume armazenado)', 'volume armazenado'); + + // Fetch each td with content "pluviometria do dia" + _buildJSON(json, data, $, 'td:contains(pluviometria do dia)', 'pluviometria do dia'); + + // Fetch each td with content "pluviometria acumulada no mês" + _buildJSON(json, data, $, 'td:contains(pluviometria acumulada no mês)', 'pluviometria acumulada no mês'); + + // Fetch each td with content "média histórica do mês" + _buildJSON(json, data, $, 'td:contains(média histórica do mês)', 'média histórica do mês'); + }); + + return json; + } + + var Sabesp = {}; + Sabesp.fetch = function() { + return new Promise(function(resolve, reject) { + request(url, function (error, response, html) { + if (error) { + reject(error); + } else { + resolve(_parserHTML(html)); + } + }); + }); + }; + + module.exports = Sabesp; +})(); diff --git a/package.json b/package.json index edaec01..f434293 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ ], "scripts": { "start": "node index.js", - "test": "./node_modules/jshint/bin/jshint tests/*.js" + "test": "./node_modules/jshint/bin/jshint *.js lib/*.js" }, "engines": { "node": "0.10.x" @@ -20,6 +20,7 @@ "dependencies": { "cheerio": "^0.18.0", "express": "^4.11.2", + "promise": "^6.1.0", "request": "^2.53.0" }, "devDependencies": { From 65d2b2a861ead755f834ef099483b633c4405390 Mon Sep 17 00:00:00 2001 From: William Bruno Date: Tue, 10 Feb 2015 01:36:59 -0200 Subject: [PATCH 2/6] getToken to fetch by specific day --- index.js | 6 +++++ lib/Sabesp.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f06deb8..9450000 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,12 @@ }); }); + app.get('/:date', function (req, res) { + Sabesp.fetch(req.params.date).then(function(resolve, reject) { + res.json(resolve); + }); + }); + app.listen(app.get('port'), function () { console.log('Magic happens on port: ' + app.get('port')); }); diff --git a/lib/Sabesp.js b/lib/Sabesp.js index 6abc174..6c1e927 100644 --- a/lib/Sabesp.js +++ b/lib/Sabesp.js @@ -5,6 +5,8 @@ Promise = require('promise'); // Our Sabesp url + var token; + var url = 'http://www2.sabesp.com.br/mananciais/DivulgacaoSiteSabesp.aspx'; var dams = { @@ -61,18 +63,79 @@ return json; } + function _buildData(date, token) { + var date = date.split('-'); + + return { + "__VIEWSTATE": token.state, + "__EVENTVALIDATION": token.validation, + "Imagebutton1.x": 8, + "Imagebutton1.y": 6, + "cmbDia": parseInt(date[2], 10), + "cmbMes": parseInt(date[1], 10), + "cmbAno": parseInt(date[0], 10) + }; + } + function _handle(error, response, html) { + if (error) { + reject(error); + } else { + resolve(_parserHTML(html)); + } + } + + var Sabesp = {}; - Sabesp.fetch = function() { + Sabesp.fetch = function(date) { + if (date) { + return new Promise(function(resolve, reject) { + var data = _buildData(date, token); + + request({ + 'url': url, + 'method': 'POST', + 'headers': { + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*-/*;q=0.8', + 'Host': 'www2.sabesp.com.br', + 'Origin': 'http://www2.sabesp.com.br', + 'Referer': url, + 'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.114 Safari/537.36', + }, + 'jar': true, + 'form': data, + }, function(error, response, html){ + resolve(_parserHTML(html)); + } + ); + + }); + } else { + return new Promise(Sabesp.today); + } + }; + Sabesp.today = function(resolve, reject) { + request(url, _handle); + }; + Sabesp.getToken = function() { return new Promise(function(resolve, reject) { request(url, function (error, response, html) { if (error) { reject(error); } else { - resolve(_parserHTML(html)); + var $ = cheerio.load(html), + ret = { + state: $('#__VIEWSTATE').val(), + validation: $('#__EVENTVALIDATION').val() + }; + resolve(ret); } }); }); }; + Sabesp.getToken().then(function(resolve, reject) { + token = resolve; + }); + module.exports = Sabesp; })(); From 6cab9217328f6e43710dc8dd7340c3454a61de01 Mon Sep 17 00:00:00 2001 From: William Bruno Date: Tue, 10 Feb 2015 01:42:28 -0200 Subject: [PATCH 3/6] Typo --- lib/Sabesp.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/Sabesp.js b/lib/Sabesp.js index 6c1e927..e5d1a34 100644 --- a/lib/Sabesp.js +++ b/lib/Sabesp.js @@ -76,13 +76,6 @@ "cmbAno": parseInt(date[0], 10) }; } - function _handle(error, response, html) { - if (error) { - reject(error); - } else { - resolve(_parserHTML(html)); - } - } var Sabesp = {}; @@ -114,7 +107,13 @@ } }; Sabesp.today = function(resolve, reject) { - request(url, _handle); + request(url, function (error, response, html) { + if (error) { + reject(error); + } else { + resolve(_parserHTML(html)); + } + }); }; Sabesp.getToken = function() { return new Promise(function(resolve, reject) { From 5def7090a557ca71fa82718d4a57bc36f30ffeee Mon Sep 17 00:00:00 2001 From: William Bruno Date: Tue, 10 Feb 2015 01:36:59 -0200 Subject: [PATCH 4/6] getToken to fetch by specific day --- index.js | 6 +++++ lib/Sabesp.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f06deb8..9450000 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,12 @@ }); }); + app.get('/:date', function (req, res) { + Sabesp.fetch(req.params.date).then(function(resolve, reject) { + res.json(resolve); + }); + }); + app.listen(app.get('port'), function () { console.log('Magic happens on port: ' + app.get('port')); }); diff --git a/lib/Sabesp.js b/lib/Sabesp.js index 6abc174..6c1e927 100644 --- a/lib/Sabesp.js +++ b/lib/Sabesp.js @@ -5,6 +5,8 @@ Promise = require('promise'); // Our Sabesp url + var token; + var url = 'http://www2.sabesp.com.br/mananciais/DivulgacaoSiteSabesp.aspx'; var dams = { @@ -61,18 +63,79 @@ return json; } + function _buildData(date, token) { + var date = date.split('-'); + + return { + "__VIEWSTATE": token.state, + "__EVENTVALIDATION": token.validation, + "Imagebutton1.x": 8, + "Imagebutton1.y": 6, + "cmbDia": parseInt(date[2], 10), + "cmbMes": parseInt(date[1], 10), + "cmbAno": parseInt(date[0], 10) + }; + } + function _handle(error, response, html) { + if (error) { + reject(error); + } else { + resolve(_parserHTML(html)); + } + } + + var Sabesp = {}; - Sabesp.fetch = function() { + Sabesp.fetch = function(date) { + if (date) { + return new Promise(function(resolve, reject) { + var data = _buildData(date, token); + + request({ + 'url': url, + 'method': 'POST', + 'headers': { + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*-/*;q=0.8', + 'Host': 'www2.sabesp.com.br', + 'Origin': 'http://www2.sabesp.com.br', + 'Referer': url, + 'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.114 Safari/537.36', + }, + 'jar': true, + 'form': data, + }, function(error, response, html){ + resolve(_parserHTML(html)); + } + ); + + }); + } else { + return new Promise(Sabesp.today); + } + }; + Sabesp.today = function(resolve, reject) { + request(url, _handle); + }; + Sabesp.getToken = function() { return new Promise(function(resolve, reject) { request(url, function (error, response, html) { if (error) { reject(error); } else { - resolve(_parserHTML(html)); + var $ = cheerio.load(html), + ret = { + state: $('#__VIEWSTATE').val(), + validation: $('#__EVENTVALIDATION').val() + }; + resolve(ret); } }); }); }; + Sabesp.getToken().then(function(resolve, reject) { + token = resolve; + }); + module.exports = Sabesp; })(); From 9808a911aef5922a73a747a9d8f61733b26d8acd Mon Sep 17 00:00:00 2001 From: William Bruno Date: Tue, 10 Feb 2015 01:42:28 -0200 Subject: [PATCH 5/6] Typo --- lib/Sabesp.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/Sabesp.js b/lib/Sabesp.js index 6c1e927..e5d1a34 100644 --- a/lib/Sabesp.js +++ b/lib/Sabesp.js @@ -76,13 +76,6 @@ "cmbAno": parseInt(date[0], 10) }; } - function _handle(error, response, html) { - if (error) { - reject(error); - } else { - resolve(_parserHTML(html)); - } - } var Sabesp = {}; @@ -114,7 +107,13 @@ } }; Sabesp.today = function(resolve, reject) { - request(url, _handle); + request(url, function (error, response, html) { + if (error) { + reject(error); + } else { + resolve(_parserHTML(html)); + } + }); }; Sabesp.getToken = function() { return new Promise(function(resolve, reject) { From ee492dc0223bfb6ba752d023e85b6f22cbfd3ee7 Mon Sep 17 00:00:00 2001 From: William Bruno Date: Tue, 10 Feb 2015 01:47:08 -0200 Subject: [PATCH 6/6] Fix jshint --- lib/Sabesp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Sabesp.js b/lib/Sabesp.js index e5d1a34..f2b3a4a 100644 --- a/lib/Sabesp.js +++ b/lib/Sabesp.js @@ -64,7 +64,7 @@ } function _buildData(date, token) { - var date = date.split('-'); + date = date.split('-'); return { "__VIEWSTATE": token.state,