Skip to content

Instantly share code, notes, and snippets.

@seanghay
Created October 19, 2022 16:08
Show Gist options
  • Save seanghay/daff995b7f66dafe2ec9a39dd64079f5 to your computer and use it in GitHub Desktop.
Save seanghay/daff995b7f66dafe2ec9a39dd64079f5 to your computer and use it in GitHub Desktop.
Download Postal Code Info in Cambodia
import axios from 'axios'
import { load } from 'cheerio'
import fs from 'fs/promises'
import fse from 'fs-extra'
import Interpreter from 'js-interpreter'
// download provinces list
async function downloadProvinceList() {
const { data: html } = await axios.get('https://www.cambodiapost.post/page/postal-codes', { responseType: "text" });
const $ = load(html);
const items = $("#city option").map(function () {
return {
id: +$(this).attr('value'),
name: $(this).text()
}
}).get();
$('script').each(function() {
const text = $(this).text();
if (/window\.windowvar\s=\swindow\.windowvar/.test(text)) {
const intr = new Interpreter(`${text}; windowvar.data`);
intr.run();
const data = intr.pseudoToNative(intr.value);
fse.writeFileSync('data.json', JSON.stringify(data, null, 2), 'utf-8');
}
})
await fs.writeFile('provinces.json', JSON.stringify(items), 'utf-8');
}
export async function parsePostalCollection(provinceId, districtNo) {
const { data: html } = await axios.get(`https://www.cambodiapost.post/page/postal-codes?city=${provinceId}&district=${districtNo}`, { responseType: 'text' })
const $ = load(html)
const items = $('tbody.tbl-content tr').map(function () {
const km = $(this).find('th.kh-name').text()
const [en, code] = $(this).find('td').get()
return {
km,
en: $(en).text(),
code: $(code).text()
}
}).get()
return items;
}
export async function downloadPostalInfo() {
const provinces = JSON.parse(await fs.readFile('./provinces.json', 'utf-8'));
const districtsOfProvince = JSON.parse(await fs.readFile('./data.json', 'utf-8'));
const data = [];
for (const province of provinces) {
const p = {
...province,
districts: [],
}
data.push(p);
const districts = districtsOfProvince[province.id];
for (const district of districts) {
const codes = await parsePostalCollection(province.id, district.no)
p.districts.push({
...district,
codes,
})
console.log(codes)
await fse.writeJSON('./out.json', data)
}
}
}
await downloadProvinceList()
await downloadPostalInfo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment