Created
May 6, 2022 10:27
-
-
Save TheoOkafor/78c809d7093e9cdd9989b79b0ea5e8d8 to your computer and use it in GitHub Desktop.
Script for scraping polling units from INEC website
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
/* eslint-disable no-param-reassign */ | |
/* eslint-disable no-await-in-loop */ | |
/** | |
* | |
* @param {string} stateId | |
* @returns {object} | |
*/ | |
const getLgas = async (stateId) => { | |
const formData = new FormData(); | |
formData.append('state_id', stateId); | |
const response = await fetch( | |
'https://inecnigeria.org/wp-content/themes/independent-national-electoral-commission/custom/views/lgaView.php', | |
{ method: 'POST', body: formData }, | |
); | |
const data = await response.json(); | |
const newData = { }; | |
Object.values(data).forEach((lga) => { | |
newData[lga.abbreviation] = lga; | |
}); | |
return newData; | |
}; | |
/** | |
* | |
* @param {array} data | |
* @param {string} stateId | |
* @returns { array } | |
*/ | |
const getWards = async (data, stateId) => { | |
let wards = []; | |
const lgas = Object.values(data); | |
for (let i = 0; i < lgas.length; i += 1) { | |
const lga = lgas[i]; | |
const nextFormData = new FormData(); | |
nextFormData.append('state_id', stateId); | |
nextFormData.append('lga_id', lga.abbreviation); | |
const resp = await fetch( | |
// eslint-disable-next-line max-len | |
'https://inecnigeria.org/wp-content/themes/independent-national-electoral-commission/custom/views/wardView.php', | |
{ method: 'POST', body: nextFormData }, | |
); | |
let lgaWards = await resp.json(); | |
lgaWards = Object.values(lgaWards); | |
lgaWards.map((ward) => { | |
ward.lgaIndex = lga.abbreviation; | |
ward.lgaName = lga.name; | |
return ward; | |
}); | |
wards = [...wards, ...lgaWards]; | |
} | |
return wards; | |
}; | |
/** | |
* | |
* @param {string} stateId | |
* @returns { array } | |
*/ | |
const getPollingUnits = async (stateId) => { | |
const lgaData = await getLgas(stateId); | |
const wards = await getWards(lgaData, stateId); | |
const pollingUnits = []; | |
for (let i = 0; i < wards.length; i += 1) { | |
const ward = wards[i]; | |
const wardFormData = new FormData(); | |
wardFormData.append('state_id', stateId); | |
wardFormData.append('lga_id', ward.lgaIndex); | |
wardFormData.append('ward_id', ward.id); | |
let response = await fetch( | |
// eslint-disable-next-line max-len | |
'https://inecnigeria.org/wp-content/themes/independent-national-electoral-commission/custom/views/pollingView.php', | |
{ method: 'POST', body: wardFormData }, | |
); | |
response = await response.json(); | |
Object.values(response).forEach(({ | |
delimitation, remark, name, units, | |
}) => { | |
const newDelimitation = `${stateId}-${ward.lgaIndex}-${ward.abbreviation}-${units}`; | |
pollingUnits.push({ | |
code: delimitation ? delimitation.replaceAll('/', '-') : newDelimitation, | |
name, | |
remark, | |
lga: ward.lgaName, | |
ward: ward.name, | |
}); | |
}); | |
} | |
return pollingUnits; | |
}; | |
const pollingUnits = await getPollingUnits('13'); | |
console.log(pollingUnits); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment