cleanup
This commit is contained in:
parent
3dac803305
commit
bcc3bc02fa
22
cogs/meme.py
22
cogs/meme.py
@ -4,7 +4,6 @@ import json
|
||||
import io
|
||||
import asyncio
|
||||
import random
|
||||
import copy
|
||||
from PIL import Image, UnidentifiedImageError
|
||||
import imagehash
|
||||
from typing import LiteralString, Optional, Any, Union
|
||||
@ -250,24 +249,22 @@ class Meme(commands.Cog):
|
||||
self.meme_leaderboard[uid] = count
|
||||
|
||||
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]:
|
||||
"""
|
||||
INSERT MEME -> SQLITE DB
|
||||
"""
|
||||
try:
|
||||
_image: io.BytesIO = io.BytesIO(
|
||||
requests.get(image_url, stream=True, timeout=20).raw.read()
|
||||
)
|
||||
image_copy = copy.deepcopy(_image)
|
||||
image = Image.open(image_copy)
|
||||
image.seek(0)
|
||||
_image = Image.open(image)
|
||||
except UnidentifiedImageError:
|
||||
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(?, ?, ?, ?, ?)"
|
||||
image.seek(0)
|
||||
async with sqlite3.connect(self.memedb_path, timeout=2) as db_conn:
|
||||
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:
|
||||
await db_conn.commit()
|
||||
@ -736,13 +733,14 @@ class Meme(commands.Cog):
|
||||
)
|
||||
await message.reply(original_message_url)
|
||||
else:
|
||||
unique_memes.append(item.url)
|
||||
image.seek(0)
|
||||
unique_memes.append(image)
|
||||
if unique_memes:
|
||||
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
|
||||
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]]:
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user