81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
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) {
|
|
const post = 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,
|
|
},
|
|
});
|
|
|
|
// Créer le postOwnership associé
|
|
await strapi.db.query("api::post-ownership.post-ownership").create({
|
|
data: {
|
|
post: post.id,
|
|
contextType: "system",
|
|
relation: "owner",
|
|
},
|
|
});
|
|
|
|
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 * * *",
|
|
},
|
|
},
|
|
};
|