Lastest objects for post, invite and add cron GNEWS
This commit is contained in:
71
config/cron-tasks.ts
Normal file
71
config/cron-tasks.ts
Normal 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 * * *",
|
||||
},
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user