refactor(invidious/nitter): add instance fallback. chore(readme): update instances.

This commit is contained in:
vulet 2021-06-07 15:30:40 +08:00
parent c7e3f26f60
commit 54631f5c2a
3 changed files with 36 additions and 24 deletions

View file

@ -6,8 +6,10 @@ const headers = ({ domain, userAgent }) => ({
const invidious = async (instance, url) => {
const req = await instance({ method: 'GET', url });
if (req.statusText !== 'OK') throw req;
const { headers } = instance.defaults;
const video = JSON.parse(req.data);
return {
url: headers['Host'],
name: video.title,
date: video.publishedText,
description: video.descriptionHtml,
@ -18,8 +20,8 @@ const invidious = async (instance, url) => {
};
};
const card = (video, base, path) =>
`<a href="${base}/${path}"><b>${video.name}</a></b><blockquote><b><i>` +
const card = (video, path) =>
`<a href="https://${video.url}/${path}"><b>${video.name}</a></b><blockquote><b><i>` +
((video.description.length > 300) ? `${video.description.substr(0, 300)}&hellip;` : ``)+
((video.description === '<p></p>') ? `No description.`: ``)+
((video.description.length < 300 && video.description !== '<p></p>') ? `${video.description}` : ``)+
@ -29,16 +31,19 @@ const card = (video, base, path) =>
`<br />(${video.date})</b> <br />
</blockquote>`;
const run = async (roomId, userInput) => {
const instance = axios.create({
baseURL: `https://${config.invidious.domain}/api/v1/videos/`,
headers: headers(config.invidious),
transformResponse: [],
timeout: 10 * 1000,
});
const video = await invidious(instance, userInput);
return await matrixClient.sendHtmlNotice(roomId, '', card(video, `https://${config.invidious.domain}`, userInput));
};
const getInstance = config =>
axios.create({
baseURL: `https://${config.domain}/api/v1/videos`,
headers: headers(config),
transformResponse: [],
timeout: 10 * 1000,
});
const run = async (roomId, userInput) => {
const video = await invidious(getInstance(config.invidious), userInput)
.catch(_ => invidious(getInstance(Object.assign(config.invidious, { domain: config.invidious.fallback })), userInput));
return matrixClient.sendHtmlNotice(roomId, '', card(video, userInput));
};
exports.runQuery = async (roomId, event, userInput) => {
try {

View file

@ -15,7 +15,9 @@ const nitter = async (instance, url) => {
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');
const { defaults } = instance;
return {
url: defaults.baseURL,
text: tweet.querySelector('.tweet-body > .tweet-content').innerHTML,
date: tweet.querySelector('.tweet-body > .tweet-published').textContent,
name: tweet.querySelector('.tweet-body > div .fullname').textContent,
@ -38,27 +40,30 @@ const nitter = async (instance, url) => {
};
};
const card = (tweet, base, check, path) =>
`<a href="${base}/${tweet.handle.replace(/^@/, '')}"><b>${tweet.name}</b></a> ` +
const card = (tweet, check, path) =>
`<a href="${tweet.url}/${tweet.handle.replace(/^@/, '')}"><b>${tweet.name}</b></a> ` +
(tweet.check ? `${check} ` : '') +
`<a href="${base}${path}"><b>${tweet.date}</b></a> ` +
`<a href="${tweet.url}${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.isReply ? tweet.isReply === 'unavailable' ? '<blockquote>Replied Tweet is unavailable</blockquote>' : `<blockquote><b><a href="${base}${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="${base}${tweet.quote.path}">Quoted Tweet</a></b><br /><b><i>${tweet.quote.text.replace('\n', '<br />')}</i></b></blockquote>` : '');
(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>` : '');
const run = async (roomId, userInput) => {
const instance = axios.create({
baseURL: `https://${config.nitter.domain}`,
headers: headers(config.nitter),
const getInstance = config =>
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.nitter.domain}`, config.nitter.check, userInput));
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));
};
exports.runQuery = async (roomId, event, userInput) => {

View file

@ -24,12 +24,14 @@ module.exports = {
},
nitter: {
domain: 'nitter.fdn.fr',
fallback: 'nitter.snopyta.org',
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)',
domains: [ 'nitter.net', 'www.nitter.net', 'twitter.com', 'www.twitter.com', 'mobile.twitter.com', 'm.twitter.com', 'nitter.fdn.fr' ],
domains: [ 'nitter.snopyta.org', 'nitter.net', 'www.nitter.net', 'twitter.com', 'www.twitter.com', 'mobile.twitter.com', 'm.twitter.com', 'nitter.fdn.fr' ],
check: '(✅)'
},
invidious: {
domain: 'invidious.fdn.fr',
fallback: 'invidious.snopyta.org',
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)',
domains: [ 'invidious.snopyta.org', 'invidious.xyz', 'youtube.com', 'www.youtube.com', 'youtu.be', 'm.youtube.com', 'invidious.fdn.fr' ]
}