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()