Skip to content

Commit

Permalink
Merge pull request #10 from zeroquinc:cache-images
Browse files Browse the repository at this point in the history
feature: new non-compact embeds & cache colors
  • Loading branch information
zeroquinc authored Jun 22, 2024
2 parents d587032 + ecb7293 commit 52aee37
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,5 @@ cython_debug/
config.py

# json files
emoji.json
emoji.json
src/*.json
14 changes: 7 additions & 7 deletions src/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/trophies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
35 changes: 34 additions & 1 deletion utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 52aee37

Please sign in to comment.