Skip to content

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

leet-to-github

Automatically archive your LeetCode solutions to a GitHub repo — no n8n, no low-code tools, just Python.

Every time you solve a problem on LeetCode, running this script pushes:

  • The problem description (with example testcases) as problem.md
  • Your actual accepted solution code, versioned by attempt number and date

...straight to a GitHub repo, so your solving history lives somewhere permanent and browsable.

Why this exists

I kept manually copy-pasting solutions into GitHub after solving problems. This automates that — but stays honest about what it actually is: a manually-triggered sync script, not a background daemon. You run it, it checks your recent accepted submissions, and pushes anything new.

How it works

  1. Hits LeetCode's unofficial GraphQL endpoint (leetcode.com/graphql) — there's no official public API for this — authenticated via your session cookie.
  2. Fetches your recent accepted submissions, filters out anything already synced (tracked locally in sync_state.json).
  3. For each new submission: fetches the full code + the problem description, converts the description HTML to Markdown, and pushes both to GitHub via the Contents API (no local git clone needed).

Repo output structure

your-solutions-repo/
├── two-sum/
│   ├── problem.md
│   └── solutions/
│       ├── solution_1_2026-07-15.py
│       └── solution_2_2026-07-20.py
├── add-two-numbers/
│   ├── problem.md
│   └── solutions/
│       └── solution_1_2026-07-16.java
  • problem.md is overwritten each time (the description doesn't change).
  • Every new accepted submission for the same question gets a new versioned file — previous attempts are never overwritten. Useful for seeing how your approach to a problem evolved over time.
  • File extension is chosen automatically based on the submission's language.

Setup

1. Create a virtual environment

python -m venv venv
venv\Scripts\activate        # Windows

2. Install dependencies

pip install -r requirements.txt

3. Configure .env

Copy .env.example to .env and fill in:

Variable Where to get it
LEETCODE_SESSION Browser DevTools → Cookies → leetcode.com → LEETCODE_SESSION value
LEETCODE_CSRF_TOKEN Same place → csrftoken value
LEETCODE_USERNAME Your LeetCode profile URL: leetcode.com/u/<username>/
GITHUB_TOKEN GitHub → Settings → Developer settings → Fine-grained PAT, scoped to Contents: read/write on one repo only
GITHUB_REPO Format: username/repo-name
GITHUB_BRANCH Defaults to main

Never commit .env — it's already covered by .gitignore.

⚠️ LEETCODE_SESSION is a login session, not a long-lived token. It will expire periodically and needs to be manually refreshed from DevTools when the script starts failing with an auth error.

4. Run it

python sync.py

That's it — one command. It checks your most recent accepted submissions (default: last 20, configurable via SUBMISSIONS_PER_POLL), skips anything already synced, and pushes the rest.

Usage

This is manually triggered, by design — run python sync.py after you solve a problem (or whenever you want to catch up). There's no background loop or scheduler set up by default; run_once() is the intended entry point, not run_forever().

If you want it to run automatically, wire it up to Windows Task Scheduler (or cron elsewhere) to call python sync.py on an interval — but note this means failures (e.g. an expired session cookie) can go unnoticed until you check.

Known limitations

  • Unofficial API. LeetCode has no public API for this. The GraphQL schema could change without notice — there's no SLA. If a call that used to work suddenly errors, this is the first thing to suspect.
  • Session expiry. LEETCODE_SESSION needs periodic manual refresh — there's no auto-login flow.
  • Only example testcases. Hidden judge testcases are intentionally not captured — only the example testcases shown in the problem description.
  • HTML→Markdown conversion. markdownify handles most LeetCode HTML cleanly, but edge cases exist — for example, <sup> tags (used for exponents like 10^9) are stripped by default and were patched to convert to ^ notation before conversion. Watch for similarly unusual HTML in problem descriptions.

Project structure

leet-to-github/
├── config.py            # loads and validates env vars
├── state.py              # local JSON state (dedup + version counters)
├── leetcode_client.py    # unofficial LeetCode GraphQL client
├── github_pusher.py      # GitHub Contents API wrapper
├── sync.py                # orchestrates everything, run_once() entry point
├── requirements.txt
├── .env.example
└── sync_state.json       # generated at runtime, not committed

Requirements

  • requests
  • python-dotenv
  • markdownify

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages