2021-01-26 00:05:44 +01:00
|
|
|
import os
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from functions.database.memes import Meme
|
|
|
|
from functions.mock import mock
|
|
|
|
|
|
|
|
|
|
|
|
def generate(meme: Meme, fields):
|
|
|
|
"""
|
|
|
|
Main function that takes a Meme as input & generates an image.
|
|
|
|
"""
|
2022-02-06 17:58:57 +01:00
|
|
|
fields = list(fields)
|
|
|
|
|
2021-01-26 00:05:44 +01:00
|
|
|
# If there's only one field, the user isn't required to use quotes
|
|
|
|
if meme.fields == 1:
|
|
|
|
fields = [" ".join(fields)]
|
|
|
|
|
2022-02-03 02:01:09 +01:00
|
|
|
fields = _apply_meme(meme, fields)
|
2021-01-26 00:05:44 +01:00
|
|
|
|
|
|
|
# List of fields to send to the API
|
|
|
|
boxes = [{"text": ""}, {"text": ""}, {"text": ""}, {"text": ""}]
|
|
|
|
|
|
|
|
# Add all fields required & ignore the excess ones
|
|
|
|
for i in range(len(fields)):
|
|
|
|
if i > 3:
|
|
|
|
break
|
|
|
|
boxes[i]["text"] = fields[i]
|
|
|
|
|
|
|
|
# Check server status
|
|
|
|
req = requests.get('https://api.imgflip.com/get_memes').json()
|
|
|
|
|
|
|
|
# Server is down
|
|
|
|
if not req["success"]:
|
|
|
|
return {"success": False, "message": "Er is een fout opgetreden."}
|
|
|
|
|
|
|
|
# Post meme
|
2022-02-03 02:01:09 +01:00
|
|
|
reply = _post_meme(meme, boxes)
|
2021-01-26 00:05:44 +01:00
|
|
|
|
|
|
|
# Adding a message parameter makes the code in the cog a lot cleaner
|
|
|
|
if not reply["success"]:
|
2022-02-06 17:58:57 +01:00
|
|
|
reply["success"] = False
|
2021-01-26 00:05:44 +01:00
|
|
|
reply["message"] = "Error! Controleer of je de juiste syntax hebt gebruikt. Gebruik het commando " \
|
|
|
|
"\"memes\" voor een lijst aan geaccepteerde meme-namen."
|
|
|
|
else:
|
|
|
|
reply["message"] = reply["data"]["url"]
|
|
|
|
|
2022-02-06 17:58:57 +01:00
|
|
|
reply["success"] = False
|
|
|
|
|
2021-01-26 00:05:44 +01:00
|
|
|
return reply
|
|
|
|
|
|
|
|
|
2022-02-03 02:01:09 +01:00
|
|
|
def _post_meme(meme: Meme, boxes):
|
2021-01-26 00:05:44 +01:00
|
|
|
"""
|
|
|
|
Performs API request to generate the meme
|
|
|
|
"""
|
|
|
|
caption = {
|
|
|
|
"template_id": meme.meme_id,
|
|
|
|
"username": os.getenv("IMGFLIPNAME"),
|
|
|
|
"password": os.getenv("IMGFLIPPASSWORD"),
|
|
|
|
"boxes[0][text]": boxes[0]["text"],
|
|
|
|
"boxes[1][text]": boxes[1]["text"],
|
|
|
|
"boxes[2][text]": boxes[2]["text"],
|
|
|
|
"boxes[3][text]": boxes[3]["text"]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Send the POST to the API
|
|
|
|
memeReply = requests.post('https://api.imgflip.com/caption_image', caption).json()
|
|
|
|
|
|
|
|
return memeReply
|
|
|
|
|
|
|
|
|
2022-02-03 02:01:09 +01:00
|
|
|
def _apply_meme(meme: Meme, fields):
|
2021-01-26 00:05:44 +01:00
|
|
|
"""
|
|
|
|
Some memes are in a special format that only requires
|
|
|
|
a few words to be added, or needs the input to be changed.
|
|
|
|
This function handles all that.
|
|
|
|
|
2021-01-26 22:26:49 +01:00
|
|
|
Links certain meme id's to functions that need to be applied first.
|
2021-01-26 00:05:44 +01:00
|
|
|
"""
|
|
|
|
memeDict = {
|
2022-02-03 02:01:09 +01:00
|
|
|
102156234: mocking_spongebob,
|
|
|
|
91538330: _x_x_everywhere,
|
|
|
|
252600902: _always_has_been,
|
2022-02-06 17:58:57 +01:00
|
|
|
167754325: _math_is_math,
|
|
|
|
206493414: _i_used_the_x_to_destroy_the_x
|
2021-01-26 00:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Meme needs no special treatment
|
|
|
|
if meme.meme_id not in memeDict:
|
|
|
|
return fields
|
|
|
|
|
|
|
|
return memeDict[meme.meme_id](fields)
|
|
|
|
|
|
|
|
|
2022-02-03 02:01:09 +01:00
|
|
|
def mocking_spongebob(fields):
|
2021-01-26 21:58:49 +01:00
|
|
|
return list(map(mock, fields))
|
2021-01-26 00:05:44 +01:00
|
|
|
|
|
|
|
|
2022-02-03 02:01:09 +01:00
|
|
|
def _x_x_everywhere(fields):
|
2021-01-26 00:05:44 +01:00
|
|
|
word = fields[0]
|
|
|
|
|
|
|
|
return ["{}".format(word), "{} everywhere".format(word)]
|
|
|
|
|
|
|
|
|
2022-02-03 02:01:09 +01:00
|
|
|
def _always_has_been(fields):
|
2021-01-26 00:05:44 +01:00
|
|
|
word = fields[0]
|
|
|
|
|
2022-02-03 02:01:09 +01:00
|
|
|
return ["Wait, {}?".format(word), "Always has been"]
|
|
|
|
|
|
|
|
|
|
|
|
def _math_is_math(fields):
|
2022-02-06 17:58:57 +01:00
|
|
|
word = fields[0].upper()
|
|
|
|
|
|
|
|
return ["", f"{word} IS {word}!"]
|
|
|
|
|
|
|
|
|
|
|
|
def _i_used_the_x_to_destroy_the_x(fields):
|
2022-02-03 02:01:09 +01:00
|
|
|
word = fields[0]
|
|
|
|
|
2022-02-06 17:58:57 +01:00
|
|
|
return ["", f"I used the {word} to destroy the {word}"]
|