mirror of
https://github.com/catdevnull/preciazo.git
synced 2024-11-26 03:26:19 +00:00
generar index.json
This commit is contained in:
parent
5c3d3f5085
commit
f7b7f1f943
2 changed files with 28 additions and 6 deletions
|
@ -4,7 +4,16 @@ import { listDirectory } from "./b2";
|
||||||
import { isSameDay } from "date-fns";
|
import { isSameDay } from "date-fns";
|
||||||
import { indexResources } from "./index-resources";
|
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 resourcesIndex = await indexResources();
|
||||||
|
|
||||||
const datasets = z
|
const datasets = z
|
||||||
|
@ -111,6 +120,9 @@ esto esta automáticamente generado por sepa-index-gen dentro de preciazo.`;
|
||||||
hour: "2-digit",
|
hour: "2-digit",
|
||||||
minute: "2-digit",
|
minute: "2-digit",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let jsonIndex: Record<string, IndexEntry[]> = {};
|
||||||
|
|
||||||
for (const dateStr of dates) {
|
for (const dateStr of dates) {
|
||||||
const date = new Date(dateStr);
|
const date = new Date(dateStr);
|
||||||
markdown += `\n* ${formatter.format(date)}:`;
|
markdown += `\n* ${formatter.format(date)}:`;
|
||||||
|
@ -120,6 +132,7 @@ esto esta automáticamente generado por sepa-index-gen dentro de preciazo.`;
|
||||||
if (!resourcesInDate.length) {
|
if (!resourcesInDate.length) {
|
||||||
markdown += " ❌ no tengo recursos para esta fecha";
|
markdown += " ❌ no tengo recursos para esta fecha";
|
||||||
}
|
}
|
||||||
|
jsonIndex[dateStr] = [];
|
||||||
for (const resource of resourcesInDate) {
|
for (const resource of resourcesInDate) {
|
||||||
const id = `${resource.id}-revID-${resource.revision_id}`;
|
const id = `${resource.id}-revID-${resource.revision_id}`;
|
||||||
const fileExists = fileList.find((file) => file.startsWith(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 ";
|
"⁉️⚠️ dia de semana incorrecto, puede haberse subido incorrectamente ";
|
||||||
}
|
}
|
||||||
markdown += `\n * ${id} ${warnings} ${fileExists ? `[✅ descargar](${link})` : "❌"} (primera vez visto: ${dateTimeFormatter.format(resource.firstSeenAt)})`;
|
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 };
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,10 @@ import { z } from "zod";
|
||||||
import { zDatasetInfo } from "ckan/schemas";
|
import { zDatasetInfo } from "ckan/schemas";
|
||||||
import { mkdtemp, writeFile, readdir, mkdir, rm } from "fs/promises";
|
import { mkdtemp, writeFile, readdir, mkdir, rm } from "fs/promises";
|
||||||
import { basename, extname, join } from "path";
|
import { basename, extname, join } from "path";
|
||||||
import { $, write } from "bun";
|
import { $ } from "bun";
|
||||||
import { S3Client, HeadObjectCommand } from "@aws-sdk/client-s3";
|
import { S3Client, HeadObjectCommand } from "@aws-sdk/client-s3";
|
||||||
import { Upload } from "@aws-sdk/lib-storage";
|
import { Upload } from "@aws-sdk/lib-storage";
|
||||||
import { generateMarkdown } from "sepa-index-gen";
|
import { generateIndexes } from "sepa-index-gen";
|
||||||
|
|
||||||
function checkEnvVariable(variableName: string) {
|
function checkEnvVariable(variableName: string) {
|
||||||
const value = process.env[variableName];
|
const value = process.env[variableName];
|
||||||
|
@ -162,8 +162,9 @@ for (const resource of datasetInfo.result.resources) {
|
||||||
await uploadToB2Bucket(fileName, response);
|
await uploadToB2Bucket(fileName, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const { markdown, jsonIndex } = await generateIndexes();
|
||||||
await saveFileIntoRepo("index.md", await generateMarkdown());
|
await saveFileIntoRepo("index.md", markdown);
|
||||||
|
await saveFileIntoRepo("index.json", JSON.stringify(jsonIndex, null, 2));
|
||||||
|
|
||||||
if (errored) {
|
if (errored) {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|
Loading…
Reference in a new issue