Skip to content

Instantly share code, notes, and snippets.

@hspedro
Created June 30, 2022 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hspedro/18c4918843a6efe00e4714ba759385ab to your computer and use it in GitHub Desktop.
Save hspedro/18c4918843a6efe00e4714ba759385ab to your computer and use it in GitHub Desktop.
Crawl IPTU Cartography Codes for Campinas
/* JS snippet that can be ran in the console to crawl for cartography codes
* of houses in Campinas:
* 1. Log in: https://cidadao.campinas.sp.gov.br/
* 2. Go to 'IPTU - CONSULTA IPTU DO EXERCICIO'
* 3. Run the script
*
* The output should be a list of valid cartography codes.
* Each cartography code starts with a BASE_CODE that can be found here:
* https://zoneamento.campinas.sp.gov.br/
* Use this to adjust the BASE_CODE
*/
// FIXME: change the code based on above comment
const BASE_CODE = 'CHANGE ME';
const inputCodeRef = document.getElementById('codCartografico');
const submitCodeRef = document.getElementById('btnConsultarImovel');
const resultModalRef = document.getElementsByClassName('modal-body');
const crawl = async (validCodes, start, end) => {
for(let i = start; i < end; i++) {
const formattedNumber = i.toString().padStart(5, '0');
const newCode = `${BASE_CODE}${formattedNumber}`;
inputCodeRef.value = newCode;
submitCodeRef.click();
// Sleep for fairness and to wait modal animation
await new Promise(r => setTimeout(r, 5 * 1000));
// There are two modals already rendered on the DOM, the diff
// between how they are rendered is contained in the height of its div
const failedTextHeight = resultModalRef[0].clientHeight;
const successTextHeight = resultModalRef[1].clientHeight;
let closeModalRef = null;
// Server can throttle our interactions, it is important to stop and restart
// adjusting the `start` parameter
if (failedTextHeight === 0 && successTextHeight === 0) {
console.log(`Stopping, we got throttled at ${newCode}`);
break;
}
// The 'X' button on the modal, may take a while to load, thus we encapsulate
// in this while. If it does not exist, then we close the modal pressing 'No'
// button. If it exists, we close with the 'X' button to dismiss.
while (!closeModalRef) {
if (failedTextHeight > 0) {
closeModalRef = document.getElementsByClassName('btn btn-primary')[0];
} else if (successTextHeight > 0) {
closeModalRef = document.getElementsByClassName('close')[1];
validCodes.push(newCode);
} else {
break;
}
closeModalRef.click();
}
}
}
console.log('validCodes: ', validCodes);
const validCodes = [];
crawl(validCodes, 0, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment