-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2bd82d3
commit 8bdf290
Showing
1 changed file
with
9 additions
and
77 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,9 @@ | ||
const XLSX = require("xlsx"); | ||
const axios = require("axios"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const https = require("https"); | ||
|
||
async function fetchIdmData(kodeDesa, tahun) { | ||
const url = `https://idm.kemendesa.go.id/open/api/desa/rumusan/${kodeDesa}/${tahun}`; | ||
|
||
try { | ||
const response = await axios.get(url, { | ||
httpsAgent: new https.Agent({ rejectUnauthorized: false }), | ||
}); | ||
return response.data; | ||
} catch (error) { | ||
console.error( | ||
`Error fetching data for ${kodeDesa} in ${tahun}:`, | ||
error.message | ||
); | ||
return null; | ||
} | ||
} | ||
|
||
function readCSVFile(filePath) { | ||
const workbook = XLSX.readFile(filePath); | ||
const sheetName = workbook.SheetNames[0]; | ||
const worksheet = workbook.Sheets[sheetName]; | ||
return XLSX.utils.sheet_to_json(worksheet, { header: 1 }); | ||
} | ||
|
||
async function generateJsonFromExcel(excelFilePath, tahunAwal, tahunAkhir) { | ||
const data = readCSVFile(excelFilePath); | ||
const outputDir = path.join(__dirname, "public", "api"); | ||
|
||
for (let i = 1; i < data.length; i++) { | ||
const [kodeDesa, kodeKecamatan, namaDesa] = data[i]; | ||
const kodeDesaStr = String(kodeDesa); | ||
|
||
for (let tahun = tahunAwal; tahun <= tahunAkhir; tahun++) { | ||
const outputFilePath = path.join(outputDir, kodeDesaStr, `${tahun}.json`); | ||
|
||
if (fs.existsSync(outputFilePath)) { | ||
console.log( | ||
`File JSON untuk desa ${namaDesa} (${kodeDesaStr}) tahun ${tahun} sudah ada.` | ||
); | ||
continue; | ||
} | ||
|
||
const idmResult = await fetchIdmData(kodeDesaStr, tahun); | ||
|
||
if (!fs.existsSync(path.dirname(outputFilePath))) { | ||
fs.mkdirSync(path.dirname(outputFilePath), { recursive: true }); | ||
} | ||
|
||
if (idmResult) { | ||
fs.writeFileSync(outputFilePath, JSON.stringify(idmResult, null, 2)); | ||
console.log( | ||
`File JSON untuk desa ${namaDesa} (${kodeDesaStr}) tahun ${tahun} berhasil dibuat.` | ||
); | ||
} else { | ||
console.warn( | ||
`Tidak ada data untuk desa ${namaDesa} (${kodeDesaStr}) tahun ${tahun}.` | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const args = process.argv.slice(2); | ||
const defaultFileName = "desa.csv"; | ||
const excelFilePath = path.join(__dirname, "data", args[0] || defaultFileName); | ||
const tahunAwal = 2024; | ||
const tahunAkhir = new Date().getFullYear(); // Ambil tahun saat ini | ||
|
||
console.log(`Menggunakan file CSV: ${excelFilePath}`); | ||
|
||
generateJsonFromExcel(excelFilePath, tahunAwal, tahunAkhir); | ||
const express = require("express"); | ||
const app = express(); | ||
app.get("/", (req, res) => { | ||
res.send("Express on Vercel"); | ||
}); | ||
const PORT = process.env.PORT || 5000; | ||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}`); | ||
}); |