From 240aed20616ab5dad263ffc98b67646c26101afe Mon Sep 17 00:00:00 2001 From: Amir Husayn Panahifar Date: Tue, 8 Apr 2025 02:51:41 +0330 Subject: [PATCH] Add get latest post api fetch with title,excerpt,url return --- src/apis/main.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/apis/main.js diff --git a/src/apis/main.js b/src/apis/main.js new file mode 100644 index 0000000..9c0b65e --- /dev/null +++ b/src/apis/main.js @@ -0,0 +1,35 @@ +import axios from "axios"; +import { log } from "../utils/logger.js"; + +const getLatestPost = async () => { + try { + const latestpostUrl = "https://forum.parchlinux.com/latest.json"; + const response = await axios.get(latestpostUrl); + const topics = response?.data?.topic_list?.topics; + + if (!Array.isArray(topics) || topics.length === 0) { + log.warn("No topics found in latest post data"); + return null; + } + + const latestPost = topics.find(t => !t.pinned); + + if (!latestPost) { + log.warn("No non-pinned topic found"); + return null; + } + + log.info("Fetched latest post successfully"); + + return { + title: latestPost.title, + excerpt: latestPost.excerpt, + url: `https://forum.parchlinux.com/t/${latestPost.slug}/${latestPost.id}`, + }; + } catch (error) { + log.error("Error fetching latest post:", error?.message || error); + return null; + } +}; + +export { getLatestPost };