change sing, removing double line breaks; cleanup jesusmemes
This commit is contained in:
		| @@ -3,7 +3,7 @@ | |||||||
|  |  | ||||||
| import traceback | import traceback | ||||||
| import logging | import logging | ||||||
| from typing import Optional | from typing import Optional, Union | ||||||
| from regex import Pattern | from regex import Pattern | ||||||
| import urllib | import urllib | ||||||
| import discord | import discord | ||||||
| @@ -100,7 +100,7 @@ class Sing(commands.Cog): | |||||||
|                     #     section = section.upper() |                     #     section = section.upper() | ||||||
|                     embed: discord.Embed = discord.Embed( |                     embed: discord.Embed = discord.Embed( | ||||||
|                     title=f"{search_result_song} by {search_result_artist}", |                     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, |                     embed.add_field(name="Confidence", value=search_result_confidence, | ||||||
|                                     inline=True) |                                     inline=True) | ||||||
| @@ -139,7 +139,7 @@ class Sing(commands.Cog): | |||||||
|             for _activity in ctx.interaction.guild.get_member(member_id).activities: |             for _activity in ctx.interaction.guild.get_member(member_id).activities: | ||||||
|                 if _activity.type == discord.ActivityType.listening: |                 if _activity.type == discord.ActivityType.listening: | ||||||
|                     activity = _activity |                     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) |                                                                activity=activity) | ||||||
|             if not parsed: |             if not parsed: | ||||||
|                 return await ctx.respond(f"Could not parse activity of {member_display}.", ephemeral=True) |                 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() |                     #     section = section.upper() | ||||||
|                     embed: discord.Embed = discord.Embed( |                     embed: discord.Embed = discord.Embed( | ||||||
|                     title=f"{search_result_song} by {search_result_artist}", |                     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="Confidence", value=search_result_confidence, inline=True) | ||||||
|                     embed.add_field(name="Time Taken", value=search_result_time_taken, inline=True) |                     embed.add_field(name="Time Taken", value=search_result_time_taken, inline=True) | ||||||
|   | |||||||
| @@ -1,50 +1,65 @@ | |||||||
| #!/usr/bin/env python3.11 | #!/usr/bin/env python3.12 | ||||||
|  |  | ||||||
| # Jesus Meme Generator  (requires Catbox uploader) | import aiohttp   | ||||||
|  | from typing import Optional | ||||||
| import aiohttp  # Not part of Python core | import regex  | ||||||
| import asyncio  # Part of Python core | from regex import Pattern | ||||||
| import regex # Not part of Python core | import os  | ||||||
| import os # Part of Python core | import random  | ||||||
| import random # Part of Python core |  | ||||||
| import traceback | import traceback | ||||||
| from urllib.parse import quote as urlquote | 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(): | class JesusMemeGenerator(): | ||||||
|     def __init__(self): |     def __init__(self) -> None: | ||||||
|         self.MEMEAPIURL = "https://apimeme.com/meme?meme=" |         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 |         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='', bottom_line='', meme="Jesus-Talking-To-Cool-Dude"): |     async def create_meme(self, top_line: str, bottom_line: str, | ||||||
|  |                           meme="Jesus-Talking-To-Cool-Dude") -> Optional[str]: | ||||||
|         try: |         try: | ||||||
|             top_line = regex.sub('[^\p{Letter}\p{Number}\p{Punctuation}\p{Horiz_Space}\p{Currency_Symbol}]', '', top_line.strip())   |             if not top_line or not bottom_line: | ||||||
|             bottom_line = regex.sub('[^\p{Letter}\p{Number}\p{Punctuation}\p{Horiz_Space}\p{Currency_Symbol}]', '', bottom_line.strip())   |                 return None | ||||||
|             OUT_FNAME = '' |             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: |             if len(top_line) < 1 or len(bottom_line) < 1: | ||||||
|                 return None |                 return None | ||||||
|              |              | ||||||
|             formed_url = f"{self.MEMEAPIURL}{meme}&top={top_line.strip()}&bottom={bottom_line.strip()}" |             formed_url: str = 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 = self.url_regex_1.sub('+', self.url_regex_2.sub('%23', formed_url.strip())) | ||||||
|             timeout = aiohttp.ClientTimeout(total=15) |             timeout = aiohttp.ClientTimeout(total=15) | ||||||
|             async with aiohttp.ClientSession(timeout=timeout) as session: |             async with aiohttp.ClientSession(timeout=timeout) as session: | ||||||
|                 async with session.get(formed_url) as response: |                 async with session.get(formed_url) as response: | ||||||
|                     UUID = f"{random.getrandbits(8)}-{random.getrandbits(8)}" |                     UUID = f"{random.getrandbits(8)}-{random.getrandbits(8)}" | ||||||
|                     OUT_FNAME = f"{UUID}.jpg" |                     out_fname = f"{UUID}.jpg" | ||||||
|                     with open(f"{self.MEMESTORAGEDIR}/{OUT_FNAME}", 'wb') as f: |                     with open(f"{self.MEMESTORAGEDIR}/{out_fname}", 'wb') as f: | ||||||
|                         f.write(await response.read()) |                         f.write(await response.read()) | ||||||
|          |          | ||||||
|             if len (OUT_FNAME) > 0: |             if not out_fname: | ||||||
|                 uploader = Catbox() |                 uploader = Catbox() | ||||||
|                 meme_link = uploader.upload(f"{self.MEMESTORAGEDIR}/{OUT_FNAME}") |                 meme_link: str = uploader.upload(f"{self.MEMESTORAGEDIR}/{out_fname}") | ||||||
|                 return meme_link |                 return meme_link | ||||||
|         except: |         except: | ||||||
|             print(traceback.format_exc()) |             print(traceback.format_exc()) | ||||||
|             pass |             pass | ||||||
|         return None |         return None | ||||||
|      |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user