generar index.json

This commit is contained in:
Cat /dev/Nulo 2024-09-12 18:08:57 -03:00
parent 5c3d3f5085
commit f7b7f1f943
2 changed files with 28 additions and 6 deletions

View file

@ -4,7 +4,16 @@ import { listDirectory } from "./b2";
import { isSameDay } from "date-fns";
import { indexResources } from "./index-resources";
export async function generateMarkdown() {
const IndexEntry = z.object({
id: z.string(),
warnings: z.string(),
name: z.string().optional(),
link: z.string().optional(),
firstSeenAt: z.string(),
});
type IndexEntry = z.infer<typeof IndexEntry>;
export async function generateIndexes() {
const resourcesIndex = await indexResources();
const datasets = z
@ -111,6 +120,9 @@ esto esta automáticamente generado por sepa-index-gen dentro de preciazo.`;
hour: "2-digit",
minute: "2-digit",
});
let jsonIndex: Record<string, IndexEntry[]> = {};
for (const dateStr of dates) {
const date = new Date(dateStr);
markdown += `\n* ${formatter.format(date)}:`;
@ -120,6 +132,7 @@ esto esta automáticamente generado por sepa-index-gen dentro de preciazo.`;
if (!resourcesInDate.length) {
markdown += " ❌ no tengo recursos para esta fecha";
}
jsonIndex[dateStr] = [];
for (const resource of resourcesInDate) {
const id = `${resource.id}-revID-${resource.revision_id}`;
const fileExists = fileList.find((file) => file.startsWith(id));
@ -135,8 +148,16 @@ esto esta automáticamente generado por sepa-index-gen dentro de preciazo.`;
"⁉️⚠️ dia de semana incorrecto, puede haberse subido incorrectamente ";
}
markdown += `\n * ${id} ${warnings} ${fileExists ? `[✅ descargar](${link})` : "❌"} (primera vez visto: ${dateTimeFormatter.format(resource.firstSeenAt)})`;
jsonIndex[dateStr].push({
id,
warnings: warnings.trim(),
name: fileExists,
link,
firstSeenAt: resource.firstSeenAt.toISOString(),
});
}
}
return markdown;
return { markdown, jsonIndex };
}

View file

@ -2,10 +2,10 @@ import { z } from "zod";
import { zDatasetInfo } from "ckan/schemas";
import { mkdtemp, writeFile, readdir, mkdir, rm } from "fs/promises";
import { basename, extname, join } from "path";
import { $, write } from "bun";
import { $ } from "bun";
import { S3Client, HeadObjectCommand } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import { generateMarkdown } from "sepa-index-gen";
import { generateIndexes } from "sepa-index-gen";
function checkEnvVariable(variableName: string) {
const value = process.env[variableName];
@ -162,8 +162,9 @@ for (const resource of datasetInfo.result.resources) {
await uploadToB2Bucket(fileName, response);
}
}
await saveFileIntoRepo("index.md", await generateMarkdown());
const { markdown, jsonIndex } = await generateIndexes();
await saveFileIntoRepo("index.md", markdown);
await saveFileIntoRepo("index.json", JSON.stringify(jsonIndex, null, 2));
if (errored) {
process.exit(1);