cleanup
This commit is contained in:
		
							
								
								
									
										22
									
								
								cogs/meme.py
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								cogs/meme.py
									
									
									
									
									
								
							| @@ -4,7 +4,6 @@ import json | |||||||
| import io | import io | ||||||
| import asyncio | import asyncio | ||||||
| import random | import random | ||||||
| import copy |  | ||||||
| from PIL import Image, UnidentifiedImageError | from PIL import Image, UnidentifiedImageError | ||||||
| import imagehash | import imagehash | ||||||
| from typing import LiteralString, Optional, Any, Union | from typing import LiteralString, Optional, Any, Union | ||||||
| @@ -250,24 +249,22 @@ class Meme(commands.Cog): | |||||||
|                     self.meme_leaderboard[uid] = count |                     self.meme_leaderboard[uid] = count | ||||||
|  |  | ||||||
|     async def insert_meme( |     async def insert_meme( | ||||||
|         self, discord_uid: int, timestamp: int, message_id: int, image_url: str |         self, discord_uid: int, timestamp: int, message_id: int, image: io.BytesIO | ||||||
|     ) -> Optional[bool]: |     ) -> Optional[bool]: | ||||||
|         """ |         """ | ||||||
|         INSERT MEME -> SQLITE DB |         INSERT MEME -> SQLITE DB | ||||||
|         """ |         """ | ||||||
|         try: |         try: | ||||||
|             _image: io.BytesIO = io.BytesIO( |             image.seek(0) | ||||||
|                 requests.get(image_url, stream=True, timeout=20).raw.read() |             _image = Image.open(image) | ||||||
|             ) |  | ||||||
|             image_copy = copy.deepcopy(_image) |  | ||||||
|             image = Image.open(image_copy) |  | ||||||
|         except UnidentifiedImageError: |         except UnidentifiedImageError: | ||||||
|             return None |             return None | ||||||
|         phash: str = str(imagehash.phash(image)) |         phash: str = str(imagehash.phash(_image)) | ||||||
|         query: str = "INSERT INTO memes(discord_uid, timestamp, image, message_ids, phash) VALUES(?, ?, ?, ?, ?)" |         query: str = "INSERT INTO memes(discord_uid, timestamp, image, message_ids, phash) VALUES(?, ?, ?, ?, ?)" | ||||||
|  |         image.seek(0) | ||||||
|         async with sqlite3.connect(self.memedb_path, timeout=2) as db_conn: |         async with sqlite3.connect(self.memedb_path, timeout=2) as db_conn: | ||||||
|             insert = await db_conn.execute_insert( |             insert = await db_conn.execute_insert( | ||||||
|                 query, (discord_uid, timestamp, _image.read(), message_id, phash) |                 query, (discord_uid, timestamp, image.read(), message_id, phash) | ||||||
|             ) |             ) | ||||||
|             if insert: |             if insert: | ||||||
|                 await db_conn.commit() |                 await db_conn.commit() | ||||||
| @@ -736,13 +733,14 @@ class Meme(commands.Cog): | |||||||
|                     ) |                     ) | ||||||
|                     await message.reply(original_message_url) |                     await message.reply(original_message_url) | ||||||
|                 else: |                 else: | ||||||
|                     unique_memes.append(item.url) |                     image.seek(0) | ||||||
|  |                     unique_memes.append(image) | ||||||
|         if unique_memes: |         if unique_memes: | ||||||
|             await self.leaderboard_increment(message.author.id) |             await self.leaderboard_increment(message.author.id) | ||||||
|             for meme_url in unique_memes: |             for meme_image in unique_memes: | ||||||
|                 author_id: int = message.author.id |                 author_id: int = message.author.id | ||||||
|                 timestamp: int = int(message.created_at.timestamp()) |                 timestamp: int = int(message.created_at.timestamp()) | ||||||
|                 await self.insert_meme(author_id, timestamp, message.id, meme_url) |                 await self.insert_meme(author_id, timestamp, message.id, meme_image) | ||||||
|  |  | ||||||
|     async def get_top(self, n: int = 10) -> Optional[list[tuple]]: |     async def get_top(self, n: int = 10) -> Optional[list[tuple]]: | ||||||
|         """ |         """ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user