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 req = await instance({ method: 'GET', url });
|
||||
if (req.statusText !== 'OK') throw req;
|
||||
|
@ -31,24 +26,28 @@ const card = (video, path) =>
|
|||
`<br />(${video.date})</b> <br />
|
||||
</blockquote>`;
|
||||
|
||||
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 '';
|
||||
|
|
|
@ -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' ? '<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 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) {
|
||||
|
|
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(() => {});
|
Loading…
Add table
Add a link
Reference in a new issue