Files
blog/scripts/generate_notification_report.js
T
2026-03-01 09:42:39 +08:00

136 lines
4.9 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const matter = require('gray-matter');
const FRIEND_CIRCLE_PATH = path.join(__dirname, '../themes/Ying/static/json/friend_circle_data.json');
const LINK_SUMMARY_PATH = path.join(__dirname, '../failed_links_summary.txt');
const POSTS_DIR = path.join(__dirname, '../content/post');
const BASE_URL = "https://usj.cc";
// Helper to format date as YYYYMMDD
function formatDateYYYYMMDD(date) {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, '0');
const d = String(date.getDate()).padStart(2, '0');
return `${y}${m}${d}`;
}
// Helper to get latest post from content/post
function getLatestPost() {
try {
if (!fs.existsSync(POSTS_DIR)) return null;
let latestFile = null;
let latestDate = new Date(0);
function scanDir(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
scanDir(fullPath);
} else if (file.endsWith('.md')) {
// Try to read frontmatter date
try {
const content = fs.readFileSync(fullPath, 'utf8');
const parsed = matter(content);
if (parsed.data.date) {
const date = new Date(parsed.data.date);
// Only consider posts that are NOT hidden
if (parsed.data.status !== 'hidden' && parsed.data.draft !== true) {
if (date > latestDate) {
latestDate = date;
latestFile = {
title: parsed.data.title || file.replace('.md', ''),
date: date,
// Construct URL based on hugo.toml: /:year:month:day.html
url: `${BASE_URL}/${formatDateYYYYMMDD(date)}.html`
};
}
}
}
} catch (e) {
// Ignore parsing errors
}
}
}
}
scanDir(POSTS_DIR);
return latestFile;
} catch (e) {
console.error('Error finding latest post:', e);
return null;
}
}
async function main() {
let report = "";
// 1. 最新文章
const latestPost = getLatestPost();
if (latestPost) {
report += `### 📝 最新文章\n`;
const dateStr = latestPost.date.toLocaleDateString();
report += `- [${latestPost.title}](${latestPost.url}) (${dateStr})\n`;
report += `\n`;
} else {
report += `### 📝 最新文章\n(未找到公开文章)\n\n`;
}
// 2. 友链检测
try {
if (fs.existsSync(LINK_SUMMARY_PATH)) {
const linkSummary = fs.readFileSync(LINK_SUMMARY_PATH, 'utf8').trim();
report += `### 🔗 友链检测\n`;
report += `${linkSummary}\n\n`;
} else {
report += `### 🔗 友链检测\n(无检测结果)\n\n`;
}
} catch (e) {
console.error("Error reading link summary:", e);
}
// 3. 近三天朋友圈
try {
if (fs.existsSync(FRIEND_CIRCLE_PATH)) {
const data = JSON.parse(fs.readFileSync(FRIEND_CIRCLE_PATH, 'utf8'));
const threeDaysAgo = new Date();
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);
const recentPosts = data.posts.filter(post => new Date(post.date) > threeDaysAgo);
report += `### 👥 朋友圈近况 (近3天)\n`;
if (recentPosts.length > 0) {
// Limit to top 10 to avoid too long email
const limitedPosts = recentPosts.slice(0, 10);
limitedPosts.forEach(post => {
const dateStr = new Date(post.date).toLocaleDateString();
report += `- [${post.author}] [${post.title}](${post.link}) (${dateStr})\n`;
});
if (recentPosts.length > 10) {
report += `... 以及其他 ${recentPosts.length - 10} 篇\n`;
}
} else {
report += `无更新\n`;
}
report += `\n`;
}
} catch (e) {
console.error("Error reading friend circle data:", e);
}
// Output to stdout for debugging
console.log(report);
// Output to GITHUB_ENV if running in Actions
if (process.env.GITHUB_ENV) {
const delimiter = `EOF_${Math.random().toString(36).substr(2, 9)}`;
fs.appendFileSync(process.env.GITHUB_ENV, `DEPLOY_REPORT<<${delimiter}\n${report}\n${delimiter}\n`);
}
}
main();