aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar korty.codes <[email protected]>2024-03-02 07:02:35 +0400
committerGravatar GitHub <[email protected]>2024-03-02 03:02:35 +0000
commit0499783055bd4381f3ddcc875c39a95c6d0d2900 (patch)
treee548072dac8aef3ef049678351c8636852ad810d
parentBump python-dateutil from 2.8.2 to 2.9.0.post0 (#2942) (diff)
Add upload date to !pypi (#2924)
Co-authored-by: Xithrius <[email protected]>
-rw-r--r--bot/exts/info/pypi.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/bot/exts/info/pypi.py b/bot/exts/info/pypi.py
index bac7d2389..e924b7a41 100644
--- a/bot/exts/info/pypi.py
+++ b/bot/exts/info/pypi.py
@@ -1,7 +1,9 @@
import itertools
import random
import re
+import typing
from contextlib import suppress
+from datetime import datetime
from discord import Embed, NotFound
from discord.ext.commands import Cog, Context, command
@@ -10,6 +12,7 @@ from discord.utils import escape_markdown
from bot.bot import Bot
from bot.constants import Colours, NEGATIVE_REPLIES, RedirectOutput
from bot.log import get_logger
+from bot.utils import time
from bot.utils.messages import wait_for_deletion
URL = "https://pypi.org/pypi/{package}/json"
@@ -22,6 +25,16 @@ INVALID_INPUT_DELETE_DELAY = RedirectOutput.delete_delay
log = get_logger(__name__)
+def _get_latest_distribution_timestamp(data: dict[str, typing.Any]) -> datetime | None:
+ """Get upload date of last distribution, or `None` if no distributions were found."""
+ if not data["urls"]:
+ return None
+
+ try:
+ return time.discord_timestamp(data["urls"][-1]["upload_time_iso_8601"], time.TimestampFormats.DATE)
+ except KeyError:
+ log.trace("KeyError trying to fetch upload time: data['urls'][-1]['upload_time_iso_8601']")
+ return None
class PyPi(Cog):
"""Cog for getting information about PyPi packages."""
@@ -63,6 +76,10 @@ class PyPi(Cog):
else:
embed.description = "No summary provided."
+ upload_date = _get_latest_distribution_timestamp(response_json)
+ if upload_date:
+ embed.description += f"\n\nReleased on {upload_date}."
+
error = False
else: