feat: Add dynamic publication type inference based on text content
This commit is contained in:
parent
75feab0ab1
commit
81826fbf9c
@ -34,6 +34,25 @@ function extractText(msg) {
|
|||||||
return null;
|
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() {
|
async function run() {
|
||||||
console.log("Starting publishing process...");
|
console.log("Starting publishing process...");
|
||||||
try {
|
try {
|
||||||
@ -42,8 +61,11 @@ async function run() {
|
|||||||
const mediaCollectionId = collections['media'] || 'media';
|
const mediaCollectionId = collections['media'] || 'media';
|
||||||
const pubTypesCollectionId = collections['publication types'] || collections['publication_types'] || 'publication_types';
|
const pubTypesCollectionId = collections['publication types'] || collections['publication_types'] || 'publication_types';
|
||||||
|
|
||||||
const pubTypes = await databases.listDocuments(DB_ID, pubTypesCollectionId);
|
const pubTypesRes = await databases.listDocuments(DB_ID, pubTypesCollectionId);
|
||||||
const defaultPubTypeId = pubTypes.documents.length > 0 ? pubTypes.documents[0].$id : null;
|
const typeMap = {};
|
||||||
|
pubTypesRes.documents.forEach(d => {
|
||||||
|
typeMap[d.codigo] = d.$id;
|
||||||
|
});
|
||||||
|
|
||||||
// 1. Delete old posts
|
// 1. Delete old posts
|
||||||
console.log("Deleting old script posts...");
|
console.log("Deleting old script posts...");
|
||||||
@ -104,7 +126,6 @@ async function run() {
|
|||||||
const text = extractText(nearbyMsg);
|
const text = extractText(nearbyMsg);
|
||||||
if (text && text.length > 20 && !text.toLowerCase().includes("comparte en tus historias")) {
|
if (text && text.length > 20 && !text.toLowerCase().includes("comparte en tus historias")) {
|
||||||
bestText = text;
|
bestText = text;
|
||||||
// if this text is very close (older), break
|
|
||||||
if (j > i) break;
|
if (j > i) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,6 +147,8 @@ async function run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const inferredType = inferPublicationType(cleanCaption, typeMap);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get base64
|
// Get base64
|
||||||
const base64Res = await axios.post(
|
const base64Res = await axios.post(
|
||||||
@ -157,14 +180,14 @@ async function run() {
|
|||||||
await databases.createDocument(DB_ID, postsCollectionId, ID.unique(), {
|
await databases.createDocument(DB_ID, postsCollectionId, ID.unique(), {
|
||||||
titulo: title,
|
titulo: title,
|
||||||
slug: slug,
|
slug: slug,
|
||||||
tipo_publicacion_id: defaultPubTypeId,
|
tipo_publicacion_id: inferredType,
|
||||||
estado: 'borrador',
|
estado: 'borrador',
|
||||||
fecha_publicacion: date.toISOString(),
|
fecha_publicacion: date.toISOString(),
|
||||||
contenido: `<p>${cleanCaption.replace(/\n/g, '<br/>')}</p>`,
|
contenido: `<p>${cleanCaption.replace(/\n/g, '<br/>')}</p>`,
|
||||||
imagen_destacada_id: mediaDoc.$id
|
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]++;
|
monthCounts[month]++;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`Failed to publish image for month ${month}:`, err.message);
|
console.error(`Failed to publish image for month ${month}:`, err.message);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user