first commit
This commit is contained in:
59
scripts/fetch-wordpress.js
Normal file
59
scripts/fetch-wordpress.js
Normal file
@@ -0,0 +1,59 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
const WORDPRESS_API = 'https://www.mistergeek.net/wp-json/wp/v2';
|
||||
const OUTPUT_DIR = path.join(__dirname, '..', 'data', 'wordpress');
|
||||
|
||||
async function fetchPosts(page = 1, perPage = 100) {
|
||||
const response = await fetch(`${WORDPRESS_API}/posts?page=${page}&per_page=${perPage}&_embed`);
|
||||
const posts = await response.json();
|
||||
|
||||
if (response.headers.get('x-wp-totalpages') > page) {
|
||||
const nextPosts = await fetchPosts(page + 1, perPage);
|
||||
return [...posts, ...nextPosts];
|
||||
}
|
||||
|
||||
return posts;
|
||||
}
|
||||
|
||||
async function fetchCategories() {
|
||||
const response = await fetch(`${WORDPRESS_API}/categories?per_page=100`);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function fetchTags() {
|
||||
const response = await fetch(`${WORDPRESS_API}/tags?per_page=100`);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function fetchAuthors() {
|
||||
const response = await fetch(`${WORDPRESS_API}/users?per_page=100`);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function generateData() {
|
||||
// Ensure output directory exists
|
||||
if (!fs.existsSync(OUTPUT_DIR)) {
|
||||
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
console.log('Fetching WordPress data...');
|
||||
|
||||
const [posts, categories, tags, authors] = await Promise.all([
|
||||
fetchPosts(),
|
||||
fetchCategories(),
|
||||
fetchTags(),
|
||||
fetchAuthors()
|
||||
]);
|
||||
|
||||
// Save data as JSON files
|
||||
fs.writeFileSync(path.join(OUTPUT_DIR, 'posts.json'), JSON.stringify(posts, null, 2));
|
||||
fs.writeFileSync(path.join(OUTPUT_DIR, 'categories.json'), JSON.stringify(categories, null, 2));
|
||||
fs.writeFileSync(path.join(OUTPUT_DIR, 'tags.json'), JSON.stringify(tags, null, 2));
|
||||
fs.writeFileSync(path.join(OUTPUT_DIR, 'authors.json'), JSON.stringify(authors, null, 2));
|
||||
|
||||
console.log(`✅ Fetched ${posts.length} posts, ${categories.length} categories, ${tags.length} tags, ${authors.length} authors`);
|
||||
}
|
||||
|
||||
generateData().catch(console.error);
|
||||
65
scripts/generate-content.js
Normal file
65
scripts/generate-content.js
Normal file
@@ -0,0 +1,65 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const DATA_DIR = path.join(__dirname, '..', 'data', 'wordpress');
|
||||
const CONTENT_DIR = path.join(__dirname, '..', 'content', 'posts');
|
||||
|
||||
function generateContent() {
|
||||
const posts = JSON.parse(fs.readFileSync(path.join(DATA_DIR, 'posts.json'), 'utf8'));
|
||||
|
||||
// Ensure content directory exists
|
||||
if (!fs.existsSync(CONTENT_DIR)) {
|
||||
fs.mkdirSync(CONTENT_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
posts.forEach(post => {
|
||||
const slug = post.slug;
|
||||
const date = new Date(post.date);
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
|
||||
const contentDir = path.join(CONTENT_DIR, `${year}-${month}-${slug}`);
|
||||
|
||||
if (!fs.existsSync(contentDir)) {
|
||||
fs.mkdirSync(contentDir, { recursive: true });
|
||||
}
|
||||
|
||||
const frontmatter = {
|
||||
title: post.title.rendered,
|
||||
date: post.date,
|
||||
draft: post.status !== 'publish',
|
||||
slug: slug,
|
||||
wordpress_id: post.id,
|
||||
excerpt: post.excerpt.rendered.replace(/<[^>]*>/g, ''),
|
||||
featured_image: post._embedded?.['wp:featuredmedia']?.[0]?.source_url || '',
|
||||
author: {
|
||||
id: post.author,
|
||||
name: post._embedded?.author?.[0]?.name || 'Unknown'
|
||||
},
|
||||
categories: post._embedded?.['wp:term']?.[0]?.map(cat => ({
|
||||
id: cat.id,
|
||||
name: cat.name,
|
||||
slug: cat.slug
|
||||
})) || [],
|
||||
tags: post._embedded?.['wp:term']?.[1]?.map(tag => ({
|
||||
id: tag.id,
|
||||
name: tag.name,
|
||||
slug: tag.slug
|
||||
})) || []
|
||||
};
|
||||
|
||||
const content = `---
|
||||
${Object.entries(frontmatter)
|
||||
.map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
|
||||
.join('\n')}
|
||||
---
|
||||
|
||||
${post.content.rendered}`;
|
||||
|
||||
fs.writeFileSync(path.join(contentDir, 'index.md'), content);
|
||||
});
|
||||
|
||||
console.log(`✅ Generated ${posts.length} content files`);
|
||||
}
|
||||
|
||||
generateContent();
|
||||
Reference in New Issue
Block a user