diff --git a/cogs/sing.py b/cogs/sing.py index b2d26b4..2a731e5 100644 --- a/cogs/sing.py +++ b/cogs/sing.py @@ -3,7 +3,7 @@ import traceback import logging -from typing import Optional +from typing import Optional, Union from regex import Pattern import urllib import discord @@ -100,7 +100,7 @@ class Sing(commands.Cog): # section = section.upper() embed: discord.Embed = discord.Embed( title=f"{search_result_song} by {search_result_artist}", - description=discord.utils.escape_markdown(section.replace("\n", "\n\n")) + description=discord.utils.escape_markdown(section) ) embed.add_field(name="Confidence", value=search_result_confidence, inline=True) @@ -139,7 +139,7 @@ class Sing(commands.Cog): for _activity in ctx.interaction.guild.get_member(member_id).activities: if _activity.type == discord.ActivityType.listening: activity = _activity - parsed: tuple|bool = self.utility.parse_song_input(song=None, + parsed: Union[tuple, bool] = self.utility.parse_song_input(song=None, activity=activity) if not parsed: return await ctx.respond(f"Could not parse activity of {member_display}.", ephemeral=True) @@ -174,7 +174,7 @@ class Sing(commands.Cog): # section = section.upper() embed: discord.Embed = discord.Embed( title=f"{search_result_song} by {search_result_artist}", - description=discord.utils.escape_markdown(section.replace("\n", "\n\n")) + description=discord.utils.escape_markdown(section) ) embed.add_field(name="Confidence", value=search_result_confidence, inline=True) embed.add_field(name="Time Taken", value=search_result_time_taken, inline=True) diff --git a/jesusmemes.py b/jesusmemes.py index 1c30b87..9651f5d 100644 --- a/jesusmemes.py +++ b/jesusmemes.py @@ -1,50 +1,65 @@ -#!/usr/bin/env python3.11 +#!/usr/bin/env python3.12 -# 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 aiohttp +from typing import Optional +import regex +from regex import Pattern +import os +import random import traceback from urllib.parse import quote as urlquote -from catbox import Catbox # Not part of Python core +from catbox import Catbox +""" +Jesus Meme Generator +(requires Catbox uploader) +""" class JesusMemeGenerator(): - def __init__(self): + def __init__(self) -> None: 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"): + self.MEMESTORAGEDIR = os.path.join(os.path.expanduser("~"), + "memes") + self.top_line_regex: Pattern = regex.compile( + r'[^\p{Letter}\p{Number}\p{Punctuation}\p{Horiz_Space}\p{Currency_Symbol}]') + self.bottom_line_regex: Pattern = regex.compile( + 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]: 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 not top_line or not bottom_line: + return None + 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 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())) + 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())) 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: + 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: + if not out_fname: uploader = Catbox() - meme_link = uploader.upload(f"{self.MEMESTORAGEDIR}/{OUT_FNAME}") + meme_link: str = uploader.upload(f"{self.MEMESTORAGEDIR}/{out_fname}") return meme_link except: print(traceback.format_exc()) pass return None - - - -