plugins-evert/urlreply/plugin.ts

549 lines
12 KiB
TypeScript
Raw Normal View History

2020-12-05 10:00:00 +00:00
import {
2023-08-04 14:37:51 +00:00
httpGET,
parseTimeToSeconds,
toHHMMSS,
} from '@squeebot/core/lib/common';
import { logger } from '@squeebot/core/lib/core';
import { Plugin, Configurable, EventListener } from '@squeebot/core/lib/plugin';
2020-12-05 10:00:00 +00:00
import { IMessage } from '@squeebot/core/lib/types';
import cheerio from 'cheerio';
import * as urllib from 'url';
2021-12-15 16:38:18 +00:00
type ActionFn = (url: urllib.URL, msg: IMessage, data?: any) => Promise<any>;
2020-12-05 10:00:00 +00:00
interface URLHandler {
plugin: string;
2021-12-15 16:38:18 +00:00
action: ActionFn;
2020-12-05 10:00:00 +00:00
}
2023-08-04 14:37:51 +00:00
let urlHandlers: { [key: string]: URLHandler } = {};
2020-12-05 10:00:00 +00:00
let htmlHandlers: any[] = [];
2023-09-21 14:07:24 +00:00
const urlRegex = /(((ftp|https?):\/\/)[-\w@:%_+.~#?,&//=()]+)/g;
2020-12-05 10:00:00 +00:00
function findUrls(text: string): string[] {
const source = (text || '').toString();
const urlArray = [];
let matchArray = urlRegex.exec(source);
while (matchArray !== null) {
urlArray.push(matchArray[0]);
matchArray = urlRegex.exec(source);
}
return urlArray;
}
// https://gist.github.com/denniszhao/8972cd4ae637cf10fe01
function ytDuration(time: string): string {
const a = time.match(/\d+H|\d+M|\d+S/g);
2020-12-05 10:00:00 +00:00
let result = 0;
2020-12-05 10:00:00 +00:00
2023-08-04 14:37:51 +00:00
const d: { [key: string]: number } = { H: 3600, M: 60, S: 1 };
let num;
let type;
2020-12-05 10:00:00 +00:00
for (const char of a || []) {
num = char.slice(0, char.length - 1);
type = char.slice(char.length - 1, char.length);
2020-12-05 10:00:00 +00:00
result += parseInt(num, 10) * d[type];
2020-12-05 10:00:00 +00:00
}
return toHHMMSS(result.toString());
2020-12-05 10:00:00 +00:00
}
async function dailymotionFromId(id: string, msg: IMessage): Promise<boolean> {
2023-08-04 14:37:51 +00:00
const url = `https://api.dailymotion.com/video/${id}?fields=id,title,owner,owner.screenname,duration,views_total`;
2020-12-05 10:00:00 +00:00
let data = await httpGET(url);
try {
data = JSON.parse(data);
} catch (e) {
return false;
}
const keys = [];
keys.push(['field', 'Dailymotion', { type: 'title' }]);
keys.push(['field', data.title, { type: 'description' }]);
2023-08-04 14:37:51 +00:00
keys.push([
'field',
data.views_total.toString(),
{ label: 'Views', type: 'metric' },
]);
keys.push([
'field',
data.duration.toString(),
{ label: 'Duration', type: 'duration' },
]);
2020-12-05 10:00:00 +00:00
keys.push(['field', data['owner.screenname'], { label: ['By'] }]);
msg.resolve(keys);
return true;
}
2023-08-04 14:37:51 +00:00
async function getSoundcloudFromUrl(
plugin: URLReplyPlugin,
url: string,
msg: IMessage
): Promise<boolean> {
2020-12-05 10:00:00 +00:00
const token = plugin.config.get('tokens.soundcloud');
if (!token) {
return false;
}
2023-08-04 14:37:51 +00:00
const sAPI = `https://api.soundcloud.com/resolve?url=${encodeURIComponent(
url
)}&client_id=${token}`;
2020-12-05 10:00:00 +00:00
let data = await httpGET(sAPI);
try {
data = JSON.parse(data);
} catch (e) {
return false;
}
if (!data) {
return false;
}
const keys = [];
2023-08-04 14:37:51 +00:00
keys.push([
'field',
'SoundCloud' + (data.kind === 'playlist' ? ' Playlist' : ''),
{ type: 'title', color: 'gold' },
]);
2020-12-05 10:00:00 +00:00
keys.push(['field', data.title, { type: 'description' }]);
if (data.kind === 'track') {
2023-08-04 14:37:51 +00:00
keys.push([
'field',
data.playback_count.toString(),
{ color: 'green', label: ['▶', 'Plays'], type: 'metric' },
]);
keys.push([
'field',
data.favoritings_count.toString(),
{ color: 'red', label: ['♥', 'Favourites'], type: 'metric' },
]);
2020-12-05 10:00:00 +00:00
} else {
2023-08-04 14:37:51 +00:00
keys.push([
'field',
data.track_count.toString(),
{ label: 'Tracks', type: 'metric' },
]);
2020-12-05 10:00:00 +00:00
}
2023-08-04 14:37:51 +00:00
keys.push([
'field',
Math.floor(data.duration / 1000).toString(),
{ label: 'Duration', type: 'duration' },
]);
2020-12-05 10:00:00 +00:00
keys.push(['field', data.user.username, { label: ['By'] }]);
msg.resolve(keys);
return true;
}
async function getYoutubeFromVideo(
plugin: URLReplyPlugin,
id: string,
full: urllib.URL,
2023-08-04 14:37:51 +00:00
msg: IMessage
): Promise<boolean> {
2020-12-05 10:00:00 +00:00
const gtoken = plugin.config.get('tokens.google');
2022-12-01 11:02:54 +00:00
const dislikeAPIBase = plugin.config.get('api.returnyoutubedislike');
2020-12-05 10:00:00 +00:00
if (!gtoken) {
return false;
}
2023-08-04 14:37:51 +00:00
const gAPI = `https://www.googleapis.com/youtube/v3/videos?id=${id}&key=${gtoken}&part=snippet,contentDetails,statistics`;
2020-12-05 10:00:00 +00:00
let data = await httpGET(gAPI);
try {
data = JSON.parse(data);
} catch (e) {
return false;
}
if (!data || !data.items || !data.items.length) {
msg.resolve('Video does not exist or is private.');
return false;
}
2021-12-15 16:38:18 +00:00
let dislikeData;
2022-12-01 11:02:54 +00:00
if (dislikeAPIBase) {
const dislikeAPI = `${dislikeAPIBase}/votes?videoId=${id}`;
try {
dislikeData = await httpGET(dislikeAPI);
dislikeData = JSON.parse(dislikeData);
} catch (e) {
dislikeData = null;
}
2021-12-14 16:03:29 +00:00
}
2020-12-05 10:00:00 +00:00
const vid = data.items[0];
const time = full.searchParams.get('t');
let live = false;
if (!vid.snippet) {
return false;
}
const keys = [];
if (vid.snippet.liveBroadcastContent === 'live') {
live = true;
2023-08-04 14:37:51 +00:00
keys.push(['field', '[LIVE]', { color: 'red' }]);
2020-12-05 10:00:00 +00:00
}
if (time) {
let tag = parseInt(time, 10);
if (tag != null && !isNaN(tag)) {
2023-08-04 14:37:51 +00:00
if (
time.indexOf('s') !== -1 ||
time.indexOf('m') !== -1 ||
time.indexOf('h') !== -1
) {
2020-12-05 10:00:00 +00:00
tag = parseTimeToSeconds(time);
}
2023-08-04 14:37:51 +00:00
keys.push(['field', `[${toHHMMSS(tag)}]`, { color: 'red' }]);
2020-12-05 10:00:00 +00:00
}
}
2023-08-04 14:37:51 +00:00
keys.push([
'field',
msg.source.format.color('white', 'You') +
msg.source.format.color('brown', 'Tube'),
{ type: 'title' },
]);
keys.push([
'field',
msg.source.format.escape(vid.snippet.title),
{ type: 'description' },
]);
keys.push([
'field',
vid.statistics.viewCount.toString(),
{ type: 'metric', label: 'Views' },
]);
2020-12-05 10:00:00 +00:00
if (!live) {
2023-08-04 14:37:51 +00:00
keys.push([
'field',
ytDuration(vid.contentDetails.duration.toString()),
{ label: 'Duration' },
]);
2020-12-05 10:00:00 +00:00
}
2021-12-14 16:03:29 +00:00
let likeCount: number | null = null;
let dislikeCount: number | null = null;
2020-12-05 10:00:00 +00:00
if (vid.statistics && vid.statistics.likeCount != null) {
2021-12-14 16:03:29 +00:00
likeCount = vid.statistics.likeCount;
}
2020-12-05 10:00:00 +00:00
2021-12-14 16:03:29 +00:00
if (dislikeData) {
dislikeCount = dislikeData.dislikes;
if (likeCount === null) {
likeCount = dislikeData.likes;
}
}
if (likeCount !== null) {
2023-08-04 14:37:51 +00:00
keys.push([
'field',
likeCount,
{ color: 'limegreen', label: ['▲', 'Likes'], type: 'metric' },
]);
2021-12-14 16:03:29 +00:00
}
if (dislikeCount !== null) {
2023-08-04 14:37:51 +00:00
keys.push([
'field',
dislikeCount,
{ color: 'red', label: ['▼', 'Dislikes'], type: 'metric' },
]);
2020-12-05 10:00:00 +00:00
}
keys.push(['field', vid.snippet.channelTitle, { label: ['By'] }]);
msg.resolve(keys);
return true;
}
@Configurable({
tokens: {
google: null,
2023-08-04 14:37:51 +00:00
soundcloud: null,
2022-12-01 11:02:54 +00:00
},
api: {
2023-08-04 14:37:51 +00:00
returnyoutubedislike: 'https://returnyoutubedislikeapi.com',
},
2020-12-05 10:00:00 +00:00
})
class URLReplyPlugin extends Plugin {
2021-12-15 16:38:18 +00:00
registerHandler(plugin: string, match: string, handler: ActionFn): void {
2020-12-05 10:00:00 +00:00
if (!handler || typeof handler !== 'function') {
throw new Error('Expected handler function as third argument.');
}
if (plugin !== 'urlreply') {
logger.log('[urlreply] %s has added an handler for "%s"', plugin, match);
}
if (match.indexOf('html') === 0) {
htmlHandlers.push([plugin, handler]);
} else {
urlHandlers[match] = {
2023-08-04 14:37:51 +00:00
plugin,
action: handler,
2020-12-05 10:00:00 +00:00
};
}
}
resetHandlers(): void {
urlHandlers = {};
htmlHandlers = [];
// YouTube
2023-08-04 14:37:51 +00:00
this.registerHandler(
this.name,
'/watch\\?v=',
async (url: urllib.URL, msg: IMessage, data: any) => {
const det = url.searchParams.get('v');
if (!det) {
return false;
}
2020-12-05 10:00:00 +00:00
2023-08-04 14:37:51 +00:00
return getYoutubeFromVideo.apply(this, [this, det, url, msg]);
2020-12-05 10:00:00 +00:00
}
2023-08-04 14:37:51 +00:00
);
2020-12-05 10:00:00 +00:00
2023-08-04 14:37:51 +00:00
this.registerHandler(
this.name,
'youtu.be/',
async (url: urllib.URL, msg: IMessage, data: any) => {
const det = url.pathname.substring(1);
2020-12-05 10:00:00 +00:00
2023-08-04 14:37:51 +00:00
if (!det) {
return false;
}
2020-12-05 10:00:00 +00:00
2023-08-04 14:37:51 +00:00
return getYoutubeFromVideo.apply(this, [this, det, url, msg]);
2020-12-05 10:00:00 +00:00
}
2023-08-04 14:37:51 +00:00
);
2020-12-05 10:00:00 +00:00
2023-09-21 14:07:24 +00:00
this.registerHandler(
this.name,
'youtube.com/shorts/',
async (url: urllib.URL, msg: IMessage, data: any) => {
const det = url.pathname.split('/')[2];
if (!det) {
return false;
}
return getYoutubeFromVideo.apply(this, [this, det, url, msg]);
}
);
this.registerHandler(
this.name,
'youtube.com/live/',
async (url: urllib.URL, msg: IMessage, data: any) => {
const det = url.pathname.split('/')[2];
if (!det) {
return false;
}
return getYoutubeFromVideo.apply(this, [this, det, url, msg]);
}
);
this.registerHandler(
this.name,
'youtube.com/v/',
async (url: urllib.URL, msg: IMessage, data: any) => {
const det = url.pathname.split('/')[2];
if (!det) {
return false;
}
return getYoutubeFromVideo.apply(this, [this, det, url, msg]);
}
);
2020-12-05 10:00:00 +00:00
// Dailymotion
2023-08-04 14:37:51 +00:00
this.registerHandler(
this.name,
'dailymotion.com/video/',
async (url: urllib.URL, msg: IMessage, data: any) => {
const det = url.href.match('/video/([^?&#]*)');
if (!det) {
return false;
}
2020-12-05 10:00:00 +00:00
2023-08-04 14:37:51 +00:00
return dailymotionFromId.apply(this, [det[1], msg]);
2020-12-05 10:00:00 +00:00
}
2023-08-04 14:37:51 +00:00
);
2020-12-05 10:00:00 +00:00
// SoundCloud
2023-08-04 14:37:51 +00:00
this.registerHandler(
this.name,
'soundcloud.com/',
async (url: urllib.URL, msg: IMessage, data: any) => {
return getSoundcloudFromUrl.apply(this, [this, url.href, msg]);
}
);
2020-12-05 10:00:00 +00:00
}
@EventListener('pluginUnload')
public unloadEventHandler(plugin: string | Plugin): void {
if (plugin === this.name || plugin === this) {
this.emit('pluginUnloaded', this);
}
}
initialize(): void {
this.resetHandlers();
this.on('message', async (msg: IMessage) => {
// Find URLS in the message
const urls = findUrls(msg.text);
if (!urls.length) {
return;
}
const url = urls[0];
let matched = false;
// Find handlers matching this URL
for (const handler in urlHandlers) {
const obj = urlHandlers[handler];
2022-10-04 15:51:58 +00:00
if (!!url.match(handler) || !obj.action) {
2020-12-05 10:00:00 +00:00
try {
const urlParsed = new urllib.URL(url);
matched = await obj.action.apply(this, [urlParsed, msg]);
} catch (e: any) {
2020-12-05 10:00:00 +00:00
logger.error(e.stack);
matched = false;
}
2022-10-04 15:51:58 +00:00
}
if (matched) {
2020-12-05 10:00:00 +00:00
break;
}
}
// If there were no matches, pull the title of the website
if (!matched) {
try {
2023-08-04 14:37:51 +00:00
const data = await httpGET(
url,
{
Accept: 'text/html,application/xhtml+xml,application/xml',
},
true
);
if (!data) {
return;
}
2020-12-05 10:00:00 +00:00
const full = cheerio.load(data);
const head = full('head');
2023-08-04 14:37:51 +00:00
if (!head) {
return;
}
2020-12-05 10:00:00 +00:00
const titleEl = head.find('title');
2023-08-04 14:37:51 +00:00
if (!titleEl || !titleEl.length) {
return;
}
2020-12-05 10:00:00 +00:00
let title = titleEl.eq(0).text();
title = title.replace(/\n/g, ' ').trim();
title = msg.source.format.strip(title);
// Try HTML handlers
let htmlHandle = false;
for (const k in htmlHandlers) {
const handler = htmlHandlers[k];
2023-08-04 14:37:51 +00:00
if (htmlHandle) {
break;
}
2020-12-05 10:00:00 +00:00
// Ensure handler is a function
2023-08-04 14:37:51 +00:00
if (!handler[1] || typeof handler[1] !== 'function') {
continue;
}
2020-12-05 10:00:00 +00:00
try {
2023-08-04 14:37:51 +00:00
htmlHandle = await handler[1].apply(this, [
url,
msg,
title,
full,
]);
2020-12-05 10:00:00 +00:00
} catch (e) {
logger.error(e);
htmlHandle = false;
}
}
2023-08-04 14:37:51 +00:00
if (htmlHandle) {
return;
}
if (title.length > 140) {
title = title.substring(0, 140) + '…';
}
2020-12-05 10:00:00 +00:00
2023-08-04 14:37:51 +00:00
msg.resolve(
msg.source.format.color('purple', '[ ') +
title +
msg.source.format.color('purple', ' ]')
);
} catch (e) {}
2020-12-05 10:00:00 +00:00
}
});
}
@EventListener('pluginUnloaded')
unloadedPlugin(plugin: string | Plugin): void {
const id = typeof plugin === 'string' ? plugin : plugin.manifest.name;
// Delete URL handlers for plugin
for (const i in urlHandlers) {
if (urlHandlers[i].plugin === id) {
delete urlHandlers[i];
}
}
// Delete HTML handlers for plugin
const resolve = [];
for (const i in htmlHandlers) {
const k = htmlHandlers[i];
if (k[0] !== id) {
resolve.push(k);
}
}
htmlHandlers = resolve;
}
}
module.exports = URLReplyPlugin;