feat: Add dynamic publication type inference based on text content

This commit is contained in:
Carlos (Almeira) 2026-06-21 12:17:42 -05:00
parent 75feab0ab1
commit 81826fbf9c

View File

@ -34,6 +34,25 @@ function extractText(msg) {
return null;
}
function inferPublicationType(text, typeMap) {
if (!text) return typeMap['NOTICIA'];
const t = text.toLowerCase();
// Check for Evento
const eventKeywords = ['invita', 'invitación', 'asiste', 'te esperamos', 'evento', 'jornada', 'foro', 'congreso', 'feria', 'festival', 'encuentro', 'conmemoramos', 'celebramos'];
if (eventKeywords.some(kw => t.includes(kw))) {
return typeMap['EVENTO'] || typeMap['NOTICIA'];
}
// Check for Clasificado (or Convocatoria)
const classKeywords = ['oferta', 'empleo', 'vacante', 'convocatoria', 'se busca', 'clasificado', 'postúlate', 'inscríbete'];
if (classKeywords.some(kw => t.includes(kw))) {
return typeMap['CLASIFICADO'] || typeMap['NOTICIA'];
}
return typeMap['NOTICIA'];
}
async function run() {
console.log("Starting publishing process...");
try {
@ -42,8 +61,11 @@ async function run() {
const mediaCollectionId = collections['media'] || 'media';
const pubTypesCollectionId = collections['publication types'] || collections['publication_types'] || 'publication_types';
const pubTypes = await databases.listDocuments(DB_ID, pubTypesCollectionId);
const defaultPubTypeId = pubTypes.documents.length > 0 ? pubTypes.documents[0].$id : null;
const pubTypesRes = await databases.listDocuments(DB_ID, pubTypesCollectionId);
const typeMap = {};
pubTypesRes.documents.forEach(d => {
typeMap[d.codigo] = d.$id;
});
// 1. Delete old posts
console.log("Deleting old script posts...");
@ -104,7 +126,6 @@ async function run() {
const text = extractText(nearbyMsg);
if (text && text.length > 20 && !text.toLowerCase().includes("comparte en tus historias")) {
bestText = text;
// if this text is very close (older), break
if (j > i) break;
}
}
@ -126,6 +147,8 @@ async function run() {
}
}
const inferredType = inferPublicationType(cleanCaption, typeMap);
try {
// Get base64
const base64Res = await axios.post(
@ -157,14 +180,14 @@ async function run() {
await databases.createDocument(DB_ID, postsCollectionId, ID.unique(), {
titulo: title,
slug: slug,
tipo_publicacion_id: defaultPubTypeId,
tipo_publicacion_id: inferredType,
estado: 'borrador',
fecha_publicacion: date.toISOString(),
contenido: `<p>${cleanCaption.replace(/\n/g, '<br/>')}</p>`,
imagen_destacada_id: mediaDoc.$id
});
console.log(`[Success] Published post ${monthCounts[month] + 1}/8 for Month ${month}: ${title}`);
console.log(`[Success] Published post ${monthCounts[month] + 1}/8 for Month ${month}: ${title} (${Object.keys(typeMap).find(k => typeMap[k] === inferredType)})`);
monthCounts[month]++;
} catch (err) {
console.error(`Failed to publish image for month ${month}:`, err.message);