From 34b1f1b9d8d840dcbc9d9f718d407a2467ad658c Mon Sep 17 00:00:00 2001 From: vulet Date: Thu, 25 Jun 2020 19:32:36 +0800 Subject: [PATCH] feat(cmd): Add Invidious --- commands/invidious.js | 56 +++++++++++++++++++++++++++++++++++++++++++ config.js | 5 ++++ main.js | 4 ++++ registrar.js | 1 + 4 files changed, 66 insertions(+) create mode 100644 commands/invidious.js diff --git a/commands/invidious.js b/commands/invidious.js new file mode 100644 index 0000000..f47d618 --- /dev/null +++ b/commands/invidious.js @@ -0,0 +1,56 @@ +const axios = require('axios'); + +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; + const video = JSON.parse(req.data); + return { + name: video.title, + date: video.publishedText, + description: video.descriptionHtml, + author: video.author, + views: video.viewCount, + likes: video.likeCount, + dislikes: video.dislikeCount + }; +}; + +const card = (video, base, path) => +`${video.name}
` + +((video.description.length > 300) ? `${video.description.substr(0, 300)}…` : ``)+ +((video.description === '

') ? `No description.`: ``)+ +((video.description.length < 300 && video.description !== '

') ? `${video.description}` : ``)+ +`
🔍️ ${video.views.toLocaleString()} ` + +`❤️ ${video.likes.toLocaleString()} ` + +`❌ ${video.dislikes.toLocaleString()}`+ +`
(${video.date})

+
`; + +const run = async (matrixClient, { roomId }, userInput, registrar) => { + const config = registrar.config.invidious; + const instance = axios.create({ + baseURL: `https://${config.domain}/api/v1/videos/`, + headers: headers(config), + transformResponse: [], + timeout: 10 * 1000 + }); + const video = await invidious(instance, userInput); + return await matrixClient.sendHtmlNotice(roomId, '', card(video, `https://${config.domain}`, userInput)); +} + +exports.runQuery = async (client, room, userInput, registrar) => { + try { + const url = new URL(userInput); + if(!registrar.config.invidious.domains.includes(url.hostname)) throw ''; + const params = new URLSearchParams(url.search).get("v"); + if(!/([a-z0-9_-]{11})?$/.test(params)) throw ''; + return await run(client, room, params, registrar); + } catch(e) { + return client.sendHtmlNotice(room.roomId, 'Sad!', `Sad!`).catch(()=>{}); + } +}; diff --git a/config.js b/config.js index e3ec895..f391a5e 100644 --- a/config.js +++ b/config.js @@ -13,5 +13,10 @@ module.exports = { domain: 'nitter.net', userAgent: 'Mozilla/4.0 (compatible; Beep Boop)', domains: [ 'nitter.net', 'twitter.com' ] + }, + invidious: { + domain: 'invidio.us', + userAgent: 'Mozilla/4.0 (compatible; Beep Boop)', + domains: [ 'invidio.us', 'youtube.com' ] } }; diff --git a/main.js b/main.js index a402f73..35bb0a1 100644 --- a/main.js +++ b/main.js @@ -118,6 +118,10 @@ let CreateClient = (token) => { if (command === 'nitter') { registrar.nitter.runQuery(matrixClient, room, userInput, registrar); } + + if (command === 'invidious') { + registrar.invidious.runQuery(matrixClient, room, userInput, registrar); + } } }); diff --git a/registrar.js b/registrar.js index ac9fc6e..af004af 100644 --- a/registrar.js +++ b/registrar.js @@ -18,4 +18,5 @@ module.exports = { mordy: require('./commands/mordy.js'), archive: require('./commands/archive.js'), nitter: require('./commands/nitter.js'), + invidious: require('./commands/invidious.js'), };