Skip to content

Commit

Permalink
Merge pull request #67 from elliot-100/refine_get_public
Browse files Browse the repository at this point in the history
Refine `get_public_Info()`
  • Loading branch information
elliot-100 authored Nov 28, 2023
2 parents 0cd745f + ad4ac40 commit fc4a6c2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
16 changes: 9 additions & 7 deletions britishcycling_clubs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,21 @@ def get_public_club_info(club_id: str) -> PublicClubInfo:
-------
PublicClubInfo
"""
profile_page = requests.get(
f"{PROFILE_BASE_URL}{club_id}/",
timeout=REQUESTS_TIMEOUT,
)
profile_soup = BeautifulSoup(profile_page.content, "html.parser")
profile_url = f"{PROFILE_BASE_URL}{club_id}/"
r = requests.get(profile_url, timeout=REQUESTS_TIMEOUT)
r.raise_for_status()
if r.url != profile_url:
error_message = f"Redirected to unexpected URL {r.url}. Is `club_id` valid?"
raise ValueError(error_message)
profile_soup = BeautifulSoup(r.content, "html.parser")
return {
"club_name": _get_club_name_from_profile(profile_soup),
"total_members": _get_total_members_from_profile(profile_soup),
}


def _get_club_name_from_profile(soup: BeautifulSoup) -> str:
"""Return the club's name from BeautifulSoup object."""
"""Return the club's name from profile page soup."""
club_name_h1 = soup.find("h1", class_="article__header__title-body__text")

# For type-checking purposes: ensures unambiguous type is passed
Expand All @@ -168,7 +170,7 @@ def _get_club_name_from_profile(soup: BeautifulSoup) -> str:


def _get_total_members_from_profile(soup: BeautifulSoup) -> int:
"""Return the club's total members count from BeautifulSoup object."""
"""Return the club's total members count from profile page soup."""
about_div = soup.find("div", id="about")

# TypeError is raised if page other than a club profile page is returned
Expand Down
40 changes: 40 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Tests for functions."""

from bs4 import BeautifulSoup

from britishcycling_clubs import (
_get_club_name_from_profile,
_get_total_members_from_profile,
)

# Partial extract from actual page
PROFILE_PAGE_EXTRACT = """
<html>
<h1 class="article__header__title-body__text">My Cycling Club</h1>
<div class="tabs__content" id="about">
<h2>Club Information</h2>
<div class="grid-tablet-wrap grid-tablet-wide-wrap grid-desktop-wrap">
<div class="grid-tablet-col-full grid-tablet-wide-col-1-2 grid-desktop-col-1-2">
<p>
<b>Club type:</b> Club Commercial<br/>
<b>Affiliated: </b>31/12/2023<br/>
<b>Affilation status:</b> Active </p>
<p><b>Total club members:</b> 42<br/></p>
</div>
</html>
"""


def test__get_club_name_from_profile__happy_path() -> None:
"""Test that club name is returned from 'profile' soup."""
profile_soup = BeautifulSoup(PROFILE_PAGE_EXTRACT)
assert _get_club_name_from_profile(profile_soup) == "My Cycling Club"


def test__get_total_members_from_profile__happy_path() -> None:
"""Test that total members count is returned from 'profile' soup."""
profile_soup = BeautifulSoup(PROFILE_PAGE_EXTRACT)
assert _get_total_members_from_profile(profile_soup) == 42

0 comments on commit fc4a6c2

Please sign in to comment.