A minimal CLI tool to:
- Pull LeetCode problems offline (Markdown + starter solution).
- Generate pytest tests via Gemini AI (or use a local stub).
- Run tests locally with pytest.
- Submit solutions to LeetCode via Playwright automation (stores auth state).
This README shows how to set up and use the tool as a complete beginner.
- Python 3.10+ installed and on PATH
- Git (optional)
- Windows PowerShell (instructions assume PowerShell)
- A Google Gemini API key (optional — tool falls back to a stub if not provided)
- Optional: LeetCode credentials or Playwright auth state (for automated submit)
PowerShell (Windows):
iwr -useb https://raw.githubusercontent.com/0xunLin/leetcode-cli-alpha/main/scripts/remote.ps1 | iexBash (Linux / macOS):
curl -sSL https://raw.githubusercontent.com/0xunLin/leetcode-cli-alpha/main/scripts/remote.sh | bashNotes:
- The bootstrap scripts create a virtual environment (.venv), install Python dependencies, install Playwright browsers, and create a
.envfrom.env.exampleif missing. - Review the script before running a remote one-liner. Remote execution is convenient but has security risks.
Open PowerShell in the project folder c:\Users\DELL\Desktop\LC_at and run:
# 1. Create & activate virtual environment
python -m venv .venv
.\.venv\Scripts\Activate.ps1
# 2. Install Python dependencies
pip install -r requirements.txt
pip install beautifulsoup4 python-dotenv
# 3. Install Playwright browsers (required for submit automation)
python -m playwright installCreate .env from .env.example:
copy .env.example .envThen open .env and set values:
GEMINI_API_KEY— (optional) your Gemini / Google GenAI API keyGEMINI_MODEL/GEMINI_ENDPOINT— only if you have a different model/endpointLEETCODE_EMAIL/LEETCODE_PASSWORD— optional for first-time login (or use auth state)LEETCODE_AUTH_STATE— optional path to Playwright storage state JSON
Security: never commit .env, auth files, or API keys.
Run the helper to log in once and save auth state:
# activate venv first if not active
.\.venv\Scripts\Activate.ps1
python save_playwright_auth.py
# Follow the interactive instructions to sign in in the opened browser.
# This produces playwright_auth.json in the project root by default.Set LEETCODE_AUTH_STATE in .env to the saved file path if desired.
Activate the venv:
.\.venv\Scripts\Activate.ps1Commands (run from project root):
-
Pull a problem (uses cache by default):
python cli.py pull two-sum -
Force-refresh from LeetCode GraphQL (ignore cache):
python cli.py pull two-sum --force-refresh
-
Run tests (pytest):
python cli.py test two-sum -
Submit solution to LeetCode (uses Playwright; headless by default):
python cli.py submit two-sum
Files created per problem:
problems/<slug>/
README.md # problem statement (markdown)
solution.py # starter / your code
test_solution.py # tests generated by AI or stub
.cache/<slug>.json # cached GraphQL response
playwright_auth.json # Playwright auth state (if created)
- If
GEMINI_API_KEYis set, the tool sends the problem text to the configured Gemini REST endpoint and requests a JSON object with keysreadme,starter, andtests. Thetestscontent is written totest_solution.py. - If no API key is provided or the request fails, a small fallback pytest stub is written so you can still run the flow locally.
Tip: inspect the raw AI output if tests fail or contain invalid imports and adjust utils/ai.py parsing logic.
- GraphQL responses are cached to
.cache/<slug>.json. - Default
pulluses cache. Use--force-refreshto fetch fresh and overwrite cache. - To clear cache manually, remove
.cachefiles.
- Pylance / VS Code import errors: ensure VS Code Python interpreter is set to
.venv\Scripts\python.exe(Ctrl+Shift+P → Python: Select Interpreter). ImportError: playwright— runpip install playwrightin the active environment andpython -m playwright install.- If submission fails due to UI changes, run submit headful (edit
submit_solution(..., headless=False)) and debug selectors in the opened browser. - If Gemini responses are not parsed correctly, enable debug prints in
utils/ai.pyto see raw response text.
- Do not commit API keys, auth state, or
.envto source control. - Automating submissions to LeetCode may violate LeetCode Terms of Service. Use the submit automation responsibly; consider manual submission if unsure.
- Add
cli cache-clearandcli cache-list. - Improve AI prompt and validation of generated tests (ensure
from solution import Solution). - Add support for other languages (TypeScript / Java / C++).
- Add unit tests for CLI commands and parsing functions.
If something breaks, open an issue or ask for help with:
- Exact error trace or terminal output
- The problem slug you tested with
- Whether Gemini key and Playwright auth were configured
Happy hacking — start by pulling two-sum and running the tooling.