ai-news-writer-ci / test (pull_request) Failing after 33s
Drop hallucinated athlete/team tags and derive Yoast fields only from published article content. Adds backfill tool for live andebol.net posts. Co-authored-by: Cursor <[email protected]>
120 lines
3.8 KiB
JavaScript
120 lines
3.8 KiB
JavaScript
#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
/**
|
|
* Backfill WordPress post tags and Yoast SEO meta on andebol.net.
|
|
*
|
|
* Usage:
|
|
* node tools/backfill-wp-meta.js --site andebol.net.example.json --dry-run
|
|
* node tools/backfill-wp-meta.js --site andebol.net.json --limit 50
|
|
*/
|
|
|
|
require('dotenv').config();
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const axios = require('axios');
|
|
const { sanitizeArticleMeta } = require('../src/publisher/article-meta');
|
|
const { resolveTagIds } = require('../src/publisher/wordpress');
|
|
|
|
const ROOT = path.resolve(__dirname, '..');
|
|
|
|
function parseArgs(argv) {
|
|
const out = { dryRun: false, limit: 100, siteFile: null };
|
|
for (let i = 0; i < argv.length; i += 1) {
|
|
const arg = argv[i];
|
|
if (arg === '--dry-run') out.dryRun = true;
|
|
else if (arg === '--limit' && argv[i + 1]) out.limit = parseInt(argv[++i], 10);
|
|
else if (arg === '--site' && argv[i + 1]) out.siteFile = argv[++i];
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function loadSiteConfig(siteFile) {
|
|
const sitesDir = path.join(ROOT, 'config', 'sites');
|
|
const filename = siteFile.endsWith('.json') ? siteFile : `${siteFile}.json`;
|
|
const fullPath = path.join(sitesDir, filename);
|
|
if (!fs.existsSync(fullPath)) {
|
|
throw new Error(`Site config not found: ${fullPath}`);
|
|
}
|
|
return JSON.parse(fs.readFileSync(fullPath, 'utf8'));
|
|
}
|
|
|
|
async function main() {
|
|
const args = parseArgs(process.argv.slice(2));
|
|
if (!args.siteFile) {
|
|
console.error('Usage: node tools/backfill-wp-meta.js --site <file.json> [--dry-run] [--limit N]');
|
|
process.exit(1);
|
|
}
|
|
|
|
const site = loadSiteConfig(args.siteFile);
|
|
const wpUrl = site.wp_url || process.env.WP_URL;
|
|
const user = site.user || process.env.WP_USER;
|
|
const password = site.password || process.env.WP_PASSWORD;
|
|
if (!wpUrl || !user || !password) {
|
|
throw new Error('Missing wp_url/user/password in site config or env');
|
|
}
|
|
|
|
const apiBase = `${wpUrl.replace(/\/$/, '').replace(/\/wp-json\/?$/, '')}/wp-json/wp/v2`;
|
|
const credentials = Buffer.from(`${user}:${password}`).toString('base64');
|
|
const siteDomain = site.site_domain || site.wp_site_domain;
|
|
const headers = {
|
|
Authorization: `Basic ${credentials}`,
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json'
|
|
};
|
|
if (siteDomain) headers['X-Site-Domain'] = siteDomain;
|
|
|
|
const postsRes = await axios.get(`${apiBase}/posts`, {
|
|
params: {
|
|
per_page: Math.min(args.limit, 100),
|
|
status: 'publish,draft',
|
|
orderby: 'date',
|
|
order: 'desc',
|
|
_fields: 'id,title,content,excerpt,tags,meta,link'
|
|
},
|
|
headers,
|
|
timeout: 20000
|
|
});
|
|
|
|
const posts = Array.isArray(postsRes.data) ? postsRes.data : [];
|
|
let patched = 0;
|
|
let skipped = 0;
|
|
|
|
for (const post of posts) {
|
|
const title = post.title?.rendered || post.title || '';
|
|
const body = post.content?.rendered || post.content || '';
|
|
const excerpt = post.excerpt?.rendered || post.excerpt || '';
|
|
|
|
const sanitized = sanitizeArticleMeta({
|
|
title,
|
|
body,
|
|
summary: excerpt,
|
|
tags: []
|
|
}, site);
|
|
|
|
const tagIds = await resolveTagIds(sanitized.tags, apiBase, credentials);
|
|
const payload = {
|
|
tags: tagIds,
|
|
meta: {
|
|
_yoast_wpseo_title: sanitized.seo_title,
|
|
_yoast_wpseo_metadesc: sanitized.seo_description,
|
|
_yoast_wpseo_focuskw: sanitized.focus_keyword
|
|
}
|
|
};
|
|
|
|
console.log(`${args.dryRun ? '[dry-run]' : 'patch'} post ${post.id} tags=${sanitized.tags.join(', ')} focus=${sanitized.focus_keyword}`);
|
|
if (!args.dryRun) {
|
|
await axios.post(`${apiBase}/posts/${post.id}`, payload, { headers, timeout: 15000 });
|
|
}
|
|
patched += 1;
|
|
}
|
|
|
|
console.log(JSON.stringify({ scanned: posts.length, patched, skipped, dryRun: args.dryRun }, null, 2));
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err.message || err);
|
|
process.exit(1);
|
|
});
|