From ecb7293f01f5327ff21b3ce833c47237f048e0ea Mon Sep 17 00:00:00 2001 From: Zeroquinc Date: Sat, 22 Jun 2024 23:18:56 +0200 Subject: [PATCH] feature: new non-compact embeds & cache colors --- .gitignore | 3 ++- src/discord.py | 14 +++++++------- src/trophies.py | 2 +- utils/image.py | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 7373346..9ddeb8f 100644 --- a/.gitignore +++ b/.gitignore @@ -166,4 +166,5 @@ cython_debug/ config.py # json files -emoji.json \ No newline at end of file +emoji.json +src/*.json \ No newline at end of file diff --git a/src/discord.py b/src/discord.py index 8aeab4c..829d226 100644 --- a/src/discord.py +++ b/src/discord.py @@ -12,23 +12,23 @@ async def create_trophy_embed(trophy, trophy_title_info, client, current, total_ trophy_title = trophy_title_info['trophy_title'] game_url = format_title(trophy_title.title_name) # format the title name into a URL platform = trophy_title_info['platform'] - most_common_color = await get_discord_color(trophy.trophy_icon_url) + most_common_color = await get_discord_color(trophy_title.title_icon_url) completion = current percentage = (completion / total_trophies) * 100 - embed = discord.Embed(description=f"**[{trophy_title.title_name}]({game_url}) ({platform})**\n\n{trophy.trophy_detail}\n\nUnlocked by {trophy.trophy_earn_rate}% of players", color=most_common_color) - embed.add_field(name="Trophy", value=f"[{trophy.trophy_name}]({trophy.trophy_icon_url})", inline=True) + embed = discord.Embed(title=trophy.trophy_name, description=f"{trophy.trophy_detail}\n\n[{trophy_title.title_name}]({game_url}) ({platform})", color=most_common_color) + embed.add_field(name="Unlock Ratio", value=f"{trophy.trophy_earn_rate}%", inline=True) embed.add_field(name="Rarity", value=replace_trophy_with_emoji(trophy.trophy_type.name.lower())) - embed.add_field(name="Completion", value=f"{completion}/{total_trophies} ({percentage:.2f}%)", inline=True) + embed.add_field(name="Progress", value=f"{completion}/{total_trophies} ({percentage:.2f}%)", inline=True) embed.set_image(url=DISCORD_IMAGE) embed.set_thumbnail(url=trophy.trophy_icon_url) - embed.set_footer(text=f"{client.online_id} • Earned on {format_date(trophy.earned_date_time)}", icon_url=client.profile_picture_url) - embed.set_author(name="A Trophy Unlocked", icon_url=trophy_title.title_icon_url) + embed.set_footer(text=f"{client.online_id} • {format_date(trophy.earned_date_time)}", icon_url=client.profile_picture_url) + embed.set_author(name="Trophy unlocked", icon_url=trophy_title.title_icon_url) return embed async def create_simple_trophy_embed(trophy, trophy_title_info, client, current, total_trophies): trophy_title = trophy_title_info['trophy_title'] game_url = format_title(trophy_title.title_name) # format the title name into a URL - most_common_color = await get_discord_color(trophy.trophy_icon_url) + most_common_color = await get_discord_color(trophy_title.title_icon_url) completion = current percentage = (completion / total_trophies) * 100 embed = discord.Embed(description=f"**{trophy.trophy_name} {replace_trophy_with_emoji(trophy.trophy_type.name.lower())} {trophy.trophy_earn_rate}%**\n{trophy.trophy_detail}\n\n[{trophy_title.title_name}]({game_url})", color=most_common_color) diff --git a/src/trophies.py b/src/trophies.py index 9ddb3e9..43f9868 100644 --- a/src/trophies.py +++ b/src/trophies.py @@ -25,7 +25,7 @@ def get_client(): profile_picture[online_id] = profile_picture_url return client -def get_recent_titles(client, hours=1000): +def get_recent_titles(client, hours=24): logger.info("Calling Sony API to get recently played games") now = get_current_time() titles = list(client.title_stats()) diff --git a/utils/image.py b/utils/image.py index 13d917d..4760177 100644 --- a/utils/image.py +++ b/utils/image.py @@ -3,20 +3,53 @@ from io import BytesIO import numpy as np import asyncio +import json +from pathlib import Path +from utils.custom_logger import logger + +# Cache file path +cache_file_path = Path("src/image_cache.json") + +# Function to load cache +def load_cache(): + if not cache_file_path.exists(): + return {} + with open(cache_file_path, "r") as file: + return json.load(file) + +# Function to save cache +def save_cache(cache): + with open(cache_file_path, "w") as file: + json.dump(cache, file) + +# Check if the color is colorful def is_colorful(color): r, g, b = color return np.std([r, g, b]) +# Asynchronous function to get discord color async def get_discord_color(image_url, crop_percentage=0.5): + # Load the cache + cache = load_cache() + # Check if the URL is already in the cache + if image_url in cache: + logger.debug(f"Cache hit for {image_url}") + return cache[image_url] + # If not in cache, process the image loop = asyncio.get_event_loop() - return await loop.run_in_executor( + color = await loop.run_in_executor( None, get_discord_color_blocking, image_url, crop_percentage, ) + # Update the cache with the new color + cache[image_url] = color + save_cache(cache) + return color +# Blocking function to get discord color def get_discord_color_blocking(image_url, crop_percentage=0.5): response = requests.get(image_url) img = Image.open(BytesIO(response.content))