mirror of
https://github.com/10h30/blog-balodeplao.git
synced 2026-05-12 15:21:15 +09:00
Initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import fs from "node:fs";
|
||||
|
||||
async function saveTraffic() {
|
||||
const token = process.env.GRAPH_TOKEN;
|
||||
const repo =
|
||||
process.env.GITHUB_REPOSITORY || "devgelo-labs/astro-starter-pro";
|
||||
const filePath = "./.github/data/clones.json";
|
||||
|
||||
const response = await fetch(
|
||||
`https://api.github.com/repos/${repo}/traffic/clones`,
|
||||
{
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) throw new Error(`GitHub API error: ${response.statusText}`);
|
||||
|
||||
const apiData = await response.json();
|
||||
|
||||
let localData = { count: 0, uniques: 0, clones: [] };
|
||||
|
||||
// 1. Si el archivo ya existe, leerlo
|
||||
if (fs.existsSync(filePath)) {
|
||||
try {
|
||||
localData = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
||||
} catch {
|
||||
console.warn(
|
||||
"⚠️ Warning: Error reading existing clones.json, starting fresh.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Unir (Merge) los clones nuevos con los viejos sin duplicar fechas
|
||||
const combinedClones = [...localData.clones];
|
||||
|
||||
apiData.clones.forEach((newClone) => {
|
||||
const index = combinedClones.findIndex(
|
||||
(c) => c.timestamp === newClone.timestamp,
|
||||
);
|
||||
if (index !== -1) {
|
||||
// Si el día ya existe, actualizamos los números (por si subieron durante el día)
|
||||
combinedClones[index] = newClone;
|
||||
} else {
|
||||
// Si el día es nuevo, lo agregamos
|
||||
combinedClones.push(newClone);
|
||||
}
|
||||
});
|
||||
|
||||
// 3. Ordenar por fecha y actualizar totales globales
|
||||
combinedClones.sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
|
||||
|
||||
const totalCount = combinedClones.reduce((sum, c) => sum + c.count, 0);
|
||||
const totalUniques = combinedClones.reduce((sum, c) => sum + c.uniques, 0);
|
||||
|
||||
const finalData = {
|
||||
count: totalCount,
|
||||
uniques: totalUniques,
|
||||
clones: combinedClones,
|
||||
};
|
||||
|
||||
if (!fs.existsSync("./.github/data"))
|
||||
fs.mkdirSync("./.github/data", { recursive: true });
|
||||
fs.writeFileSync(filePath, JSON.stringify(finalData, null, 2));
|
||||
|
||||
console.log(
|
||||
`✅ Historial actualizado: ${combinedClones.length} días registrados.`,
|
||||
);
|
||||
}
|
||||
|
||||
saveTraffic().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user