plugins-evert/urlreply/plugin.ts

397 lines
11 KiB
TypeScript

import { httpGET, parseTimeToSeconds, toHHMMSS } from '@squeebot/core/lib/common';
import { logger } from '@squeebot/core/lib/core';
import {
Plugin,
Configurable,
EventListener,
} from '@squeebot/core/lib/plugin';
import { IMessage } from '@squeebot/core/lib/types';
import cheerio from 'cheerio';
import * as urllib from 'url';
type ActionFn = (url: urllib.URL, msg: IMessage, data?: any) => Promise<any>;
interface URLHandler {
plugin: string;
action: ActionFn;
}
let urlHandlers: {[key: string]: URLHandler} = {};
let htmlHandlers: any[] = [];
const urlRegex = /(((ftp|https?):\/\/)[-\w@:%_+.~#?,&//=]+)/g;
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);
let result = 0;
const d: {[key: string]: number} = { H: 3600, M: 60, S: 1 };
let num;
let type;
for (const char of a || []) {
num = char.slice(0, char.length - 1);
type = char.slice(char.length - 1, char.length);
result += parseInt(num, 10) * d[type];
}
return toHHMMSS(result.toString());
}
async function dailymotionFromId(id: string, msg: IMessage): Promise<boolean> {
const url = 'https://api.dailymotion.com/video/' + id + '?fields=id,title,owner,owner.screenname,duration,views_total';
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' }]);
keys.push(['field', data.views_total.toString(), { label: 'Views', type: 'metric' }]);
keys.push(['field', data.duration.toString(), { label: 'Duration', type: 'duration' }]);
keys.push(['field', data['owner.screenname'], { label: ['By'] }]);
msg.resolve(keys);
return true;
}
async function getSoundcloudFromUrl(plugin: URLReplyPlugin, url: string, msg: IMessage): Promise<boolean> {
const token = plugin.config.get('tokens.soundcloud');
if (!token) {
return false;
}
const sAPI = 'https://api.soundcloud.com/resolve?url=' + encodeURIComponent(url) + '&client_id=' + token;
let data = await httpGET(sAPI);
try {
data = JSON.parse(data);
} catch (e) {
return false;
}
if (!data) {
return false;
}
const keys = [];
keys.push(['field', 'SoundCloud' + ((data.kind === 'playlist') ? ' Playlist' : ''), { type: 'title', color: 'gold' }]);
keys.push(['field', data.title, { type: 'description' }]);
if (data.kind === 'track') {
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' }]);
} else {
keys.push(['field', data.track_count.toString(), { label: 'Tracks', type: 'metric' }]);
}
keys.push(['field', Math.floor(data.duration / 1000).toString(), { label: 'Duration', type: 'duration' }]);
keys.push(['field', data.user.username, { label: ['By'] }]);
msg.resolve(keys);
return true;
}
async function getYoutubeFromVideo(
plugin: URLReplyPlugin,
id: string,
full: urllib.URL,
msg: IMessage): Promise<boolean> {
const gtoken = plugin.config.get('tokens.google');
const dislikeAPIBase = plugin.config.get('api.returnyoutubedislike');
if (!gtoken) {
return false;
}
const gAPI = 'https://www.googleapis.com/youtube/v3/videos?id=' + id + '&key=' +
gtoken + '&part=snippet,contentDetails,statistics';
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;
}
let dislikeData;
if (dislikeAPIBase) {
const dislikeAPI = `${dislikeAPIBase}/votes?videoId=${id}`;
try {
dislikeData = await httpGET(dislikeAPI);
dislikeData = JSON.parse(dislikeData);
} catch (e) {
dislikeData = null;
}
}
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;
keys.push(['field', '[LIVE]', {color: 'red'}]);
}
if (time) {
let tag = parseInt(time, 10);
if (tag != null && !isNaN(tag)) {
if (time.indexOf('s') !== -1 || time.indexOf('m') !== -1 || time.indexOf('h') !== -1) {
tag = parseTimeToSeconds(time);
}
keys.push(['field', `[${toHHMMSS(tag)}]`, {color: 'red'}]);
}
}
keys.push(['field', 'You' + msg.source.format.color('brown', 'Tube'), { color: 'white', type: 'title' }]);
keys.push(['field', vid.snippet.title, {type: 'description'}]);
keys.push(['field', vid.statistics.viewCount.toString(), { type: 'metric', label: 'Views' }]);
if (!live) {
keys.push(['field', ytDuration(vid.contentDetails.duration.toString()), { label: 'Duration' }]);
}
let likeCount: number | null = null;
let dislikeCount: number | null = null;
if (vid.statistics && vid.statistics.likeCount != null) {
likeCount = vid.statistics.likeCount;
}
if (dislikeData) {
dislikeCount = dislikeData.dislikes;
if (likeCount === null) {
likeCount = dislikeData.likes;
}
}
if (likeCount !== null) {
keys.push(['field', likeCount, { color: 'limegreen', label: ['▲', 'Likes'], type: 'metric' }]);
}
if (dislikeCount !== null) {
keys.push(['field', dislikeCount, { color: 'red', label: ['▼', 'Dislikes'], type: 'metric' }]);
}
keys.push(['field', vid.snippet.channelTitle, { label: ['By'] }]);
msg.resolve(keys);
return true;
}
@Configurable({
tokens: {
google: null,
soundcloud: null
},
api: {
returnyoutubedislike: 'https://returnyoutubedislikeapi.com'
}
})
class URLReplyPlugin extends Plugin {
registerHandler(plugin: string, match: string, handler: ActionFn): void {
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] = {
plugin, action: handler
};
}
}
resetHandlers(): void {
urlHandlers = {};
htmlHandlers = [];
// YouTube
this.registerHandler(this.name, '/watch\\?v=', async (url: urllib.URL, msg: IMessage, data: any) => {
const det = url.searchParams.get('v');
if (!det) {
return false;
}
return getYoutubeFromVideo.apply(this, [this, det, url, msg]);
});
this.registerHandler(this.name, 'youtu.be/', async (url: urllib.URL, msg: IMessage, data: any) => {
const det = url.pathname.substring(1);
if (!det) {
return false;
}
return getYoutubeFromVideo.apply(this, [this, det, url, msg]);
});
// Dailymotion
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;
}
return dailymotionFromId.apply(this, [det[1], msg]);
});
// SoundCloud
this.registerHandler(this.name, 'soundcloud.com/', async (url: urllib.URL, msg: IMessage, data: any) => {
return getSoundcloudFromUrl.apply(this, [this, url.href, msg]);
});
}
@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];
if (!!url.match(handler) || !obj.action) {
try {
const urlParsed = new urllib.URL(url);
matched = await obj.action.apply(this, [urlParsed, msg]);
} catch (e: any) {
logger.error(e.stack);
matched = false;
}
}
if (matched) {
break;
}
}
// If there were no matches, pull the title of the website
if (!matched) {
try {
const data = await httpGET(url, {
Accept: 'text/html,application/xhtml+xml,application/xml'
}, true);
if (!data) { return; }
const full = cheerio.load(data);
const head = full('head');
if (!head) { return; }
const titleEl = head.find('title');
if (!titleEl || !titleEl.length) { return; }
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];
if (htmlHandle) { break; }
// Ensure handler is a function
if (!handler[1] || typeof handler[1] !== 'function') { continue; }
try {
htmlHandle = await handler[1].apply(this, [url, msg, title, full]);
} catch (e) {
logger.error(e);
htmlHandle = false;
}
}
if (htmlHandle) { return; }
if (title.length > 140) { title = title.substring(0, 140) + '…'; }
msg.resolve(msg.source.format.color('purple', '[ ') + title + msg.source.format.color('purple', ' ]'));
} catch (e) {}
}
});
}
@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;