2021-02-14 10:57:35 +03:00
|
|
|
const { JSDOM } = require('jsdom');
|
2020-06-23 23:22:30 +03:00
|
|
|
|
|
|
|
const headers = ({ domain, userAgent }) => ({
|
2021-02-14 10:57:35 +03:00
|
|
|
Host: `${domain}`,
|
|
|
|
'User-Agent': `${userAgent}`,
|
2020-06-23 23:22:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const nitter = async (instance, url) => {
|
|
|
|
const req = await instance({ method: 'GET', url });
|
|
|
|
if (req.statusText !== 'OK') throw req;
|
|
|
|
const dom = new JSDOM(req.data);
|
2021-02-14 10:57:35 +03:00
|
|
|
const { document } = dom.window;
|
2020-06-24 01:00:53 +03:00
|
|
|
const tweet = document.querySelector('#m');
|
2020-06-23 23:22:30 +03:00
|
|
|
const stats = tweet.querySelectorAll('.tweet-body > .tweet-stats .icon-container');
|
2020-06-24 01:00:53 +03:00
|
|
|
const quote = tweet.querySelector('.tweet-body > .quote');
|
|
|
|
const isReply = tweet.querySelector('.tweet-body > .replying-to');
|
|
|
|
const replies = document.querySelectorAll('.main-thread > .before-tweet > .timeline-item');
|
2021-06-07 10:30:40 +03:00
|
|
|
const { defaults } = instance;
|
2020-06-23 23:22:30 +03:00
|
|
|
return {
|
2021-06-07 10:30:40 +03:00
|
|
|
url: defaults.baseURL,
|
2020-06-23 23:22:30 +03:00
|
|
|
text: tweet.querySelector('.tweet-body > .tweet-content').innerHTML,
|
|
|
|
date: tweet.querySelector('.tweet-body > .tweet-published').textContent,
|
|
|
|
name: tweet.querySelector('.tweet-body > div .fullname').textContent,
|
2020-11-02 18:22:27 +03:00
|
|
|
check: !!tweet.querySelector('.tweet-body > div .fullname .icon-ok'),
|
2020-06-23 23:22:30 +03:00
|
|
|
handle: tweet.querySelector('.tweet-body > div .username').textContent,
|
|
|
|
hasAttachments: !!tweet.querySelector('.tweet-body > .attachments'),
|
2020-06-24 01:00:53 +03:00
|
|
|
quote: quote ? {
|
|
|
|
path: quote.querySelector('a.quote-link').href,
|
2020-11-02 17:48:07 +03:00
|
|
|
text: quote.querySelector('.quote-text') ? quote.querySelector('.quote-text').innerHTML : '',
|
2020-06-24 01:00:53 +03:00
|
|
|
} : null,
|
2020-09-06 18:10:41 +03:00
|
|
|
isReply: isReply && replies.length > 0 ? replies[replies.length - 1].classList.contains('unavailable') ? 'unavailable' : {
|
2020-06-24 01:00:53 +03:00
|
|
|
path: replies[replies.length - 1].querySelector('a.tweet-link').href,
|
|
|
|
text: replies[replies.length - 1].querySelector('.tweet-content').innerHTML,
|
|
|
|
} : null,
|
|
|
|
stats: {
|
2020-06-23 23:22:30 +03:00
|
|
|
replies: stats[0].textContent.trim(),
|
|
|
|
retweets: stats[1].textContent.trim(),
|
2021-02-14 10:57:35 +03:00
|
|
|
favorites: stats[2].textContent.trim(),
|
|
|
|
},
|
2020-06-23 23:22:30 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-06-07 10:30:40 +03:00
|
|
|
const card = (tweet, check, path) =>
|
|
|
|
`<a href="${tweet.url}/${tweet.handle.replace(/^@/, '')}"><b>${tweet.name}</b></a> ` +
|
2021-02-03 07:13:05 +03:00
|
|
|
(tweet.check ? `${check} ` : '') +
|
2021-06-07 10:30:40 +03:00
|
|
|
`<a href="${tweet.url}${path}"><b>${tweet.date}</b></a> ` +
|
2020-06-23 23:22:30 +03:00
|
|
|
`<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>' : '') +
|
2021-06-07 10:30:40 +03:00
|
|
|
(tweet.isReply ? tweet.isReply === 'unavailable' ? '<blockquote>Replied Tweet is unavailable</blockquote>' : `<blockquote><b><a href="${tweet.url}${tweet.isReply.path}">Replied Tweet</a></b><br /><b><i>${tweet.isReply.text.replace('\n', '<br />')}</i></b></blockquote>` : '') +
|
|
|
|
(tweet.quote ? `<blockquote><b><a href="${tweet.url}${tweet.quote.path}">Quoted Tweet</a></b><br /><b><i>${tweet.quote.text.replace('\n', '<br />')}</i></b></blockquote>` : '');
|
2021-02-14 10:57:35 +03:00
|
|
|
|
2021-06-07 10:30:40 +03:00
|
|
|
const getInstance = config =>
|
|
|
|
axios.create({
|
|
|
|
baseURL: `https://${config.domain}`,
|
|
|
|
headers: headers(config),
|
2020-06-23 23:22:30 +03:00
|
|
|
transformResponse: [],
|
2021-02-14 10:57:35 +03:00
|
|
|
timeout: 10 * 1000,
|
2020-06-23 23:22:30 +03:00
|
|
|
});
|
2021-06-07 10:30:40 +03:00
|
|
|
|
|
|
|
const run = async (roomId, userInput) => {
|
|
|
|
const tweet = await nitter(getInstance(config.nitter), userInput)
|
|
|
|
.catch(_ => nitter(getInstance(Object.assign(config.nitter, { domain: config.nitter.fallback })), userInput));
|
|
|
|
return matrixClient.sendHtmlNotice(roomId, '', card(tweet, config.nitter.check, userInput));
|
2021-02-14 10:57:35 +03:00
|
|
|
};
|
2020-06-24 01:00:53 +03:00
|
|
|
|
2021-02-14 10:57:35 +03:00
|
|
|
exports.runQuery = async (roomId, event, userInput) => {
|
2020-06-23 23:22:30 +03:00
|
|
|
try {
|
2020-06-23 23:45:09 +03:00
|
|
|
const url = new URL(userInput);
|
2021-02-14 10:57:35 +03:00
|
|
|
if (!config.nitter.domains.includes(url.hostname)) throw '';
|
|
|
|
if (!/^\/[^/]+\/status\/\d+\/?$/.test(url.pathname)) throw '';
|
|
|
|
return await run(roomId, url.pathname);
|
|
|
|
} catch (e) {
|
|
|
|
return matrixClient.sendHtmlNotice(roomId, 'Sad!', '<strong>Sad!</strong>').catch(() => {});
|
2020-06-23 23:22:30 +03:00
|
|
|
}
|
|
|
|
};
|