Add meme generator module

This commit is contained in:
Tanguy Herbron 2021-05-09 20:29:49 +02:00
parent fc1c2db27c
commit 883147550f
3 changed files with 157 additions and 0 deletions

View File

@ -0,0 +1,2 @@
IMGFLIP_USER=
IMGFLIP_PASSWORD=

View File

@ -0,0 +1,139 @@
require("dotenv").config({path: process.env.MODULES_DIR + "/meme_generator/.env"});
const Imgflip = require("imgflip.com").Imgflip;
const imgflip = new Imgflip();
const fs = require("fs");
const shortcutMemes = {
"perhaps": 105577219,
"drake": 181913649
}
let meme = undefined;
let command = process.argv[2].substring(6);
let paramLimiter = command.indexOf(' ') === -1 ? command.length : command.indexOf(' ');
let memeParameter = command.substring(0, paramLimiter);
let paramDivider = command.indexOf('-') === -1 ? command.length : command.indexOf('-');
let text0 = command.substring(paramLimiter, paramDivider);
let text1 = command.substring(paramDivider + 2);
switch(memeParameter) {
case "help":
printHelp();
return;
case "shortcuts":
printShortcuts();
return;
default:
break;
}
if(/^\d+$/.test(memeParameter)) {
meme = memeParameter;
} else {
meme = shortcutMemes[memeParameter];
if(meme === undefined) {
printError();
return;
}
}
function requestMeme() {
return imgflip.api
.captionImage({
username: process.env.IMGFLIP_USER,
password: process.env.IMGFLIP_PASSWORD,
template_id: meme,
text0: text0 || ' ',
text1: text1 || ' '
})
.then(res => res)
.catch(err => console.error(err));
}
function printError() {
console.log("Failed to generate meme");
}
function printShortcuts() {
let fields = [];
for(let shortcut in shortcutMemes) {
fields.push({
name: shortcut,
value: "https://imgflip.com/memegenerator/" + shortcutMemes[shortcut],
inline: true
})
}
console.log(JSON.stringify({
status: "success",
action: "standard",
format: "embed",
profile: {
color: 0x0099f,
title: "Meme shortcuts",
url: undefined,
author: undefined,
description: "Meme shortcuts, linked to respective template id",
thumbnail: undefined,
fields: fields
}
}));
}
function printHelp() {
console.log(JSON.stringify({
status: "success",
action: "standard",
format: "embed",
profile: {
color: 0x0099f,
title: "Meme generator",
url: undefined,
author: undefined,
description: "Generate meme using imgflip API",
thumbnail: undefined,
fields: [
{
name: "`!meme <shortcut> <text 1/optional> <text 2/optional>`",
value: "Generate a meme using the given shortcut and the two provided texts (if provided)",
inline: false,
},
{
name: "`!meme <template id> <text 1/optional> <text 2/optional>`",
value: "Generate a meme using the given template id and the two provided texts (if provided)",
inline: false,
},
{
name: "`!meme shortcuts`",
value: "List available shortcuts",
inline: false,
},
{
name: "`!meme help`",
value: "Display this help menu",
inline: false,
},
]
}
}));
}
function formatOutput(result) {
if(result.success === true) {
console.log(result.data.url);
} else {
printError();
}
}
requestMeme().then(res => formatOutput(res));

View File

@ -0,0 +1,16 @@
{
"name": "meme-generator",
"version": "0.0.1",
"description": "Generate meme via command text message using imgflip API",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Tanguy Herbron",
"license": "MIT",
"prefix": "!meme",
"dependencies": {
"dotenv": "^9.0.1",
"imgflip.com": "^3.1.3"
}
}