Lastest objects for post, invite and add cron GNEWS

This commit is contained in:
2025-09-26 13:22:07 +02:00
parent b6d18c2681
commit a84fdbed9a
21 changed files with 16320 additions and 5273 deletions

71
config/cron-tasks.ts Normal file
View File

@@ -0,0 +1,71 @@
interface GNewsArticle {
title: string;
description: string;
url: string;
image: string;
publishedAt: string;
source: {
name: string;
url: string;
};
}
type GNewsResponse = {
articles: GNewsArticle[];
};
export default {
/**
* Simple example.
* Every monday at 1am.
*/
GNEWS: {
task: async ({ strapi }) => {
// Add your own logic here (e.g. send a queue of email, create a database backup, etc.).
const API_KEY = "b3760544f6efb233f19d32fe78cc1300";
const keywords = ["chorale"].join("+");
const url = `https://gnews.io/api/v4/search?q=${keywords}&lang=fr&max=10&token=${API_KEY}`;
try {
const response = await fetch(url);
const data = (await response.json()) as GNewsResponse;
const articles: GNewsArticle[] = data.articles;
for (const article of articles) {
const alreadyExists = await strapi.db
.query("api::post.post")
.findOne({
where: { url: article.url },
});
if (!alreadyExists) {
await strapi.db.query("api::post.post").create({
data: {
title: article.title,
content: article.description,
url: article.url,
imageUrl: article.image,
source: article.source.name,
published_at: article.publishedAt,
//is_auto_generated: true,
category: "photo",
author: 14,
},
});
strapi.log.info(`🆕 Article créé : ${article.title}`);
} else {
strapi.log.info(`🔁 Article déjà existant : ${article.title}`);
}
}
} catch (error) {
strapi.log.error("❌ Erreur lors du fetch GNews :", error);
}
},
options: {
rule: "0 0 8 * * *",
},
},
};