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);
|
||||
Reference in New Issue
Block a user