discord-havoc/util/jesusmemes.py

82 lines
2.8 KiB
Python
Raw Normal View History

2025-04-17 14:35:56 -04:00
import aiohttp
from typing import Optional
2025-04-17 14:35:56 -04:00
import regex
from regex import Pattern
2025-04-17 14:35:56 -04:00
import os
import random
2025-02-16 13:16:53 -05:00
import logging
2025-02-15 08:36:45 -05:00
import traceback
2025-02-16 13:19:41 -05:00
from util.catbox import CatboxAsync
2025-02-15 08:36:45 -05:00
"""
Jesus Meme Generator
(requires Catbox uploader)
"""
2025-02-15 08:36:45 -05:00
2025-04-17 14:35:56 -04:00
class JesusMemeGenerator:
def __init__(self) -> None:
2025-02-15 08:36:45 -05:00
self.MEMEAPIURL = "https://apimeme.com/meme?meme="
2025-04-17 14:35:56 -04:00
self.MEMESTORAGEDIR = os.path.join(os.path.expanduser("~"), "memes")
self.top_line_regex: Pattern = regex.compile(
2025-04-17 14:35:56 -04:00
r"[^\p{Letter}\p{Number}\p{Punctuation}\p{Horiz_Space}\p{Currency_Symbol}]"
)
self.bottom_line_regex: Pattern = regex.compile(
2025-04-17 14:35:56 -04:00
r"[^\p{Letter}\p{Number}\p{Punctuation}\p{Horiz_Space}\p{Currency_Symbol}]"
)
self.url_regex_1: Pattern = regex.compile(r"\p{Horiz_Space}")
self.url_regex_2: Pattern = regex.compile(r"#")
async def create_meme(
self, top_line: str, bottom_line: str, meme="Jesus-Talking-To-Cool-Dude"
) -> Optional[str]:
2025-02-16 09:37:47 -05:00
"""
Create Meme
2025-04-17 14:35:56 -04:00
2025-02-16 09:37:47 -05:00
Args:
top_line (str): Top line of meme
bottom_line (str): Bottom line of meme
meme (str): The meme to use, defaults to Jesus-Talking-To-Cool-Dude
2025-04-17 14:35:56 -04:00
2025-02-16 09:37:47 -05:00
Returns:
Optional[str]
2025-04-17 14:35:56 -04:00
2025-02-16 09:37:47 -05:00
"""
2025-02-15 08:36:45 -05:00
try:
if not top_line or not bottom_line:
return None
2025-04-17 14:35:56 -04:00
top_line = self.top_line_regex.sub("", top_line.strip())
bottom_line = self.bottom_line_regex.sub("", bottom_line.strip())
out_fname: Optional[str] = None
2025-04-17 14:35:56 -04:00
2025-02-15 08:36:45 -05:00
if len(top_line) < 1 or len(bottom_line) < 1:
return None
2025-04-17 14:35:56 -04:00
formed_url: str = (
f"{self.MEMEAPIURL}{meme}&top={top_line.strip()}&bottom={bottom_line.strip()}"
)
formed_url = self.url_regex_1.sub(
"+", self.url_regex_2.sub("%23", formed_url.strip())
)
2025-02-15 08:36:45 -05:00
timeout = aiohttp.ClientTimeout(total=15)
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.get(formed_url) as response:
UUID = f"{random.getrandbits(8)}-{random.getrandbits(8)}"
out_fname = f"{UUID}.jpg"
2025-04-17 14:35:56 -04:00
with open(f"{self.MEMESTORAGEDIR}/{out_fname}", "wb") as f:
2025-02-15 08:36:45 -05:00
f.write(await response.read())
2025-04-17 14:35:56 -04:00
if not out_fname:
2025-02-16 13:16:53 -05:00
uploader = CatboxAsync()
2025-04-17 14:35:56 -04:00
meme_link: Optional[str] = await uploader.upload(
f"{self.MEMESTORAGEDIR}/{out_fname}"
)
2025-02-16 13:16:53 -05:00
if not meme_link:
logging.info("Meme upload failed!")
return None
2025-02-15 08:36:45 -05:00
return meme_link
except:
print(traceback.format_exc())
pass
return None