2020-06-25 14:32:36 +03:00
|
|
|
const invidious = async (instance, url) => {
|
|
|
|
const req = await instance({ method: 'GET', url });
|
|
|
|
if (req.statusText !== 'OK') throw req;
|
2021-06-07 10:30:40 +03:00
|
|
|
const { headers } = instance.defaults;
|
2020-06-25 14:32:36 +03:00
|
|
|
const video = JSON.parse(req.data);
|
|
|
|
return {
|
2021-06-07 10:30:40 +03:00
|
|
|
url: headers['Host'],
|
2020-06-25 14:32:36 +03:00
|
|
|
name: video.title,
|
|
|
|
date: video.publishedText,
|
|
|
|
description: video.descriptionHtml,
|
|
|
|
author: video.author,
|
|
|
|
views: video.viewCount,
|
|
|
|
likes: video.likeCount,
|
2021-02-14 10:57:35 +03:00
|
|
|
dislikes: video.dislikeCount,
|
2020-06-25 14:32:36 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-06-07 10:30:40 +03:00
|
|
|
const card = (video, path) =>
|
|
|
|
`<a href="https://${video.url}/${path}"><b>${video.name}</a></b><blockquote><b><i>` +
|
2020-06-25 14:32:36 +03:00
|
|
|
((video.description.length > 300) ? `${video.description.substr(0, 300)}…` : ``)+
|
|
|
|
((video.description === '<p></p>') ? `No description.`: ``)+
|
|
|
|
((video.description.length < 300 && video.description !== '<p></p>') ? `${video.description}` : ``)+
|
|
|
|
`<br /><span>🔍️ ${video.views.toLocaleString()}</span> ` +
|
|
|
|
`<span>❤️ ${video.likes.toLocaleString()}</span> ` +
|
|
|
|
`<span>❌ ${video.dislikes.toLocaleString()}</span>`+
|
|
|
|
`<br />(${video.date})</b> <br />
|
|
|
|
</blockquote>`;
|
|
|
|
|
2021-07-23 09:02:32 +03:00
|
|
|
const getInstance = (domain, config) =>
|
|
|
|
axios.create({
|
|
|
|
baseURL: `https://${domain}/api/v1/videos`,
|
|
|
|
headers: {
|
|
|
|
Host: `${domain}`,
|
|
|
|
'User-Agent': `${config.userAgent}`,
|
|
|
|
},
|
|
|
|
transformResponse: [],
|
|
|
|
timeout: 10 * 1000,
|
|
|
|
});
|
2021-06-07 10:30:40 +03:00
|
|
|
|
2021-07-23 09:02:32 +03:00
|
|
|
const run = async (roomId, userInput) => {
|
|
|
|
const cfg = config.invidious;
|
|
|
|
const video = await matrix.utils.retryPromise(cfg.domains.redirect, domain => invidious(getInstance(domain, cfg), userInput));
|
2022-04-26 06:09:55 +03:00
|
|
|
return matrixClient.sendHtmlNotice(roomId, ' ', card(video, userInput));
|
2021-07-23 09:02:32 +03:00
|
|
|
};
|
2020-06-25 14:32:36 +03:00
|
|
|
|
2021-02-14 10:57:35 +03:00
|
|
|
exports.runQuery = async (roomId, event, userInput) => {
|
2020-06-25 14:32:36 +03:00
|
|
|
try {
|
|
|
|
const url = new URL(userInput);
|
2021-07-23 09:02:32 +03:00
|
|
|
const { redirect, original } = config.invidious.domains;
|
|
|
|
if (!redirect.includes(url.hostname) && !original.includes(url.hostname)) throw '';
|
2021-02-14 10:57:35 +03:00
|
|
|
if (/^\/[\w-]{11}$/.test(url.pathname)) return await run(roomId, url.pathname.slice(1));
|
|
|
|
const params = new URLSearchParams(url.search).get('v');
|
|
|
|
if (!/^[\w-]{11}$/.test(params)) throw '';
|
|
|
|
return await run(roomId, params);
|
|
|
|
} catch (e) {
|
|
|
|
return matrixClient.sendHtmlNotice(roomId, 'Sad!', '<strong>Sad!</strong>').catch(() => {});
|
2020-06-25 14:32:36 +03:00
|
|
|
}
|
|
|
|
};
|