feat(cmd): Add Nitter consumption

This commit is contained in:
vulet 2020-06-24 04:22:30 +08:00
parent 2d23f24a6a
commit 751dbb335f
5 changed files with 75 additions and 1 deletions

63
commands/nitter.js Normal file
View file

@ -0,0 +1,63 @@
const axios = require('axios');
const { JSDOM } = require("jsdom");
const headers = ({ domain, userAgent }) => ({
'Host': `${domain}`,
'User-Agent': `${userAgent}`
});
const nitter = async (instance, url) => {
const req = await instance({ method: 'GET', url });
if (req.statusText !== 'OK') throw req;
const dom = new JSDOM(req.data);
const tweet = dom.window.document.querySelector('#m');
const stats = tweet.querySelectorAll('.tweet-body > .tweet-stats .icon-container');
const quote = tweet.querySelector('.tweet-body > .quote a.quote-link');
return {
text: tweet.querySelector('.tweet-body > .tweet-content').innerHTML,
date: tweet.querySelector('.tweet-body > .tweet-published').textContent,
name: tweet.querySelector('.tweet-body > div .fullname').textContent,
handle: tweet.querySelector('.tweet-body > div .username').textContent,
hasAttachments: !!tweet.querySelector('.tweet-body > .attachments'),
quote: quote ? quote.href : null,
stats: {
replies: stats[0].textContent.trim(),
retweets: stats[1].textContent.trim(),
favorites: stats[2].textContent.trim()
}
};
};
const card = (tweet, base, path) =>
`<a href="${base}/${tweet.handle.replace(/^@/, '')}"><b>${tweet.name}</b></a> ` +
`<a href="${base}${path}"><b>${tweet.date}</b></a> ` +
`<span>🗨️ ${tweet.stats.replies}</span> ` +
`<span>🔁 ${tweet.stats.retweets}</span> ` +
`<span>❤️ ${tweet.stats.favorites}</span> ` +
`<br /><blockquote><b><i>${tweet.text.replace('\n', '<br />')}</i></b></blockquote>` +
(tweet.hasAttachments ? '<blockquote><b>This tweet has attached media.</b></blockquote>' : '') +
(tweet.quote ? `<blockquote><b><a href="${base}${tweet.quote}">Quoted Tweet</a></b></blockquote>` : '');
const run = async (matrixClient, { roomId }, userInput, registrar) => {
const config = registrar.config.nitter;
const instance = axios.create({
baseURL: `https://${config.domain}`,
headers: headers(config),
transformResponse: [],
timeout: 10 * 1000
});
const tweet = await nitter(instance, userInput);
return await matrixClient.sendHtmlNotice(roomId, '', card(tweet, `https://${config.domain}`, userInput));
}
exports.runQuery = (client, room, userInput, registrar) => {
let url = null;
try {
url = new URL(userInput);
if(!registrar.config.nitter.domains.includes(url.hostname)) throw '';
if(!/^\/[^/]+\/status\/\d+\/?$/.test(url.pathname)) throw '';
} catch(e) {
return client.sendHtmlNotice(roomId, 'Sad!', `<strong>Sad!</strong>`).catch(()=>{});
}
return run(client, room, url.pathname, registrar);
};

View file

@ -8,5 +8,10 @@ module.exports = {
archive: { archive: {
domain: 'archive.is', domain: 'archive.is',
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)' userAgent: 'Mozilla/4.0 (compatible; Beep Boop)'
},
nitter: {
domain: 'nitter.net',
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)',
domains: [ 'nitter.net', 'twitter.com' ]
} }
}; };

View file

@ -114,6 +114,10 @@ let CreateClient = (token) => {
if (command === 'rearchive') { if (command === 'rearchive') {
registrar.archive.runQuery(matrixClient, room, userInput, true, registrar); registrar.archive.runQuery(matrixClient, room, userInput, true, registrar);
} }
if (command === 'nitter') {
registrar.nitter.runQuery(matrixClient, room, userInput, registrar);
}
} }
}); });

View file

@ -20,7 +20,8 @@
"dependencies": { "dependencies": {
"axios": "^0.19.2", "axios": "^0.19.2",
"file-system": "^2.2.2", "file-system": "^2.2.2",
"matrix-js-sdk": "^2.4.6" "matrix-js-sdk": "^2.4.6",
"jsdom": "^16.2.2"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^5.16.0", "eslint": "^5.16.0",

View file

@ -17,4 +17,5 @@ module.exports = {
unpin: require('./commands/unpin.js'), unpin: require('./commands/unpin.js'),
mordy: require('./commands/mordy.js'), mordy: require('./commands/mordy.js'),
archive: require('./commands/archive.js'), archive: require('./commands/archive.js'),
nitter: require('./commands/nitter.js'),
}; };