Skip to content

Core API Reference

This page documents the core modules of Medium Converter.

Article Model

medium_converter.core.models.Article

A Medium article.

Fetcher

medium_converter.core.fetcher.fetch_article(url, cookies=None) async

Fetch a Medium article's HTML content.

PARAMETER DESCRIPTION
url

The URL of the Medium article

TYPE: str

cookies

Optional cookies for authentication

TYPE: dict[str, str] | None DEFAULT: None

RETURNS DESCRIPTION
str

HTML content of the article

Source code in medium_converter/core/fetcher.py
async def fetch_article(url: str, cookies: dict[str, str] | None = None) -> str:
    """Fetch a Medium article's HTML content.

    Args:
        url: The URL of the Medium article
        cookies: Optional cookies for authentication

    Returns:
        HTML content of the article
    """
    async with httpx.AsyncClient(follow_redirects=True, cookies=cookies) as client:
        response = await client.get(url)
        response.raise_for_status()
        result: str = response.text
        return result

Parser

medium_converter.core.parser.parse_article(html)

Parse a Medium article's HTML content.

PARAMETER DESCRIPTION
html

The HTML content of the Medium article

TYPE: str

RETURNS DESCRIPTION
Article

Structured Article object

Source code in medium_converter/core/parser.py
def parse_article(html: str) -> Article:
    """Parse a Medium article's HTML content.

    Args:
        html: The HTML content of the Medium article

    Returns:
        Structured Article object
    """
    BeautifulSoup(html, "lxml")
    # Placeholder implementation
    return Article(
        title="Sample Article Title",
        author="Sample Author",
        date="2023-01-01",
        content=[],
        estimated_reading_time=5,
    )

Authentication

medium_converter.core.auth.get_medium_cookies()

Extract Medium cookies from the user's browser.

RETURNS DESCRIPTION
dict[str, str]

Dict of cookies for Medium domain

Source code in medium_converter/core/auth.py
def get_medium_cookies() -> dict[str, str]:
    """Extract Medium cookies from the user's browser.

    Returns:
        Dict of cookies for Medium domain
    """
    try:
        cookies = browser_cookie3.chrome(domain_name="medium.com")
        return {cookie.name: cookie.value for cookie in cookies}
    except Exception:
        # Fallback to Firefox
        try:
            cookies = browser_cookie3.firefox(domain_name="medium.com")
            return {cookie.name: cookie.value for cookie in cookies}
        except Exception:
            return {}