├── .gitignore ├── LICENSE ├── README.md ├── package-lock.json ├── package.json └── src ├── config.json.example ├── download-submissions.js └── fetch-submissions.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | 61 | # Project specific excludes 62 | config.json 63 | data 64 | submissions.json 65 | zero-submissions.json 66 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tay Yang Shun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode Downloader 2 | 3 | Download your accepted submissions from LeetCode! 4 | 5 | ## Getting Started 6 | 7 | ``` 8 | $ npm install 9 | $ cp src/config.json.example src/config.json 10 | ``` 11 | 12 | Copy your LeetCode cookie from the browser and paste it in `config.json`. Open your browser debugger, select the "Network" tab, and refresh the page. Look for the `Cookie` string under the `Request-Headers` section for the first network request made and copy the **entire value**. The string should start with `__cfduid=...` (as of Jan 18 2018). 13 | 14 | ## Usage 15 | 16 | ``` 17 | $ cd src 18 | $ node fetch-submissions.js 19 | $ node download-submissions.js 20 | ``` 21 | 22 | The first command fetches the URLs to your submissions into a `submissions.json` file. Some of your accepted questions may not have submissions if they were accepted only via contest. Those questions will be written in `zero-submissions.json`. 23 | 24 | The second command reads the submissions from `submissions.json` and fetches the code for each submission. The downloaded code will be written into the `data` directory. If the downloading hangs, kill it and restart again. LeetCode servers sometimes fail to give a response. 25 | 26 | ## License 27 | 28 | MIT 29 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "leetcode-scraper", 3 | "version": "0.0.1", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "axios": { 8 | "version": "0.16.2", 9 | "resolved": "https://registry.npmjs.org/axios/-/axios-0.16.2.tgz", 10 | "integrity": "sha1-uk+S8XFn37q0CYN4VFS5rBScPG0=", 11 | "requires": { 12 | "follow-redirects": "1.2.4", 13 | "is-buffer": "1.1.5" 14 | } 15 | }, 16 | "debug": { 17 | "version": "2.6.8", 18 | "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.8.tgz", 19 | "integrity": "sha1-5zFTHKLt4n0YgiJCfaF4IdaP9Pw=", 20 | "requires": { 21 | "ms": "2.0.0" 22 | } 23 | }, 24 | "follow-redirects": { 25 | "version": "1.2.4", 26 | "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.2.4.tgz", 27 | "integrity": "sha512-Suw6KewLV2hReSyEOeql+UUkBVyiBm3ok1VPrVFRZnQInWpdoZbbiG5i8aJVSjTr0yQ4Ava0Sh6/joCg1Brdqw==", 28 | "requires": { 29 | "debug": "2.6.8" 30 | } 31 | }, 32 | "is-buffer": { 33 | "version": "1.1.5", 34 | "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.5.tgz", 35 | "integrity": "sha1-Hzsm72E7IUuIy8ojzGwB2Hlh7sw=" 36 | }, 37 | "ms": { 38 | "version": "2.0.0", 39 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", 40 | "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "leetcode-scraper", 3 | "version": "0.0.1", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Yangshun Tay", 10 | "license": "MIT", 11 | "dependencies": { 12 | "axios": "^0.16.2" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/config.json.example: -------------------------------------------------------------------------------- 1 | { 2 | "cookie": "" 3 | } 4 | -------------------------------------------------------------------------------- /src/download-submissions.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios'); 2 | const fs = require('fs'); 3 | 4 | const TIMEOUT = 15000; 5 | const cookie = require('./config.json').cookie; 6 | if (!cookie) { 7 | console.error('Please provide your cookie in "config.json"!'); 8 | process.exit(); 9 | } 10 | const requestParams = { 11 | method: 'get', 12 | headers: { 13 | Cookie: cookie, 14 | }, 15 | }; 16 | 17 | const dataPath = '../data'; 18 | 19 | const submissions = require('./submissions.json'); 20 | const codeRegex = /submissionCode: '(.*)',\n editCodeUrl/; 21 | const extensionsMap = { 22 | bash: 'sh', 23 | c: 'c', 24 | cpp: 'cpp', 25 | csharp: 'cs', 26 | golang: 'go', 27 | java: 'java', 28 | javascript: 'js', 29 | mysql: 'sql', 30 | python: 'py', 31 | python3: 'py', 32 | ruby: 'rb', 33 | scala: 'scala', 34 | swift: 'swift', 35 | }; 36 | 37 | console.log(`Downloading ${submissions.length} submissions`); 38 | if (!fs.existsSync(dataPath)) { 39 | fs.mkdirSync(dataPath); 40 | } 41 | 42 | Promise.all( 43 | submissions.map(submission => { 44 | return new Promise(resolve => { 45 | const { id, slug, url, language } = submission; 46 | const timer = setTimeout(() => { 47 | console.log(`Timeout for ${id}-${slug}`); 48 | resolve(); 49 | }, TIMEOUT); 50 | axios 51 | .request( 52 | Object.assign(requestParams, { 53 | url: `https://leetcode.com${url}`, 54 | }) 55 | ) 56 | .then(({ data }) => { 57 | // Pad ID to 4 digits. 58 | let idStr = id.toString(); 59 | const num = idStr.length; 60 | for (let i = 0; i < 4 - num; i++) { 61 | idStr = '0' + idStr; 62 | } 63 | const matches = data.match(codeRegex); 64 | const code = 65 | matches[1].replace(/\\u[\dA-F]{4}/gi, match => { 66 | return String.fromCharCode( 67 | parseInt(match.replace(/\\u/g, ''), 16) 68 | ); 69 | }) + '\n'; 70 | const filename = `${idStr}-${slug}.${ 71 | extensionsMap[language] 72 | }`; 73 | fs.writeFileSync(dataPath + '/' + filename, code); 74 | console.log(`Downloaded ${filename}`); 75 | }) 76 | .catch(error => { 77 | console.warn(error); 78 | }) 79 | .then(() => { 80 | clearTimeout(timer); 81 | resolve(); 82 | }); 83 | }); 84 | }) 85 | ).then(() => { 86 | console.log('\nDone fetching'); 87 | process.exit(); 88 | }); 89 | -------------------------------------------------------------------------------- /src/fetch-submissions.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios'); 2 | const fs = require('fs'); 3 | 4 | const TIMEOUT = 10000; 5 | const cookie = require('./config.json').cookie; 6 | if (!cookie) { 7 | console.error('Please provide your cookie in "config.json"!'); 8 | process.exit(); 9 | } 10 | const requestParams = { 11 | method: 'get', 12 | headers: { 13 | Cookie: cookie, 14 | }, 15 | }; 16 | 17 | const zeroSubmissionsAccepted = []; 18 | const submissions = []; 19 | 20 | axios 21 | .request( 22 | Object.assign(requestParams, { 23 | url: 'https://leetcode.com/api/problems/all/', 24 | }) 25 | ) 26 | .then(response => { 27 | const questions = response.data.stat_status_pairs; 28 | const promises = []; 29 | questions 30 | .forEach(question => { 31 | console.log(question); 32 | const id = question.stat.question_id; 33 | const slug = question.stat.question__title_slug; 34 | const promise = new Promise(resolve => { 35 | const timer = setTimeout(() => { 36 | console.log(`Timeout for ${id}-${slug}`); 37 | resolve(); 38 | }, TIMEOUT); 39 | axios 40 | .request( 41 | Object.assign(requestParams, { 42 | url: `https://leetcode.com/api/submissions/${slug}`, 43 | }) 44 | ) 45 | .then(response => { 46 | const numberOfSubmissions = 47 | response.data.submissions_dump.length; 48 | if (numberOfSubmissions === 0) { 49 | zeroSubmissionsAccepted.push({ id, slug }); 50 | return; 51 | } 52 | console.log( 53 | `${id}-${slug}: ${ 54 | response.data.submissions_dump.length 55 | } submissions` 56 | ); 57 | for (let i = 0; i < numberOfSubmissions; i++) { 58 | const submission = 59 | response.data.submissions_dump[i]; 60 | if (submission.status_display === 'Accepted') { 61 | submissions.push({ 62 | id, 63 | slug, 64 | url: submission.url, 65 | language: submission.lang, 66 | }); 67 | break; 68 | } 69 | } 70 | }) 71 | .catch(error => { 72 | console.warn(error); 73 | }) 74 | .then(() => { 75 | clearTimeout(timer); 76 | resolve(); 77 | }); 78 | }); 79 | promises.push(promise); 80 | }); 81 | return Promise.all(promises); 82 | }) 83 | .then(() => { 84 | console.log('\nDone fetching'); 85 | zeroSubmissionsAccepted.sort((a, b) => a.id - b.id); 86 | submissions.sort((a, b) => a.id - b.id); 87 | fs.writeFileSync( 88 | 'zero-submissions.json', 89 | JSON.stringify(zeroSubmissionsAccepted, null, 2) 90 | ); 91 | fs.writeFileSync( 92 | 'submissions.json', 93 | JSON.stringify(submissions, null, 2) 94 | ); 95 | process.exit(); 96 | }); 97 | --------------------------------------------------------------------------------