such cleanup
This commit is contained in:
50
jesusmemes.py
Normal file
50
jesusmemes.py
Normal file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3.11
|
||||
|
||||
# Jesus Meme Generator (requires Catbox uploader)
|
||||
|
||||
import aiohttp # Not part of Python core
|
||||
import asyncio # Part of Python core
|
||||
import regex # Not part of Python core
|
||||
import os # Part of Python core
|
||||
import random # Part of Python core
|
||||
import traceback
|
||||
from urllib.parse import quote as urlquote
|
||||
from catbox import Catbox # Not part of Python core
|
||||
|
||||
|
||||
class JesusMemeGenerator():
|
||||
def __init__(self):
|
||||
self.MEMEAPIURL = "https://apimeme.com/meme?meme="
|
||||
self.MEMESTORAGEDIR = os.path.join(os.path.expanduser("~"), "memes") # Save memes "temporarily" to the home directory-->'memes' subfolder; cleanup must be done externally
|
||||
|
||||
async def create_meme(self, top_line='', bottom_line='', meme="Jesus-Talking-To-Cool-Dude"):
|
||||
try:
|
||||
top_line = regex.sub('[^\p{Letter}\p{Number}\p{Punctuation}\p{Horiz_Space}\p{Currency_Symbol}]', '', top_line.strip())
|
||||
bottom_line = regex.sub('[^\p{Letter}\p{Number}\p{Punctuation}\p{Horiz_Space}\p{Currency_Symbol}]', '', bottom_line.strip())
|
||||
OUT_FNAME = ''
|
||||
|
||||
if len(top_line) < 1 or len(bottom_line) < 1:
|
||||
return None
|
||||
|
||||
formed_url = f"{self.MEMEAPIURL}{meme}&top={top_line.strip()}&bottom={bottom_line.strip()}"
|
||||
formed_url = regex.sub('\p{Horiz_Space}', '+', regex.sub('#', '%23', formed_url.strip()))
|
||||
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"
|
||||
with open(f"{self.MEMESTORAGEDIR}/{OUT_FNAME}", 'wb') as f:
|
||||
f.write(await response.read())
|
||||
|
||||
if len (OUT_FNAME) > 0:
|
||||
uploader = Catbox()
|
||||
meme_link = uploader.upload(f"{self.MEMESTORAGEDIR}/{OUT_FNAME}")
|
||||
return meme_link
|
||||
except:
|
||||
print(traceback.format_exc())
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user