This file provides guidance to agents when working with code in this repository.
Unofficial Python client for the TooGoodToGo API. Published on PyPI as tgtg. Python 3.11+. Licensed under GPL-3.0.
# Install dependencies (requires uv) pipx install uv # Then install project deps uv sync # Run all tests (with coverage) make test # Run a single test file uv run pytest tests/test_login.py # Run a single test function uv run pytest tests/test_login.py::test_login_success # Run linting (ruff check + format) make lint # Build and publish to PyPI make publish
- Formatter: Ruff (uses Black-compatible formatting)
- Linter: Ruff (selects E, F, W rules)
- All enforced via
make lint
The entire library is a single class TgtgClient in tgtg/__init__.py (~522 lines). There are no subpackages or complex abstractions.
tgtg/__init__.py-TgtgClientclass plus all API endpoint constants. Every public method callsself.login()first, which handles token refresh automatically. The client usesrequests.Sessionfor HTTP, with Android user-agent spoofing. All API calls go through the central_post()method which handles DataDome cookie management and 403 retry logic.tgtg/exceptions.py- Three exception types:TgtgLoginError,TgtgAPIError,TgtgPollingError.tgtg/google_play_scraper.py- Scrapes the Google Play Store page to get the current TooGoodToGo APK version for user-agent strings. Falls back toDEFAULT_APK_VERSION.
TooGoodToGo uses DataDome for bot protection. The client handles this by:
- Fetching a
datadomecookie fromhttps://api-sdk.datadome.co/sdk/before API requests, mimicking the Android app's DataDome SDK integration (device fingerprint parameters like model, OS version, screen size). - The
_post()method automatically ensures a DataDome cookie is present. On 403, it invalidates the cookie and retries with a fresh one. - The
_generate_datadome_cid()helper creates random 120-char client IDs matching DataDome's expected format. - VPN/datacenter IPs are frequently blocked by DataDome regardless of cookie validity — residential IPs work best.
- Email-based login sends a request to
auth/v5/authByEmail - User receives a PIN code via email
- Client submits PIN via
auth/v5/authByRequestPinto complete authentication - Fallback: client can also poll
auth/v5/authByRequestPollingId(legacy link-click flow) - On success, stores
access_token,refresh_token, andcookie - Tokens auto-refresh after
access_token_lifetime(default 4 hours)
Auth endpoints use v5, item endpoints use v8, order endpoints use v8, token refresh uses v1, favorites use v1, discover uses v1.
- Framework: pytest with responses library for HTTP mocking
- Coverage: branch coverage enabled, configured in
pyproject.toml(addopts = --cov=tgtg) - Tests in
tests/use fixtures fromconftest.pythat mock auth/refresh endpoints tests/constants.pycontains expected API response property lists and fake token credentialstest_api.pycontains integration tests marked@pytest.mark.withoutresponsesthat hit the real API (requiresTGTG_EMAILenv var)- Time-dependent tests use freezegun for mocking
datetime.datetime.now()