Skip to content

Commit 275c943

Browse files
authored
Merge pull request #31 from zeroquinc:platinum-total-time
feature: support for total time for platinum
2 parents 8a2992a + 7a2b37d commit 275c943

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/trophies.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from psnawp_api import PSNAWP
44

55
from utils.image_utils import get_discord_color
6-
from utils.trophy_utils import format_title
6+
from utils.trophy_utils import format_title, calculate_total_time
77
from utils.date_utils import format_date
88
from config.config import PSNTOKEN, DISCORD_IMAGE, TROPHIES_INTERVAL
99

@@ -63,16 +63,16 @@ def create_trophy_embed(trophy, trophy_title_info, client, current, total_trophi
6363
embed.set_author(name="A Trophy Unlocked", icon_url=trophy_title.title_icon_url)
6464
return embed
6565

66-
def create_platinum_embed(trophy, trophy_title_info, client):
66+
def create_platinum_embed(trophy, trophy_title_info, client, formatted_time_diff):
6767
trophy_title = trophy_title_info['trophy_title']
6868
game_url = format_title(trophy_title.title_name) # format the title name into a URL
6969
platform = trophy_title_info['platform']
7070
most_common_color = get_discord_color(trophy_title.title_icon_url)
71-
embed = discord.Embed(description=f"**[{trophy_title.title_name}]({game_url}) ({platform})**\n\n {client.online_id} has achieved the Platinum trophy of the game! \n\n{trophy_title.title_name} has {trophy_title.defined_trophies['bronze']} Bronze, {trophy_title.defined_trophies['silver']} Silver, {trophy_title.defined_trophies['gold']} Gold, and {trophy_title.defined_trophies['platinum']} Platinum trophy.\n\n The Platinum has been achieved by {trophy.trophy_earn_rate}% of players", color=most_common_color)
71+
embed = discord.Embed(description=f"**[{trophy_title.title_name}]({game_url}) ({platform})**\n\n Achieved in {formatted_time_diff} \n\n{trophy_title.title_name} has {trophy_title.defined_trophies['bronze']} Bronze, {trophy_title.defined_trophies['silver']} Silver, {trophy_title.defined_trophies['gold']} Gold, and {trophy_title.defined_trophies['platinum']} Platinum trophy\n\n The Platinum has been achieved by {trophy.trophy_earn_rate}% of players", color=most_common_color)
7272
embed.add_field(name="Trophy", value=f"[{trophy.trophy_name}]({trophy.trophy_icon_url})", inline=True)
7373
embed.set_image(url=DISCORD_IMAGE)
7474
embed.set_thumbnail(url=trophy_title.title_icon_url)
75-
embed.set_footer(text=f"{client.online_id}Earned on {format_date(trophy.earned_date_time)}", icon_url=client.profile_picture_url)
75+
embed.set_footer(text=f"{client.online_id}Platinum achieved on {format_date(trophy.earned_date_time)}", icon_url=client.profile_picture_url)
7676
embed.set_author(name="A New Platinum", icon_url=trophy_title.title_icon_url)
7777
return embed
7878

@@ -107,7 +107,15 @@ async def process_trophies_embeds(client, title_ids, TROPHIES_INTERVAL):
107107
embed = create_trophy_embed(trophy, trophy_title, client, starting_count + i + 1, total_trophies)
108108
trophy_embeds.append((trophy.earned_date_time, embed))
109109
if trophy.trophy_type.name.lower() == 'platinum':
110-
embed = create_platinum_embed(trophy, trophy_title, client)
110+
# Get the oldest and newest trophy
111+
oldest_trophy = earned_trophies[0]
112+
newest_trophy = earned_trophies[-1]
113+
# Calculate the time difference
114+
time_diff = newest_trophy[0].earned_date_time - oldest_trophy[0].earned_date_time
115+
# Format the time difference
116+
formatted_time_diff = calculate_total_time(time_diff)
117+
# Pass formatted_time_diff to create_platinum_embed function
118+
embed = create_platinum_embed(trophy, trophy_title, client, formatted_time_diff)
111119
platinum_embeds.append((trophy.earned_date_time, embed))
112120
return trophy_embeds, len(recent_trophies), platinum_embeds
113121

utils/trophy_utils.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,31 @@ def format_title(title):
44
title = title.lower() # convert to lowercase
55
title = re.sub(r"[^\w\s]", '', title) # remove special characters except whitespace and underscores
66
title = title.replace(' ', '-') # replace spaces with hyphens
7-
return f"https://www.playstation.com/games/{title}/" # prepend base URL
7+
return f"https://www.playstation.com/games/{title}/" # prepend base URL
8+
9+
def calculate_total_time(td):
10+
minutes, _ = divmod(td.seconds + td.days * 86400, 60)
11+
hours, minutes = divmod(minutes, 60)
12+
days, hours = divmod(hours, 24)
13+
weeks, days = divmod(days, 7)
14+
years, weeks = divmod(weeks, 52)
15+
16+
result = []
17+
if years:
18+
result.append(f"{years} year{'s' if years > 1 else ''}")
19+
if weeks:
20+
result.append(f"{weeks} week{'s' if weeks > 1 else ''}")
21+
if days:
22+
result.append(f"{days} day{'s' if days > 1 else ''}")
23+
if hours:
24+
result.append(f"{hours} hour{'s' if hours > 1 else ''}")
25+
if minutes:
26+
result.append(f"{minutes} minute{'s' if minutes > 1 else ''}")
27+
28+
if len(result) > 1:
29+
last = result.pop()
30+
return ', '.join(result) + ' and ' + last
31+
elif result:
32+
return result[0]
33+
else:
34+
return ''

0 commit comments

Comments
 (0)