api/carpa_json_to_markdown/print_update.mjs

107 lines
2.6 KiB
JavaScript

import Typesense from "typesense";
import dayjs from "dayjs";
import { JSDOM } from "jsdom";
const { window } = new JSDOM();
let client = new Typesense.Client({
nodes: [
{
host: "searchts.carpa.com",
port: "443",
protocol: "https",
}
],
apiKey: "3KrmYlcirARCxG4AZPV5bnJgQD0qtoW0",
});
import fs from 'fs';
import readline from 'readline';
import { create } from "domain";
async function processJsonLines(filePath) {
const errors = [];
const stream = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: stream,
crlfDelay: Infinity,
});
let lineCount = 0;
for await (const line of rl) {
lineCount = 0;
try {
// Parse the line as JSON
const data = JSON.parse(line);
//if( data.id != 62643 ) continue; // Skip until we find the specific ID
console.log('Processing: ', data.title);
const doc_id = await createDocument(data);
console.log('Document created with ID:', doc_id);
} catch (error) {
//console.error(`Error processing line ${line}:`, error);
let data = JSON.parse(line);
errors.push({ line: data.id, error: error.message });
}
//break;
}
console.log(`Finished processing. Total lines: ${lineCount}. Errors: ${errors.length}`);
if (errors.length > 0) {
console.log('Errors:', errors);
}
}
//processJsonLines('./input/activities_wp_ES.json');
processJsonLines('./input/conferences_wp_ES.json');
{
"date": 1779210000,
"files": [
{
"downloadname": "marcador_elsonador-cumpleJBP2026 copy.jpg",
"filename": "b7b145db-b3ae-4ade-bc07-0fd675ae1195.jpg",
"type": ""
}
],
"id": "1",
"images": [
{
"downloadname": "photo_2026-05-19_17-41-20.jpg",
"file": "995283e3-a798-48c9-9221-ae1edd4b2706.jpg"
}
],
"locale": "es",
"number": 1,
"slug": "marcador-el-sonador-cumpliendo-el-establecimiento-del-reino-judio",
"subtype": "marcador",
"thumbnail": "15f887a2-c18a-43d5-aa83-0313707aef22.png",
"title": "Marcador: El Soñador cumpliendo el establecimiento del Reino Judío",
"type": "material"
}
// Main function to create document in Typesense
async function createDocument(data){
let document = {
date: data.timestamp,
files: data.files,
id: data.id,
images: data.images,
locale: data.locale,
number: data.number,
slug: data.slug,
subtype: data.subtype,
thumbnail: data.thumbnail,
title: data.title,
type: data.type
};
let doc = await client.collections('print-materials').documents().upsert(document);
return doc.id;
}