Skip to content

Commit

Permalink
Use single method for both RSS feeds (#5)
Browse files Browse the repository at this point in the history
* use single method for both rss feeds

Signed-off-by: Bradley Reynolds <[email protected]>

* deps: add ruff

Signed-off-by: Bradley Reynolds <[email protected]>

---------

Signed-off-by: Bradley Reynolds <[email protected]>
  • Loading branch information
shenanigansd authored Apr 15, 2023
1 parent 06e90b9 commit d70a5ca
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
3 changes: 3 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
Changelog
=========

- :release:`2.0.0 <14th April 2023>`
- :feature:`4` Use single method for all RSS feeds

- :release:`1.0.0 <29th March 2023>`
- :feature:`1` Initialize package
14 changes: 7 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "letsbuilda-pypi"
version = "1.0.0"
version = "2.0.0"
description = "A wrapper for PyPI's API and RSS feed"
authors = [
{ name = "Bradley Reynolds", email = "[email protected]" },
Expand All @@ -21,7 +21,7 @@ documentation = "https://docs.letsbuilda.dev/letsbuilda-pypi/"
dev = [
"black",
"isort",
"pylint",
"ruff",
]
tests = [
"pytest",
Expand All @@ -42,15 +42,15 @@ build-backend = "setuptools.build_meta"
copyright = "Let's build a ..."
author = "Bradley Reynolds"

[tool.isort]
profile = "black"

[tool.black]
target-version = ["py310"]
line-length = 120

[tool.isort]
profile = "black"
[tool.ruff]
line-length = 120

[tool.pytest.ini_options]
addopts = "tests -r a -v --doctest-modules src"

[tool.pylint.format]
max-line-length = 120
25 changes: 9 additions & 16 deletions src/letsbuilda/pypi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

__all__: list[str] = [
"PyPIServices",
"NewPackageMetadata",
"PackageMetadata",
]


Expand All @@ -20,7 +20,7 @@ def _parse_publication_date(publication_date: str) -> datetime:


@dataclass(frozen=True, slots=True)
class NewPackageMetadata:
class PackageMetadata:
"""Package metadata"""

title: str
Expand All @@ -31,14 +31,14 @@ class NewPackageMetadata:
publication_date: datetime

@classmethod
def build_from(cls, data: dict[str, str]) -> "NewPackageMetadata":
def build_from(cls, data: dict[str, str]) -> "PackageMetadata":
"""Build an instance from raw data"""
publication_date: str | None = data.get("pubDate")
if publication_date is not None:
publication_date: datetime = _parse_publication_date(publication_date)

return cls(
title=data.get("title").removesuffix(" added to PyPI"),
title=data.get("title").split()[0],
package_link=data.get("link"),
guid=data.get("guid"),
description=data.get("description"),
Expand All @@ -53,19 +53,12 @@ class PyPIServices:
NEWEST_PACKAGES_FEED_URL: Final[str] = "https://pypi.org/rss/packages.xml"
PACKAGE_UPDATES_FEED_URL: Final[str] = "https://pypi.org/rss/updates.xml"

def __init__(self, http_session: ClientSession | None = None) -> None:
self.http_session = http_session or ClientSession()
def __init__(self, http_session: ClientSession) -> None:
self.http_session = http_session

async def get_new_packages_feed(self) -> list[NewPackageMetadata]:
async def get_rss_feed(self, feed_url: str) -> list[PackageMetadata]:
"""Get the new packages RSS feed"""
async with self.http_session.get(self.NEWEST_PACKAGES_FEED_URL) as response:
async with self.http_session.get(feed_url) as response:
response_text = await response.text()
rss_data = xmltodict.parse(response_text)["rss"]["channel"]["item"]
return [NewPackageMetadata.build_from(package_data) for package_data in rss_data]

async def get_package_updates_feed(self) -> dict:
"""Get the package updates RSS feed"""
async with self.http_session.get(self.PACKAGE_UPDATES_FEED_URL) as response:
response_text = await response.text()
rss_data = xmltodict.parse(response_text)["rss"]["channel"]["item"]
return rss_data
return [PackageMetadata.build_from(package_data) for package_data in rss_data]
8 changes: 4 additions & 4 deletions tests/test_new_packages_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from datetime import datetime

from letsbuilda.pypi import NewPackageMetadata
from letsbuilda.pypi import PackageMetadata


def test_parse_publication_date() -> None:
Expand All @@ -15,7 +15,7 @@ def test_parse_publication_date() -> None:
"author": "[email protected]",
"pubDate": "Wed, 29 Mar 2023 21:30:05 GMT",
}
parsed_data = NewPackageMetadata.build_from(data)
parsed_data = PackageMetadata.build_from(data)
assert parsed_data.publication_date == datetime(2023, 3, 29, 21, 30, 5)


Expand All @@ -28,7 +28,7 @@ def test_author_missing() -> None:
"description": "a test package",
"pubDate": "Wed, 29 Mar 2023 21:30:05 GMT",
}
parsed_data = NewPackageMetadata.build_from(data)
parsed_data = PackageMetadata.build_from(data)
assert parsed_data.author is None


Expand All @@ -41,5 +41,5 @@ def test_description_missing() -> None:
"author": "[email protected]",
"pubDate": "Wed, 29 Mar 2023 21:30:05 GMT",
}
parsed_data = NewPackageMetadata.build_from(data)
parsed_data = PackageMetadata.build_from(data)
assert parsed_data.description is None

0 comments on commit d70a5ca

Please sign in to comment.