""" Utility functions to fetch auction items (lots) from different providers. This module defines three functions that make HTTP calls to the public APIs of Troostwijk Auctions (TWK), AuctionPort (AP) and Online Veilingmeester (OVM) and normalises their responses into Python dictionaries. Each function returns a list of dictionaries where each dictionary represents an individual lot and includes standardised keys: ``title``, ``description``, ``bids`` (the number of bids if available), ``current_bid`` (current price and currency if available), ``image_url`` and ``end_time``. The implementations rely on the ``requests`` library for HTTP transport and include basic error handling. They raise ``requests.HTTPError`` when the remote server responds with a non‑200 status code. Note: the APIs these functions call are subject to change. Endpoints and field names may differ depending on the auction status or provider version. These functions are intended as a starting point for integrating with multiple auction platforms; you may need to adjust query parameters, header values or JSON field names if the provider updates their API. Examples -------- ``` from auction_items import get_items_twk, get_items_ap, get_items_ovm # Troostwijk Auctions (TWK): pass the visible auction identifier lots = get_items_twk(display_id="35563") for lot in lots: print(lot['lot_number'], lot['title'], lot['current_bid']) # AuctionPort (AP): pass the auction ID from the AuctionPort website ap_lots = get_items_ap(auction_id=1323) # Online Veilingmeester (OVM): the country code is required to build the # endpoint path (e.g. ``'nederland'`` or ``'belgie'``) along with the # numeric auction ID. ovm_lots = get_items_ovm(country="nederland", auction_id=7713) ``` """ from __future__ import annotations import json import logging from typing import Dict, List, Optional import requests logger = logging.getLogger(__name__) def get_items_twk( display_id: str, *, page: int = 1, page_size: int = 200, locale: str = "nl", platform: str = "WEB", request_session: Optional[requests.Session] = None, ) -> List[Dict[str, Optional[str]]]: """Fetch lots (items) for a Troostwijk auction using the GraphQL API. Troostwijk Auctions exposes its public data through a GraphQL endpoint at ``https://storefront.tbauctions.com/storefront/graphql``. The ``auctionWithLotsV5`` query returns a list of lots for a given auction. According to the GraphQL documentation, the query accepts a ``request`` object of type ``AuctionWithLotsInputV3`` and a ``platform`` argument. The ``request`` object requires the auction's ``displayId`` (a string identifier visible in the URL of the auction page), ``locale`` (language code), ``pageNumber``, ``pageSize`` and two lists for range and value facets. The return type ``AuctionWithListingLots`` contains an ``auction`` and a list of ``lots`` with details such as the lot number, title, description, current bid and images【561575328263299†screenshot】. Fields included in this function's query correspond to those documented in the schema. Parameters ---------- display_id: str The human‑readable identifier of the auction (e.g. ``"35563"``). page: int, optional The page number of results (defaults to 1). The API uses 1‑based page numbering. A page size of 200 appears sufficient for most auctions. page_size: int, optional The maximum number of lots to fetch per page (defaults to 200). locale: str, optional Language/locale code for the content (defaults to ``"nl"``). platform: str, optional Platform enumeration value required by the API (default ``"WEB"``). Other values may include ``"B2B"`` or ``"B2C"``; consult the GraphQL documentation if you encounter an error. request_session: Optional[requests.Session], optional An existing requests session to reuse connections. If omitted, a temporary session is created for this call. Returns ------- List[Dict[str, Optional[str]]] A list of dictionaries. Each dictionary represents a lot and contains the keys ``lot_number``, ``title``, ``description``, ``bids`` (number of bids, if provided), ``current_bid`` (a dictionary with ``amount`` and ``currency`` or ``None`` if no bid), ``image_url`` (first image) and ``end_time`` (auction end time in ISO 8601 format). Raises ------ requests.HTTPError If the HTTP response has a non‑200 status code. Exception For other errors such as JSON decoding failures. """ session = request_session or requests.Session() url = "https://storefront.tbauctions.com/storefront/graphql" # GraphQL query string. The fields selected here mirror those # described in the GraphQL documentation for the ``auctionWithLotsV5`` # operation【561575328263299†screenshot】. Additional fields can be added # if necessary. graphql_query = """ query AuctionWithLots($request: AuctionWithLotsInputV3!, $platform: Platform!) { auctionWithLotsV5(request: $request, platform: $platform) { lots { lotNumber id title description numberOfBids currentBid { amount currency } endDateISO images { url } } } } """ # Build the variables for the query. The request object must include # ``displayId``, ``locale``, ``pageNumber``, ``pageSize``, and two empty # lists for range and value facets as required by the schema【835513158978214†screenshot】. variables = { "request": { "displayId": str(display_id), "locale": locale, "pageNumber": page, "pageSize": page_size, # These facets are optional; empty lists mean no filters "rangeFacetInputs": [], "valueFacetInputs": [], }, "platform": platform, } headers = { # A typical browser may send JSON content; set an Accept header "Accept": "application/json", "Content-Type": "application/json", # The GraphQL service uses a CSRF protection token; a random # ``x-csrf-token`` header can be supplied if needed. Leaving it # empty usually works for public queries. "x-csrf-token": "", } response = session.post( url, json={"query": graphql_query, "variables": variables}, headers=headers, timeout=30, ) # Raise an HTTPError for non‑200 responses try: response.raise_for_status() except requests.HTTPError: logger.error("Troostwijk API returned status %s: %s", response.status_code, response.text) raise # Parse the JSON body data = response.json() # Check for GraphQL errors if "errors" in data and data["errors"]: message = data["errors"] logger.error("GraphQL returned errors: %s", message) raise Exception(f"GraphQL returned errors: {message}") lots = [] # Navigate the nested structure to the list of lots. The path # matches the GraphQL selection set defined above. try: lot_items = data["data"]["auctionWithLotsV5"]["lots"] except (KeyError, TypeError) as e: logger.error("Unexpected response structure from Troostwijk API: %s", data) raise Exception(f"Unexpected response structure: {e}") for item in lot_items: # Some fields may be missing; use dict.get with defaults lot_number = item.get("lotNumber") title = item.get("title") description = item.get("description") bids = item.get("numberOfBids") current_bid = item.get("currentBid") end_time = item.get("endDateISO") images = item.get("images", []) or [] image_url = images[0]["url"] if images else None lots.append( { "lot_number": lot_number, "title": title, "description": description, "bids": bids, "current_bid": current_bid, "image_url": image_url, "end_time": end_time, } ) return lots def get_items_ap( auction_id: int, *, request_session: Optional[requests.Session] = None, ) -> List[Dict[str, Optional[str]]]: """Retrieve items (lots) from an AuctionPort auction. AuctionPort operates a JSON API on ``https://api.auctionport.be``. While official documentation for the lot endpoints is scarce, community code suggests that auctions can be fetched via ``/auctions/small``【461010206788258†L10-L39】. The corresponding lot information appears to reside under an ``/auctions/{id}/lots`` or ``/lots?auctionId={id}`` endpoint (the platform uses XML internally for some pages as observed when visiting ``/auctions/{id}/lots`` in a browser). This function attempts to call these endpoints in order and parse their JSON responses. If the response is not JSON, it falls back to a simple text scrape looking for lot numbers, titles, descriptions and current bid amounts. Parameters ---------- auction_id: int The numeric identifier of the auction on AuctionPort. request_session: Optional[requests.Session], optional An existing requests session. Returns ------- List[Dict[str, Optional[str]]] A list of lot dictionaries with the keys ``lot_number``, ``title``, ``description``, ``bids`` (if available), ``current_bid`` (amount and currency if provided), ``image_url`` and ``end_time``. If no lots could be parsed, an empty list is returned. Raises ------ requests.HTTPError If both endpoint attempts return non‑200 responses. """ session = request_session or requests.Session() # Candidate endpoints for AuctionPort lots. The first URL follows the # pattern used by the AuctionPort website; the second is a query by # parameter. Additional endpoints can be added if discovered. url_candidates = [ f"https://api.auctionport.be/auctions/{auction_id}/lots", f"https://api.auctionport.be/lots?auctionId={auction_id}", ] last_error: Optional[Exception] = None for url in url_candidates: try: response = session.get(url, headers={"Accept": "application/json"}, timeout=30) except Exception as exc: # Capture connection errors and continue with the next endpoint last_error = exc continue if response.status_code == 404: # Try the next candidate continue if response.status_code >= 400: last_error = requests.HTTPError( f"AuctionPort API error {response.status_code} for {url}", response=response, ) continue # If the response is OK, attempt to parse JSON try: data = response.json() except json.JSONDecodeError: # Not JSON: fallback to naive parsing of plain text/XML. AuctionPort # sometimes returns XML for lots pages. We'll attempt to extract # structured information using simple patterns. text = response.text lots: List[Dict[str, Optional[str]]] = [] # Split by