mrmBot-Matrix/utils/imagedetect.js

119 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-09-13 20:02:41 +00:00
const fetch = require("node-fetch");
2020-11-20 21:16:52 +00:00
const url = require("url");
const { getType } = require("./image.js");
const execPromise = require("util").promisify(require("child_process").exec);
2019-09-13 20:02:41 +00:00
2020-11-20 21:16:52 +00:00
const tenorURLs = [
"tenor.com",
"www.tenor.com"
];
const giphyURLs = [
"giphy.com",
"www.giphy.com"
];
const imgurURLs = [
"imgur.com",
"www.imgur.com",
"i.imgur.com"
];
const gfycatURLs = [
"gfycat.com",
"www.gfycat.com",
"thumbs.gfycat.com",
"giant.gfycat.com"
];
2020-11-20 21:16:52 +00:00
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
// gets the proper image paths
const getImage = async (image, image2, gifv = false) => {
2019-09-13 20:02:41 +00:00
try {
2020-10-19 23:48:43 +00:00
const payload = {
url: image2,
path: image
};
if (gifv) {
2020-11-20 21:16:52 +00:00
const host = url.parse(image2).host;
if (tenorURLs.includes(host)) {
if (process.env.TENOR !== "") {
const data = await fetch(`https://api.tenor.com/v1/gifs?ids=${image2.split("-").pop()}&key=${process.env.TENOR}`);
const json = await data.json();
payload.path = json.results[0].media[0].gif.url;
} else {
const delay = (await execPromise(`ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate ${image}`)).stdout.replace("\n", "");
payload.delay = (100 / delay.split("/")[0]) * delay.split("/")[1];
}
2020-11-20 21:16:52 +00:00
} else if (giphyURLs.includes(host)) {
2020-10-19 23:48:43 +00:00
payload.path = `https://media0.giphy.com/media/${image2.split("-").pop()}/giphy.gif`;
2020-11-20 21:16:52 +00:00
} else if (imgurURLs.includes(host)) {
2020-10-19 23:48:43 +00:00
payload.path = image.replace(".mp4", ".gif");
} else if (gfycatURLs.includes(host)) {
payload.path = `https://thumbs.gfycat.com/${image.split("/").pop().split(".mp4")[0]}-size_restricted.gif`;
}
2020-10-20 01:24:53 +00:00
payload.type = "image/gif";
} else {
payload.type = await getType(payload.path);
if (!payload.type || !formats.includes(payload.type)) return;
2019-09-13 20:02:41 +00:00
}
return payload;
2019-09-13 20:02:41 +00:00
} catch (error) {
if (error.name === "AbortError") {
throw Error("Timed out");
} else {
throw error;
}
2019-09-13 20:02:41 +00:00
}
};
const checkImages = async (message) => {
let type;
// first check the embeds
if (message.embeds.length !== 0) {
// embeds can vary in types, we check for tenor gifs first
if (message.embeds[0].type === "gifv") {
type = await getImage(message.embeds[0].video.url, message.embeds[0].url, true);
// then we check for other image types
} else if ((message.embeds[0].type === "video" || message.embeds[0].type === "image") && message.embeds[0].thumbnail) {
type = await getImage(message.embeds[0].thumbnail.proxy_url, message.embeds[0].thumbnail.url);
// finally we check both possible image fields for "generic" embeds
} else if (message.embeds[0].type === "rich") {
if (message.embeds[0].thumbnail) {
type = await getImage(message.embeds[0].thumbnail.proxy_url, message.embeds[0].thumbnail.url);
} else if (message.embeds[0].image) {
type = await getImage(message.embeds[0].image.proxy_url, message.embeds[0].image.url);
}
}
// then check the attachments
} else if (message.attachments.length !== 0 && message.attachments[0].width) {
type = await getImage(message.attachments[0].proxy_url, message.attachments[0].url);
}
// if the file is an image then return it
return type ? type : false;
};
2019-09-13 20:02:41 +00:00
// this checks for the latest message containing an image and returns the url of the image
module.exports = async (client, cmdMessage) => {
// we start by checking the current message for images
const result = await checkImages(cmdMessage);
if (result !== false) return result;
// if there aren't any in the current message then check if there's a reply
if (cmdMessage.messageReference) {
const replyMessage = await client.getMessage(cmdMessage.messageReference.channelID, cmdMessage.messageReference.messageID);
if (replyMessage) {
const replyResult = await checkImages(replyMessage);
if (replyResult !== false) return replyResult;
}
}
// if there aren't any replies then iterate over the last few messages in the channel
2019-09-13 20:02:41 +00:00
const messages = await cmdMessage.channel.getMessages();
// iterate over each message
for (const message of messages) {
const result = await checkImages(message);
if (result === false) {
continue;
} else {
return result;
2019-09-13 20:02:41 +00:00
}
}
};