Last active
March 19, 2018 19:03
-
-
Save leocb/0c6883f0f4fbf31c5996b17f110cfdce to your computer and use it in GitHub Desktop.
This file contains 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
// Como usar: | |
// Copie todo esse codigo | |
// abra o site do inmet: http://www.inmet.gov.br/sim/cond_reg/tempoCapitais.php | |
// aperte F12, vá na aba console, cole o codigo a aperte enter | |
// Defina as datas de inicio e fim nos campos que aparecem na tela | |
// Clique em 'buscar dados' | |
// Aguarde o fim do processo | |
// Clique no link gerado para fazer donwload do arquivo | |
var countRequests = 0 | |
var countDone = 0 | |
var dados = [] | |
var outString = '' | |
var dateLoop | |
function daysBetweenTwoDates(firstDate, secondDate) { | |
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds | |
return Math.abs(Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)))) | |
} | |
function makeRequests() { | |
dateString = dateLoop.getFullYear() + '-' + | |
(dateLoop.getMonth() < 9 ? ('0' + (dateLoop.getMonth() + 1)) : (dateLoop.getMonth() + 1)) + '-' + | |
(dateLoop.getDate() <= 9 ? ('0' + dateLoop.getDate()) : dateLoop.getDate()) | |
inmetRequest(dateString) | |
} | |
function inmetRequest(dateString) { | |
var xhttp = new XMLHttpRequest() | |
xhttp.onreadystatechange = function () { | |
if (this.readyState == 4 && this.status == 200) { | |
processResponse(this.responseText) | |
} | |
} | |
xhttp.open('GET', 'http://www.inmet.gov.br/sim/cond_reg/tempoCapitais.php?data=' + dateString, true) | |
xhttp.send() | |
} | |
function processResponse(res) { | |
var resp = res.match(/<td(?!(\s?style)|(>?<b>)|\s?height|.*<img).*\/td>/ig) | |
for (var i = 5; i < resp.length; i += 5) { | |
var cidade = resp[i + 0].replace(/<\/?td.*?>/i, '').replace(/<\/?td.*?>/i, '').trim() | |
var tempMin = resp[i + 1].replace(/<\/?td.*?>/i, '').replace(/<\/?td.*?>/i, '').replace(',','.').trim().replace(' °C', '') | |
var tempMax = resp[i + 2].replace(/<\/?td.*?>/i, '').replace(/<\/?td.*?>/i, '').replace(',','.').trim().replace(' °C', '') | |
var umidade = resp[i + 3].replace(/<\/?td.*?>/i, '').replace(/<\/?td.*?>/i, '').replace(',','.').trim() | |
var chuva = resp[i + 4].replace(/<\/?td.*?>/i, '').replace(/<\/?td.*?>/i, '').replace(',','.').trim() | |
if (!dados[cidade]) { | |
dados[cidade] = { id: [], tempMin: [], tempMax: [], umidade: [], chuva: [] } | |
} | |
dados[cidade].tempMin.push(tempMin) | |
dados[cidade].tempMax.push(tempMax) | |
dados[cidade].umidade.push(umidade) | |
dados[cidade].chuva.push(chuva) | |
} | |
countDone++ | |
checkAllDone() | |
} | |
function checkAllDone() { | |
let completed = Math.round((countDone / countRequests * 100), 1) | |
console.log(completed + '%') | |
myBar.style.width = completed + '%'; | |
if (countDone == countRequests) { | |
printResult() | |
}else{ | |
dateLoop.setDate(dateLoop.getDate() + 1) | |
makeRequests() | |
} | |
} | |
function printResult() { | |
var cidades = Object.keys(dados) | |
cidades.forEach(function (chave) { | |
cidade = dados[chave] | |
seqIndex = 0 | |
var dateLoop = new Date(dataIniEl.value + 'T00:00:00') | |
outString += '\n' + chave + ', Data:' | |
for (var i = 0; i < cidade.tempMin.length; i++) { | |
outString += ',' + dateLoop.getDate().toString() + '/' + (dateLoop.getMonth() + 1).toString() + '/' + dateLoop.getFullYear().toString() | |
dateLoop.setDate(dateLoop.getDate() + 1) | |
} | |
outString += '\n' + chave + ', TempMin:' | |
for (var i = 0; i < cidade.tempMin.length; i++) { | |
outString += ',' + cidade.tempMin[i] | |
} | |
outString += '\n' + chave + ', TempMax:' | |
for (var i = 0; i < cidade.tempMax.length; i++) { | |
outString += ',' + cidade.tempMax[i] | |
} | |
outString += '\n' + chave + ', Umidade:' | |
for (var i = 0; i < cidade.umidade.length; i++) { | |
outString += ',' + cidade.umidade[i] | |
} | |
outString += '\n' + chave + ', Chuva:' | |
for (var i = 0; i < cidade.chuva.length; i++) { | |
outString += ',' + cidade.chuva[i] | |
} | |
}) | |
console.log(outString) | |
habilitaDownload() | |
} | |
// date to Vaue polyfill | |
Date.prototype.toDateInputValue = (function () { | |
var local = new Date(this); | |
local.setMinutes(this.getMinutes() - this.getTimezoneOffset()); | |
return local.toJSON().slice(0, 10); | |
}); | |
// Document elements cusomization | |
document.body.innerHTML = '' | |
dataIniEl = document.createElement('input') | |
dataIniEl.type = 'date' | |
dataIniEl.value = new Date().toDateInputValue(); | |
dataFinEl = document.createElement('input') | |
dataFinEl.type = 'date' | |
dataFinEl.value = new Date().toDateInputValue(); | |
botaoIniciar = document.createElement('button') | |
botaoIniciar.innerHTML = 'Buscar dados' | |
botaoIniciar.setAttribute('onClick', 'puxarDados()') | |
linkDeDownload = document.createElement('a') | |
myProgress = document.createElement('div') | |
myBar = document.createElement('div') | |
myProgress.appendChild(myBar) | |
myProgress.style = 'width: 100%; background-color: grey' | |
myBar.style = 'width: 0%; height: 30px; background-color: green;' | |
document.body.appendChild(dataIniEl) | |
document.body.appendChild(dataFinEl) | |
document.body.appendChild(botaoIniciar) | |
document.body.appendChild(myProgress) | |
document.body.appendChild(linkDeDownload) | |
dataIniEl.insertAdjacentText('beforebegin','Data Inicial: ') | |
dataFinEl.insertAdjacentText('beforebegin',' - Data Final: ') | |
// UI Functions | |
function habilitaDownload() { | |
botaoIniciar.disabled = false | |
botaoIniciar.innerHTML = 'Buscar dados' | |
with (linkDeDownload) { | |
href = 'data:text/csv;base64,' + btoa(outString) | |
download = 'Dados_temperatura.csv' | |
innerHTML = 'Clique aqui para salvar em um arquivo .csv' | |
} | |
} | |
function puxarDados() { | |
countRequests = 0 | |
countDone = 0 | |
dados = [] | |
outString = '' | |
dateLoop | |
botaoIniciar.disabled = true | |
botaoIniciar.innerHTML = 'Aguarde...' | |
linkDeDownload.innerHTML = '' | |
myBar.style.width = '0%'; | |
let dateIni = new Date(dataIniEl.value + 'T00:00:00') | |
let dateFin = new Date(dataFinEl.value + 'T00:00:00') | |
dateLoop = dateIni | |
countRequests = daysBetweenTwoDates(dateIni, dateFin) | |
makeRequests(dateIni, dateFin) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment