API Reference

API Reference

Complete reference for the swimrankings package (v0.3.0, Python 3.10+).

Package Exports

Top-level imports exposed by swimrankings:

from swimrankings import (
    # Core classes & data models
    Athletes,
    Athlete,
    PersonalBest,
    AthleteDetails,
    # Authentication & session helpers
    login,
    set_auth,
    get_auth,
    # Reference data helpers
    fetch_countries,
    fetch_time_periods,
    get_country_code,
    get_country_name,
    find_nation_id_by_code,
    get_available_years,
    get_months_for_year,
    get_cached_countries,
    get_cached_time_periods,
    clear_cache,
    # Exceptions
    SwimRankingsError,
    AthleteNotFoundError,
    NetworkError,
    AuthenticationError,
)

Exceptions exported from swimrankings.exceptions (not re-exported at top level):

from swimrankings.exceptions import InvalidGenderError, ParseError

Low-level HTTP and utility modules:

  • swimrankings.http.get(url, **kwargs) — Cloudflare-aware GET helper sharing the module session.
  • swimrankings.utils.is_valid_time(time_str) — Validates swimming time string formatting (SS.CC, M:SS.CC, MM:SS.CC, or H:MM:SS.CC).

Core Classes

Athletes

Searches swimrankings.net (internalRequest=athleteFind) on initialization and exposes the matched Athlete objects as an iterable sequence.

class Athletes:
    def __init__(
        self,
        name: str,
        gender: str = "all",
        club_id: int = -1,
        timeout: int = 30,
        auth: Optional[Tuple[str, str]] = None,
    ) -> None: ...

Parameters:

ParameterTypeDefaultDescription
namestr—Last name to search for (leading/trailing whitespace is stripped).
genderstr"all"Gender filter: "all", "male", or "female" (case-insensitive).
club_idint-1Swimrankings club ID filter (-1 searches all clubs).
timeoutint30HTTP request timeout in seconds.
authOptional[Tuple[str, str]]NoneOptional (email, password) credentials propagated to each returned Athlete.

Raises: InvalidGenderError, NetworkError (or AuthenticationError), ParseError, AthleteNotFoundError.

Sequence & Container Protocol:

  • len(athletes) — Number of matched athletes.
  • for athlete in athletes — Iterates over matched Athlete instances.
  • athletes[index] — Supports integer indexing and slicing (List[Athlete]).
  • bool(athletes) — True when at least one athlete was found.

Methods:

  • filter_by_country(country_code: str) -> List[Athlete] — Case-insensitive filter on 3-letter country code (e.g., "BEL", "USA").
  • filter_by_birth_year(min_year: int, max_year: Optional[int] = None) -> List[Athlete] — Inclusive birth-year filter; matches a single year when max_year is None.
  • filter_by_gender(gender: str) -> List[Athlete] — Case-insensitive filter on "male" or "female".
  • to_dict() -> List[Dict[str, Any]] — Returns a list of dicts with keys athlete_id, full_name, first_name, last_name, birth_year, gender, country, club, and profile_url.

Athlete

Represents a single swimmer from search results. Two Athlete instances are equal and hash identically when their athlete_id matches.

Attributes & Properties:

MemberTypeDescription
athlete_idintUnique Swimrankings athlete ID.
full_namestrFull name in "Last, First" format.
first_namestr (property)Extracted first name (or "" if unavailable).
last_namestr (property)Extracted last name.
birth_yearintYear of birth (0 if unavailable).
genderstr"male", "female", or "unknown".
countrystr3-letter country code (e.g., "BEL").
clubstrClub label (e.g., "BEL - TiMe Swimming Team").
profile_urlstrFull URL to the athlete detail page.
authOptional[Tuple[str, str]]Default (email, password) credentials inherited from Athletes.

Methods:

athlete.get_details(timeout: int = 30, auth: Optional[Tuple[str, str]] = None) -> AthleteDetails

Fetches and parses the athlete's detail page (personal bests and profile metadata).

  • Credential precedence: per-call auth argument → athlete.auth → set_auth() / login() → environment variables.
  • Raises: AuthenticationError (when protected and unauthenticated or rejected), NetworkError, ParseError.

Data Models

AthleteDetails

@dataclass returned by Athlete.get_details().

FieldTypeDescription
athlete_idintUnique Swimrankings athlete ID.
personal_bestsList[PersonalBest]Parsed personal best times.
profile_infoDict[str, Any]Parsed profile fields (header_name, country_code, country_name, club_name, photo_url, and table/label entries).
last_updateddatetimeLocal timestamp (datetime.now()) when details were fetched.

PersonalBest

@dataclass representing one personal best entry.

FieldTypeDefaultDescription
eventstr—Event name (e.g., "50m Freestyle").
coursestr—Pool course: "LCM" (mapped from "50m"), "SCM" (mapped from "25m"), "SCY", or "Unknown".
timestr—Formatted swim time (e.g., "24.50", "1:52.34").
dateOptional[str]NoneDate achieved (None when "-" or absent).
meetOptional[str]NoneMeet name (None when "-" or absent).
locationOptional[str]NoneMeet city/location (None when "-" or absent).

Authentication & HTTP

login(username: str, password: str, timeout: int = 30) -> bool

Submits the Swimrankings web login form (POST https://www.swimrankings.net/index.php) to establish an authenticated session and stores (username, password) globally via set_auth().

  • Returns True on success.
  • Clears stored global credentials (set_auth(None, None)) and raises AuthenticationError if swimrankings.net rejects the login.
  • Raises NetworkError on connection or unexpected HTTP failures.

set_auth(username: Optional[str] = None, password: Optional[str] = None) -> None

Sets process-wide HTTP Basic Auth credentials on the HTTP session. Pass (None, None) to clear stored credentials.

get_auth() -> Optional[Tuple[str, str]]

Returns the active (username, password) tuple from set_auth()/login(), or falls back to SWIMRANKINGS_USERNAME (or SWIMRANKINGS_EMAIL) and SWIMRANKINGS_PASSWORD environment variables. Returns None if neither is configured.

swimrankings.http.get(url: str, **kwargs: Any)

Performs an HTTP GET using the shared curl_cffi session, automatically solving Cloudflare challenges with a local Chrome/Chromium browser via nodriver and caching clearance (cf_state.json) for up to 2 hours (CACHE_MAX_AGE = 7200, BROWSER_TIMEOUT = 45). Do not pass a custom User-Agent header.


Reference Data

Country Helpers

  • fetch_countries() -> Dict[str, Tuple[str, str]] — Fetches nation IDs mapped to (code, name) (e.g., {"43": ("BEL", "Belgium")}). Raises NetworkError or ParseError.
  • get_country_code(nation_id: str) -> Optional[str] — Returns the 3-letter country code for nation_id, or None if missing or on NetworkError/ParseError.
  • get_country_name(nation_id: str) -> Optional[str] — Returns the country name for nation_id, or None if missing or on NetworkError/ParseError.
  • find_nation_id_by_code(country_code: str) -> Optional[str] — Case-insensitive lookup returning the nation_id for a 3-letter code, or None if missing or on error.

Time Period Helpers

  • fetch_time_periods() -> Dict[str, str] — Fetches period codes mapped to display names (e.g., {"2025_m07": "2025 - juli"}). Raises NetworkError or ParseError.
  • get_available_years() -> List[int] — Returns available years sorted descending, or [] on error.
  • get_months_for_year(year: int) -> Dict[str, str] — Returns {period_code: display_name} for year, or {} on error.

In-Memory Caching

  • get_cached_countries() -> Dict[str, Tuple[str, str]] — Cached wrapper around fetch_countries().
  • get_cached_time_periods() -> Dict[str, str] — Cached wrapper around fetch_time_periods().
  • clear_cache() -> None — Clears both in-memory reference data caches.

Exceptions

SwimRankingsError
├── NetworkError
│   └── AuthenticationError
├── AthleteNotFoundError
├── InvalidGenderError     (import from swimrankings.exceptions)
└── ParseError             (import from swimrankings.exceptions)

Note: Catch AuthenticationError before NetworkError whenever handling both.


Environment Variables

VariablePurposeDefault
SWIMRANKINGS_USERNAMESwimrankings Member email/username—
SWIMRANKINGS_EMAILAlias for SWIMRANKINGS_USERNAME—
SWIMRANKINGS_PASSWORDSwimrankings Member password—
SWIMRANKINGS_BROWSER_PATHPath to a specific Chrome/Chromium executableAuto-detected by nodriver
SWIMRANKINGS_CACHE_DIRDirectory storing Cloudflare clearance (cf_state.json)~/.cache/swimrankings
DISPLAYX11 display on Linux; starts xvfb via pyvirtualdisplay (1440x900) when unsetSystem env