refactor(proxy): w/ cycling, rm fallback.
refactor(config): adjust for cycling.
This commit is contained in:
parent
2f58d6bb84
commit
b7e73fc533
5 changed files with 55 additions and 39 deletions
|
@ -1,8 +1,3 @@
|
||||||
const headers = ({ domain, userAgent }) => ({
|
|
||||||
Host: `${domain}`,
|
|
||||||
'User-Agent': `${userAgent}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
const invidious = async (instance, url) => {
|
const invidious = async (instance, url) => {
|
||||||
const req = await instance({ method: 'GET', url });
|
const req = await instance({ method: 'GET', url });
|
||||||
if (req.statusText !== 'OK') throw req;
|
if (req.statusText !== 'OK') throw req;
|
||||||
|
@ -31,24 +26,28 @@ const card = (video, path) =>
|
||||||
`<br />(${video.date})</b> <br />
|
`<br />(${video.date})</b> <br />
|
||||||
</blockquote>`;
|
</blockquote>`;
|
||||||
|
|
||||||
const getInstance = config =>
|
const getInstance = (domain, config) =>
|
||||||
axios.create({
|
axios.create({
|
||||||
baseURL: `https://${config.domain}/api/v1/videos`,
|
baseURL: `https://${domain}/api/v1/videos`,
|
||||||
headers: headers(config),
|
headers: {
|
||||||
transformResponse: [],
|
Host: `${domain}`,
|
||||||
timeout: 10 * 1000,
|
'User-Agent': `${config.userAgent}`,
|
||||||
});
|
},
|
||||||
|
transformResponse: [],
|
||||||
|
timeout: 10 * 1000,
|
||||||
|
});
|
||||||
|
|
||||||
const run = async (roomId, userInput) => {
|
const run = async (roomId, userInput) => {
|
||||||
const video = await invidious(getInstance(config.invidious), userInput)
|
const cfg = config.invidious;
|
||||||
.catch(_ => invidious(getInstance(Object.assign(config.invidious, { domain: config.invidious.fallback })), userInput));
|
const video = await matrix.utils.retryPromise(cfg.domains.redirect, domain => invidious(getInstance(domain, cfg), userInput));
|
||||||
return matrixClient.sendHtmlNotice(roomId, '', card(video, userInput));
|
return matrixClient.sendHtmlNotice(roomId, '', card(video, userInput));
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.runQuery = async (roomId, event, userInput) => {
|
exports.runQuery = async (roomId, event, userInput) => {
|
||||||
try {
|
try {
|
||||||
const url = new URL(userInput);
|
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));
|
if (/^\/[\w-]{11}$/.test(url.pathname)) return await run(roomId, url.pathname.slice(1));
|
||||||
const params = new URLSearchParams(url.search).get('v');
|
const params = new URLSearchParams(url.search).get('v');
|
||||||
if (!/^[\w-]{11}$/.test(params)) throw '';
|
if (!/^[\w-]{11}$/.test(params)) throw '';
|
||||||
|
|
|
@ -1,10 +1,5 @@
|
||||||
const { JSDOM } = require('jsdom');
|
const { JSDOM } = require('jsdom');
|
||||||
|
|
||||||
const headers = ({ domain, userAgent }) => ({
|
|
||||||
Host: `${domain}`,
|
|
||||||
'User-Agent': `${userAgent}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
const nitter = async (instance, url) => {
|
const nitter = async (instance, url) => {
|
||||||
const req = await instance({ method: 'GET', url });
|
const req = await instance({ method: 'GET', url });
|
||||||
if (req.statusText !== 'OK') throw req;
|
if (req.statusText !== 'OK') throw req;
|
||||||
|
@ -52,24 +47,28 @@ const card = (tweet, check, path) =>
|
||||||
(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.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>` : '');
|
(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 getInstance = config =>
|
const getInstance = (domain, config) =>
|
||||||
axios.create({
|
axios.create({
|
||||||
baseURL: `https://${config.domain}`,
|
baseURL: `https://${domain}`,
|
||||||
headers: headers(config),
|
headers: {
|
||||||
|
Host: `${domain}`,
|
||||||
|
'User-Agent': `${config.userAgent}`,
|
||||||
|
},
|
||||||
transformResponse: [],
|
transformResponse: [],
|
||||||
timeout: 10 * 1000,
|
timeout: 10 * 1000,
|
||||||
});
|
});
|
||||||
|
|
||||||
const run = async (roomId, userInput) => {
|
const run = async (roomId, userInput) => {
|
||||||
const tweet = await nitter(getInstance(config.nitter), userInput)
|
const cfg = config.nitter;
|
||||||
.catch(_ => nitter(getInstance(Object.assign(config.nitter, { domain: config.nitter.fallback })), userInput));
|
const tweet = await matrix.utils.retryPromise(cfg.domains.redirect, domain => nitter(getInstance(domain, cfg), userInput));
|
||||||
return matrixClient.sendHtmlNotice(roomId, '', card(tweet, config.nitter.check, userInput));
|
return matrixClient.sendHtmlNotice(roomId, '', card(tweet, cfg.check, userInput));
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.runQuery = async (roomId, event, userInput) => {
|
exports.runQuery = async (roomId, event, userInput) => {
|
||||||
try {
|
try {
|
||||||
const url = new URL(userInput);
|
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 '';
|
if (!/^\/[^/]+\/status\/\d+\/?$/.test(url.pathname)) throw '';
|
||||||
return await run(roomId, url.pathname);
|
return await run(roomId, url.pathname);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
4
commands/proxy.js
Normal file
4
commands/proxy.js
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
const msg = 'Invalid proxy domain!';
|
||||||
|
exports.runQuery = (roomId, event, userInput) =>
|
||||||
|
matrixClient.sendHtmlNotice(roomId, msg, `<b>${msg}</b>`).catch(() => {});
|
|
@ -23,16 +23,18 @@ module.exports = {
|
||||||
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)'
|
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)'
|
||||||
},
|
},
|
||||||
nitter: {
|
nitter: {
|
||||||
domain: 'nitter.fdn.fr',
|
|
||||||
fallback: 'nitter.snopyta.org',
|
|
||||||
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)',
|
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' ],
|
domains: {
|
||||||
check: '(✅)'
|
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: {
|
invidious: {
|
||||||
domain: 'invidious.fdn.fr',
|
|
||||||
fallback: 'invidious.snopyta.org',
|
|
||||||
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)',
|
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' ]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
16
utils.js
16
utils.js
|
@ -49,9 +49,11 @@ const eventHandler = (args, roomId, command, event) => {
|
||||||
case 'proxy': case 'p':
|
case 'proxy': case 'p':
|
||||||
try {
|
try {
|
||||||
const url = new URL(userInput);
|
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'
|
? 'invidious'
|
||||||
: config.nitter.domains.includes(url.hostname)
|
: nitter.redirect.includes(url.hostname) || nitter.original.includes(url.hostname)
|
||||||
? 'nitter'
|
? 'nitter'
|
||||||
: 'proxy';
|
: 'proxy';
|
||||||
} catch (e) { sendError(event, roomId, e); }
|
} catch (e) { sendError(event, roomId, e); }
|
||||||
|
@ -131,3 +133,13 @@ module.exports.selfReact = async (event) => {
|
||||||
if (config.fediverse.tipping === true) addReact(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');
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue