diff --git a/commands/invidious.js b/commands/invidious.js
index 0b49329..90d6dd4 100644
--- a/commands/invidious.js
+++ b/commands/invidious.js
@@ -1,8 +1,3 @@
-const headers = ({ domain, userAgent }) => ({
- Host: `${domain}`,
- 'User-Agent': `${userAgent}`,
-});
-
const invidious = async (instance, url) => {
const req = await instance({ method: 'GET', url });
if (req.statusText !== 'OK') throw req;
@@ -31,24 +26,28 @@ const card = (video, path) =>
`
(${video.date})
`;
- const getInstance = config =>
- axios.create({
- baseURL: `https://${config.domain}/api/v1/videos`,
- headers: headers(config),
- transformResponse: [],
- timeout: 10 * 1000,
- });
+const getInstance = (domain, config) =>
+ axios.create({
+ baseURL: `https://${domain}/api/v1/videos`,
+ headers: {
+ Host: `${domain}`,
+ 'User-Agent': `${config.userAgent}`,
+ },
+ 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));
- };
+const run = async (roomId, userInput) => {
+ const cfg = config.invidious;
+ const video = await matrix.utils.retryPromise(cfg.domains.redirect, domain => invidious(getInstance(domain, cfg), userInput));
+ return matrixClient.sendHtmlNotice(roomId, '', card(video, userInput));
+};
exports.runQuery = async (roomId, event, userInput) => {
try {
const url = new URL(userInput);
- if (!config.invidious.domains.includes(url.hostname)) throw '';
+ const { redirect, original } = config.invidious.domains;
+ if (!redirect.includes(url.hostname) && !original.includes(url.hostname)) throw '';
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 '';
diff --git a/commands/nitter.js b/commands/nitter.js
index aa470e6..5651b59 100644
--- a/commands/nitter.js
+++ b/commands/nitter.js
@@ -1,10 +1,5 @@
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;
@@ -52,24 +47,28 @@ const card = (tweet, check, path) =>
(tweet.isReply ? tweet.isReply === 'unavailable' ? '
Replied Tweet is unavailable' : `
Replied Tweet` : '') + (tweet.quote ? `
${tweet.isReply.text.replace('\n', '
')}
Quoted Tweet` : ''); -const getInstance = config => +const getInstance = (domain, config) => axios.create({ - baseURL: `https://${config.domain}`, - headers: headers(config), + baseURL: `https://${domain}`, + headers: { + Host: `${domain}`, + 'User-Agent': `${config.userAgent}`, + }, transformResponse: [], timeout: 10 * 1000, }); 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)); + const cfg = config.nitter; + const tweet = await matrix.utils.retryPromise(cfg.domains.redirect, domain => nitter(getInstance(domain, cfg), userInput)); + return matrixClient.sendHtmlNotice(roomId, '', card(tweet, cfg.check, userInput)); }; exports.runQuery = async (roomId, event, userInput) => { try { const url = new URL(userInput); - if (!config.nitter.domains.includes(url.hostname)) throw ''; + const { redirect, original } = config.nitter.domains; + if (!redirect.includes(url.hostname) && !original.includes(url.hostname)) throw ''; if (!/^\/[^/]+\/status\/\d+\/?$/.test(url.pathname)) throw ''; return await run(roomId, url.pathname); } catch (e) { diff --git a/commands/proxy.js b/commands/proxy.js new file mode 100644 index 0000000..0b5b431 --- /dev/null +++ b/commands/proxy.js @@ -0,0 +1,4 @@ + +const msg = 'Invalid proxy domain!'; +exports.runQuery = (roomId, event, userInput) => + matrixClient.sendHtmlNotice(roomId, msg, `${msg}`).catch(() => {}); diff --git a/config.example.js b/config.example.js index a216d7d..26e6f4d 100644 --- a/config.example.js +++ b/config.example.js @@ -23,16 +23,18 @@ module.exports = { userAgent: 'Mozilla/4.0 (compatible; Beep Boop)' }, nitter: { - domain: 'nitter.fdn.fr', - fallback: 'nitter.snopyta.org', userAgent: 'Mozilla/4.0 (compatible; Beep Boop)', - domains: [ 'nitter.snopyta.org', 'nitter.net', 'www.nitter.net', 'twitter.com', 'www.twitter.com', 'mobile.twitter.com', 'm.twitter.com', 'nitter.fdn.fr' ], - check: '(✅)' + domains: { + redirect: [ 'nitter.fdn.fr', 'nitter.snopyta.org', 'nitter.net' ], + original: [ 'nitter.net', 'www.nitter.net', 'twitter.com', 'www.twitter.com', 'mobile.twitter.com', 'm.twitter.com' ] + }, + 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' ] + domains: { + redirect: [ 'invidious.fdn.fr', 'invidious.snopyta.org', 'invidious.xyz', 'inv.riverside.rocks', 'vid.puffyan.us', 'y.com.cm' ], + original: [ 'youtube.com', 'www.youtube.com', 'youtu.be', 'm.youtube.com' ] + } } }; diff --git a/utils.js b/utils.js index ac60bb7..5bde1d2 100644 --- a/utils.js +++ b/utils.js @@ -49,9 +49,11 @@ const eventHandler = (args, roomId, command, event) => { case 'proxy': case 'p': try { const url = new URL(userInput); - command = config.invidious.domains.includes(url.hostname) + const invidio = config.invidious.domains; + const nitter = config.nitter.domains; + command = invidio.redirect.includes(url.hostname) || invidio.original.includes(url.hostname) ? 'invidious' - : config.nitter.domains.includes(url.hostname) + : nitter.redirect.includes(url.hostname) || nitter.original.includes(url.hostname) ? 'nitter' : 'proxy'; } catch (e) { sendError(event, roomId, e); } @@ -131,3 +133,13 @@ module.exports.selfReact = async (event) => { if (config.fediverse.tipping === true) addReact(event, '🌧️'); } }; + +module.exports.retryPromise = async (argList, promiseFn) => { + let err; + for(var arg of argList) { + try { + return await promiseFn(arg); + } catch(e) { err = e; } + } + throw err || new Error('retryPromise error'); +};
${tweet.quote.text.replace('\n', '
')}