plugins-evert/urlreply/plugin.ts

385 lines
10 KiB
TypeScript
Raw Normal View History

2020-12-05 10:00:00 +00:00
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';
interface URLHandler {
plugin: string;
action: Function;
}
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;
}
// http://stackoverflow.com/a/22149575
function ytDuration(source: string): string {
const a = source.match(/\d+/g);
let res;
if (!a) {
return '';
}
if (source.indexOf('M') >= 0 && source.indexOf('H') === -1 && source.indexOf('S') === -1) {
res = [0, a[0], 0];
}
if (source.indexOf('H') >= 0 && source.indexOf('M') === -1) {
res = [a[0], 0, a[1]];
}
if (source.indexOf('H') >= 0 && source.indexOf('M') === -1 && source.indexOf('S') === -1) {
res = [a[0], 0, 0];
}
let duration = 0;
if (a.length === 3) {
duration = duration + parseInt(a[0], 10) * 3600;
duration = duration + parseInt(a[1], 10) * 60;
duration = duration + parseInt(a[2], 10);
}
if (a.length === 2) {
duration = duration + parseInt(a[0], 10) * 60;
duration = duration + parseInt(a[1], 10);
}
if (a.length === 1) {
duration = duration + parseInt(a[0], 10);
}
return toHHMMSS(duration.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');
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;
}
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' }]);
}
if (vid.statistics && vid.statistics.likeCount != null) {
const likeCount = vid.statistics.likeCount.toString();
const dislikeCount = vid.statistics.dislikeCount.toString();
keys.push(['field', likeCount, { color: 'limegreen', label: ['▲', 'Likes'], type: 'metric' }]);
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
}
})
class URLReplyPlugin extends Plugin {
registerHandler(plugin: string, match: string, handler: Function): 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, 'youtube.com/', 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.indexOf(handler) !== -1 || !obj.action) {
try {
const urlParsed = new urllib.URL(url);
matched = await obj.action.apply(this, [urlParsed, msg]);
} catch (e) {
logger.error(e.stack);
matched = false;
}
break;
}
}
// If there were no matches, pull the title of the website
if (!matched) {
try {
const data = await httpGET(url, {}, 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) {
logger.error(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;