Created
August 9, 2022 17:34
-
-
Save tkon99/b2b302ced26fffa88ea61dbce982691c to your computer and use it in GitHub Desktop.
WP-JSON to RSS proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express'; | |
import rateLimit from 'express-rate-limit'; | |
import isValidDomain from 'is-valid-domain'; | |
import fetch from 'node-fetch'; | |
import Feed from 'pfeed'; | |
const app = express(); | |
const port = 2847; | |
/* Rate limiter */ | |
const limiter = rateLimit({ | |
windowMs: 5 * 60 * 1000, // 5 minutes | |
max: 25, // Limit each IP to 25 requests per `window` (here, per 15 minutes) | |
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers | |
legacyHeaders: false, // Disable the `X-RateLimit-*` headers | |
}); | |
//TODO: ENABLE; app.use(limiter); | |
/* Routes */ | |
app.get('/', (req, res) => { | |
res.send('Hi, this is a WP-JSON to RSS proxy, add a domain name to the end of this url to convert its WP-JSON feed (pages & posts) to RSS. Note: subdomains are allowed, but directories are stripped.'); | |
}); | |
// Main function | |
app.get('/:domain*', (req, res) => { | |
try { | |
let domain = req.params.domain; | |
if(!isValidDomain(domain, {allowUnicode: false})){ | |
res.send("Domain not valid."); | |
} | |
let promises = [ | |
fetch('http://'+domain+'/wp-json/wp/v2/pages?context=embed'), | |
fetch('http://'+domain+'/wp-json/wp/v2/posts?scope=embed') | |
]; | |
let feed = new Feed({ | |
title: domain, | |
id: domain, | |
link: domain | |
}); | |
Promise.all(promises).then(rs => { | |
Promise.all(rs.map(r => { | |
if(r.status == 200){ | |
return r.json() | |
} | |
})).then(c => { | |
let content = c.flat().filter(i => i !== undefined); | |
content.forEach(item => { | |
feed.addItem({ | |
title: item.title.rendered, | |
id: item.id, | |
link: item.link, | |
description: item.excerpt.rendered, | |
content: item.excerpt.rendered, | |
date: new Date(item.date) | |
}); | |
}); | |
res.set('Content-Type', 'application/rss+xml'); | |
res.send(feed.rss2()); | |
}).catch(e => res.send("Something went wrong. The website probably does not support WP-JSON."));; | |
}).catch(e => res.send("Something went wrong. The website probably does not support WP-JSON.")); | |
} catch (error) { | |
res.send("Something went wrong. The website probably does not support WP-JSON."); | |
} | |
}); | |
app.listen(port, () => { | |
console.log(`Example app listening on port ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The idea is easy: take any Wordpress based website and convert it to an RSS feed, I know this is possible for posts by using the /feed url in Wordpress, but I came across a website that uses pages for everything and still wanted to follow it. Feel free to use it in whatever way you want, no warranty provided.