Skip to content

Commit

Permalink
replace internal pypi wrapper with letsbuilda-pypi (#36)
Browse files Browse the repository at this point in the history
Signed-off-by: Bradley Reynolds <[email protected]>
  • Loading branch information
shenanigansd authored Apr 17, 2023
1 parent 2ff8b91 commit fc8c6a8
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 85 deletions.
3 changes: 3 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
Changelog
=========

- :release:`1.5.1 <17th April 2023>`
- :feature:`30` Replace PyPI parser with letsbuilda-pypi

- :release:`1.5.0 <16th April 2023>`
- :feature:`31` Update PyPI package report email to match Inspector's new template

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "alessia"
version = "1.5.0"
version = "1.5.1"
dependencies = [
"python-dotenv==1.0.0",
"discord.py==2.2.2",
Expand Down
1 change: 0 additions & 1 deletion src/bot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class _PyPi(EnvConfig):

EnvConfig.Config.env_prefix = "pypi_"

rss_feed_url = "https://pypi.org/rss/packages.xml"
show_author_in_embed = False


Expand Down
4 changes: 3 additions & 1 deletion src/bot/exts/dragonfly/dragonfly.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def __init__(self, *, email_template: Template, package: PackageScanResult) -> N
self.package = package

async def on_submit(self, interaction: discord.Interaction):
content = self.email_template.render(package=self.package, description=self.description.value, rules=", ".join(self.package.matches))
content = self.email_template.render(
package=self.package, description=self.description.value, rules=", ".join(self.package.matches)
)

log.info(
"Sending report to with sender %s with recipient %s with bcc %s",
Expand Down
16 changes: 8 additions & 8 deletions src/bot/exts/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@

import discord
from discord.ext import commands
from letsbuilda.pypi import PackageMetadata, PyPIServices

from bot.bot import Bot
from bot.constants import PyPiConfigs
from bot.pypi import get_packages
from bot.pypi.pypi import Package


class EmbedPaginator:
"""Paginate embeds"""

def __init__(self, packages: list[Package], per_page: int) -> None:
def __init__(self, packages: list[PackageMetadata], per_page: int) -> None:
self.idx = 0
self.per_page = per_page
self.packages = packages
self.embeds = self._build_embeds()

def _batched(self) -> Generator[list[Package], None, None]:
def _batched(self) -> Generator[list[PackageMetadata], None, None]:
it = iter(self.packages)
while True:
batch = list(islice(it, self.per_page))
Expand All @@ -48,13 +47,13 @@ def _build_embeds(self) -> list[discord.Embed]:
"",
discord.utils.format_dt(package.publication_date),
f"[Package Link]({package.package_link})",
f"[Inspector Link]({package.inspector_link})",
f"[Inspector Link](https://inspector.pypi.io/project/{package.title})",
package.author if PyPiConfigs.show_author_in_embed and package.author else "",
)
),
)

embed.set_footer(text=f"Page {page_number+1}/{ceil(len(self.packages) / self.per_page)}")
embed.set_footer(text=f"Page {page_number + 1}/{ceil(len(self.packages) / self.per_page)}")

embeds.append(embed)

Expand Down Expand Up @@ -90,7 +89,7 @@ def last(self) -> None:
class PackageViewer(discord.ui.View):
"""Package viewer"""

def __init__(self, *, packages: list[Package], author: discord.User | discord.Member) -> None:
def __init__(self, *, packages: list[PackageMetadata], author: discord.User | discord.Member) -> None:
self.paginator = EmbedPaginator(packages, per_page=3)
self.author = author
self.message: discord.Message | None = None
Expand Down Expand Up @@ -140,7 +139,8 @@ def __init__(self, bot: Bot):

@commands.command()
async def pypi(self, ctx: commands.Context) -> None:
packages = await get_packages(self.bot.http_session)
client = PyPIServices(http_session=self.bot.http_session)
packages = await client.get_rss_feed(client.NEWEST_PACKAGES_FEED_URL)
view = PackageViewer(packages=packages, author=ctx.author)
message = await ctx.send(embed=view.paginator.current, view=view)
view.message = message
Expand Down
3 changes: 0 additions & 3 deletions src/bot/pypi/__init__.py

This file was deleted.

71 changes: 0 additions & 71 deletions src/bot/pypi/pypi.py

This file was deleted.

0 comments on commit fc8c6a8

Please sign in to comment.