2019-08-21 03:49:06 +00:00
|
|
|
const express = require('express');
|
|
|
|
const app = express();
|
2019-08-23 08:21:25 +00:00
|
|
|
const port = 33843;
|
2019-08-21 03:49:06 +00:00
|
|
|
|
|
|
|
const request = require('request');
|
|
|
|
const JSDOM = require('jsdom').JSDOM;
|
|
|
|
const Readability = require('readability');
|
|
|
|
|
2019-08-23 08:21:25 +00:00
|
|
|
app.use(express.urlencoded({ extended: true }));
|
2019-08-21 03:49:06 +00:00
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
2019-08-23 08:21:25 +00:00
|
|
|
res.send('<form method="POST" accept-charset="UTF-8"><input name="url"><button type="submit">SUBMIT</button></form>');
|
2019-08-21 03:49:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const requestCallback = (url, res) => (error, response, body) => {
|
|
|
|
if (!error && response.statusCode == 200) {
|
|
|
|
console.log('Response OK.');
|
|
|
|
|
2019-08-24 05:09:02 +00:00
|
|
|
const doc = new JSDOM(body, {url: url});
|
|
|
|
const reader = new Readability(doc.window.document);
|
2019-08-21 03:49:06 +00:00
|
|
|
const article = reader.parse();
|
|
|
|
|
2019-08-23 08:21:25 +00:00
|
|
|
if (article && article.content) {
|
|
|
|
res.send(article.content);
|
|
|
|
} else {
|
|
|
|
res.sendStatus(404);
|
|
|
|
}
|
2019-08-21 03:49:06 +00:00
|
|
|
} else {
|
|
|
|
console.log('Response error:', error ? error.toString() : response.statusCode);
|
|
|
|
res.sendStatus(response ? response.statusCode : 404);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
app.post('/', (req, res) => {
|
|
|
|
const url = req.body.url;
|
|
|
|
const requestOptions = {
|
|
|
|
url: url,
|
2019-08-23 08:21:25 +00:00
|
|
|
//headers: {'User-Agent': 'Googlebot/2.1 (+http://www.google.com/bot.html)'},
|
|
|
|
headers: {'User-Agent': 'Twitterbot/1.0'},
|
2019-08-21 03:49:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
console.log('Parse request for:', url);
|
|
|
|
|
|
|
|
request(requestOptions, requestCallback(url, res));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Example app listening on port ${port}!`);
|
|
|
|
});
|