├── track.conf ├── .gitattributes ├── chapters.pickle ├── requirements.txt ├── Leetcode Questions.epub ├── .github └── workflows │ ├── mirror-to-gitlab.yml │ ├── cron-job.yml │ └── manual-job.yml ├── README.md ├── utils.py ├── LICENSE ├── epub_writer.py ├── .gitignore └── main.py /track.conf: -------------------------------------------------------------------------------- 1 | 2130 -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.* linguist-language=Python 2 | -------------------------------------------------------------------------------- /chapters.pickle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Bishalsarang/Leetcode-Questions/HEAD/chapters.pickle -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Bishalsarang/Leetcode-Questions/HEAD/requirements.txt -------------------------------------------------------------------------------- /Leetcode Questions.epub: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Bishalsarang/Leetcode-Questions/HEAD/Leetcode Questions.epub -------------------------------------------------------------------------------- /.github/workflows/mirror-to-gitlab.yml: -------------------------------------------------------------------------------- 1 | name: Mirror and run GitLab CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v1 10 | - name: Mirror + trigger CI 11 | uses: SvanBoxel/gitlab-mirror-and-ci-action@master 12 | with: 13 | args: "https://gitlab.com/sarangbishal/Leetcode-Questions" 14 | env: 15 | FORCE_PUSH: "false" 16 | GITLAB_HOSTNAME: "gitlab.com" 17 | GITLAB_USERNAME: "sarangbishal" 18 | GITLAB_PASSWORD: ${{ secrets.GITLAB_PASSWORD }} 19 | GITLAB_PROJECT_ID: "38192142" 20 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Leetcode-Questions 2 | Cronjob and Manual Workflow to download leetcode problems 3 | 4 | This project uses [Leetcode Questions Scraper](https://github.com/Bishalsarang/Leetcode-Questions-Scraper) and Github Actions cronjob to download free leetcode problems. 5 | 6 | You can download [out.html](https://github.com/Bishalsarang/Leetcode-Questions/blob/main/out.html) , [Leetcode Questions.epub](https://github.com/Bishalsarang/Leetcode-Questions/blob/main/Leetcode%20Questions.epub). 7 | 8 | You won't necessarily see the all the problems here. If you want to download all the problems use [Leetcode Questions Scrapper](https://github.com/Bishalsarang/Leetcode-Questions-Scraper) in your system. 9 | You can view html preview [here](https://bishalsarang.github.io/Leetcode-Questions/out.html) -------------------------------------------------------------------------------- /.github/workflows/cron-job.yml: -------------------------------------------------------------------------------- 1 | name: download_letcode_cron 2 | 3 | on: 4 | schedule: 5 | - cron: '0 0 * * *' 6 | 7 | jobs: 8 | build: 9 | 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Set up Python 3.7 15 | uses: actions/setup-python@v2 16 | with: 17 | python-version: 3.7 18 | - name: Install dependencies 19 | run: | 20 | python -m pip install --upgrade pip 21 | pip install -r requirements.txt 22 | - name: Generate epub and html 23 | run: | 24 | python main.py 25 | - name: Commit and push 26 | run: |- 27 | git config --global user.email "githubaction@example.com" 28 | git config --global user.name "github-action" 29 | git diff --quiet || (git add chapters.pickle out.html track.conf "Leetcode Questions.epub" && git commit -m "Update with new problems") 30 | git push origin HEAD:main -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | """ 2 | Contains utility function to update upto which the problems has been downloaded, writing chapter info to a file, resetting configuration, 3 | reading upto which the problems has been downloaded 4 | """ 5 | import pickle 6 | 7 | def update_tracker(file_name, problem_num): 8 | """ 9 | 10 | """ 11 | with open(file_name, "w") as f: 12 | f.write(str(problem_num)) 13 | 14 | def dump_chapters_to_file(chapters): 15 | """ 16 | 17 | """ 18 | with open('chapters.pickle', 'wb') as f: 19 | pickle.dump(chapters, f) 20 | 21 | def reset_configuration(): 22 | """ 23 | Resets problem num downloaded upto to -1 24 | Resets all the chapters 25 | Resets html file 26 | """ 27 | update_tracker("track.conf", -1) 28 | dump_chapters_to_file([]) 29 | 30 | with open("out.html", "wb") as f: 31 | f.write(b" ") 32 | 33 | 34 | def read_tracker(file_name): 35 | """ 36 | 37 | """ 38 | with open(file_name, "r") as f: 39 | return int(f.readline()) 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Bishal Sarangkoti 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 | -------------------------------------------------------------------------------- /.github/workflows/manual-job.yml: -------------------------------------------------------------------------------- 1 | name: download_letcode_manual 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | MAXIMUM_NUMBER_OF_PROBLEMS: 7 | description: 'Number of problems to download' 8 | default: '30' 9 | SLEEP_TIME_PER_PROBLEM_IN_SECOND: 10 | description: 'Number of seconds to wait after downloading each problem' 11 | default: '8' 12 | 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v2 18 | - name: Set up Python 3.7 19 | uses: actions/setup-python@v2 20 | with: 21 | python-version: 3.7 22 | - name: Install dependencies 23 | run: | 24 | python -m pip install --upgrade pip 25 | pip install -r requirements.txt 26 | - name: Generate epub and html 27 | run: | 28 | python main.py 29 | env: 30 | MAXIMUM_NUMBER_OF_PROBLEMS: ${{ github.event.inputs.MAXIMUM_NUMBER_OF_PROBLEMS }} 31 | SLEEP_TIME_PER_PROBLEM_IN_SECOND: ${{ github.event.inputs.SLEEP_TIME_PER_PROBLEM_IN_SECOND }} 32 | - name: Commit and push 33 | run: |- 34 | git config --global user.email "githubaction@example.com" 35 | git config --global user.name "github-action" 36 | git diff --quiet || (git add chapters.pickle out.html track.conf "Leetcode Questions.epub" && git commit -m "Update with new problems") 37 | git push --force-with-lease -------------------------------------------------------------------------------- /epub_writer.py: -------------------------------------------------------------------------------- 1 | from ebooklib import epub 2 | import colorama 3 | from colorama import Back, Fore 4 | import pickle 5 | 6 | colorama.init() 7 | 8 | def write(file_name, title, author, chapters): 9 | # Ebook 10 | book = epub.EpubBook() 11 | 12 | # set metadata 13 | book.set_identifier('id123456') 14 | book.set_title(title) 15 | book.set_language('en') 16 | book.add_author(author) 17 | book.add_author('Anonymous', file_as='Anonymous', role='ill', uid='coauthor') 18 | 19 | toc = [] 20 | spine = ['nav'] 21 | # For each chapter add chapter to the book, TOC and spine 22 | for chapter in chapters: 23 | book.add_item(chapter) 24 | toc.append(epub.Link(chapter.file_name, chapter.title, chapter.title)) 25 | spine.append(chapter) 26 | 27 | # define Table Of Contents 28 | book.toc = tuple(toc) 29 | 30 | # add default NCX and Nav file 31 | book.add_item(epub.EpubNcx()) 32 | book.add_item(epub.EpubNav()) 33 | 34 | # define CSS style 35 | style = 'pre{white-space:pre-wrap;background:#f7f9fa;padding:10px 15px;color:#263238;line-height:1.6;font-size:13px;border-radius:3px margin-top: 0;margin-bottom:1em;overflow:auto}b,strong{font-weight:bolder}#title{font-size:16px;color:#212121;font-weight:600;margin-bottom:10px}hr{height:10px;border:0;box-shadow:0 10px 10px -10px #8c8b8b inset}' 36 | nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style) 37 | 38 | # add CSS file 39 | book.add_item(nav_css) 40 | 41 | # basic spine 42 | book.spine = spine 43 | 44 | # write to the file 45 | epub.write_epub(file_name, book, {}) 46 | 47 | print(Back.GREEN + Fore.BLACK + " File " + Back.YELLOW + f" {file_name} " + Back.GREEN + " Successfully Written ") 48 | def main(): 49 | # Load chapters list that stores chapter info 50 | # Store chapter info 51 | with open('chapters.pickle', 'rb') as f: 52 | chapters = pickle.load(f) 53 | 54 | 55 | write("Leetcode Questions.epub", "Leetcode Questions", "Anonymous", chapters) 56 | 57 | 58 | if __name__ == "__main__": 59 | main() -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/python,intellij 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=python,intellij 4 | 5 | ### Intellij ### 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 8 | .idea 9 | # User-specific stuff 10 | .idea/**/workspace.xml 11 | .idea/**/tasks.xml 12 | .idea/**/usage.statistics.xml 13 | .idea/**/dictionaries 14 | .idea/**/shelf 15 | 16 | # AWS User-specific 17 | .idea/**/aws.xml 18 | 19 | # Generated files 20 | .idea/**/contentModel.xml 21 | 22 | # Sensitive or high-churn files 23 | .idea/**/dataSources/ 24 | .idea/**/dataSources.ids 25 | .idea/**/dataSources.local.xml 26 | .idea/**/sqlDataSources.xml 27 | .idea/**/dynamic.xml 28 | .idea/**/uiDesigner.xml 29 | .idea/**/dbnavigator.xml 30 | 31 | # Gradle 32 | .idea/**/gradle.xml 33 | .idea/**/libraries 34 | 35 | # Gradle and Maven with auto-import 36 | # When using Gradle or Maven with auto-import, you should exclude module files, 37 | # since they will be recreated, and may cause churn. Uncomment if using 38 | # auto-import. 39 | # .idea/artifacts 40 | # .idea/compiler.xml 41 | # .idea/jarRepositories.xml 42 | # .idea/modules.xml 43 | # .idea/*.iml 44 | # .idea/modules 45 | # *.iml 46 | # *.ipr 47 | 48 | # CMake 49 | cmake-build-*/ 50 | 51 | # Mongo Explorer plugin 52 | .idea/**/mongoSettings.xml 53 | 54 | # File-based project format 55 | *.iws 56 | 57 | # IntelliJ 58 | out/ 59 | 60 | # mpeltonen/sbt-idea plugin 61 | .idea_modules/ 62 | 63 | # JIRA plugin 64 | atlassian-ide-plugin.xml 65 | 66 | # Cursive Clojure plugin 67 | .idea/replstate.xml 68 | 69 | # Crashlytics plugin (for Android Studio and IntelliJ) 70 | com_crashlytics_export_strings.xml 71 | crashlytics.properties 72 | crashlytics-build.properties 73 | fabric.properties 74 | 75 | # Editor-based Rest Client 76 | .idea/httpRequests 77 | 78 | # Android studio 3.1+ serialized cache file 79 | .idea/caches/build_file_checksums.ser 80 | 81 | ### Intellij Patch ### 82 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 83 | 84 | # *.iml 85 | # modules.xml 86 | # .idea/misc.xml 87 | # *.ipr 88 | 89 | # Sonarlint plugin 90 | # https://plugins.jetbrains.com/plugin/7973-sonarlint 91 | .idea/**/sonarlint/ 92 | 93 | # SonarQube Plugin 94 | # https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin 95 | .idea/**/sonarIssues.xml 96 | 97 | # Markdown Navigator plugin 98 | # https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced 99 | .idea/**/markdown-navigator.xml 100 | .idea/**/markdown-navigator-enh.xml 101 | .idea/**/markdown-navigator/ 102 | 103 | # Cache file creation bug 104 | # See https://youtrack.jetbrains.com/issue/JBR-2257 105 | .idea/$CACHE_FILE$ 106 | 107 | # CodeStream plugin 108 | # https://plugins.jetbrains.com/plugin/12206-codestream 109 | .idea/codestream.xml 110 | 111 | ### Python ### 112 | # Byte-compiled / optimized / DLL files 113 | __pycache__/ 114 | *.py[cod] 115 | *$py.class 116 | 117 | # C extensions 118 | *.so 119 | 120 | # Distribution / packaging 121 | .Python 122 | build/ 123 | develop-eggs/ 124 | dist/ 125 | downloads/ 126 | eggs/ 127 | .eggs/ 128 | lib/ 129 | lib64/ 130 | parts/ 131 | sdist/ 132 | var/ 133 | wheels/ 134 | share/python-wheels/ 135 | *.egg-info/ 136 | .installed.cfg 137 | *.egg 138 | MANIFEST 139 | 140 | # PyInstaller 141 | # Usually these files are written by a python script from a template 142 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 143 | *.manifest 144 | *.spec 145 | 146 | # Installer logs 147 | pip-log.txt 148 | pip-delete-this-directory.txt 149 | 150 | # Unit test / coverage reports 151 | htmlcov/ 152 | .tox/ 153 | .nox/ 154 | .coverage 155 | .coverage.* 156 | .cache 157 | nosetests.xml 158 | coverage.xml 159 | *.cover 160 | *.py,cover 161 | .hypothesis/ 162 | .pytest_cache/ 163 | cover/ 164 | 165 | # Translations 166 | *.mo 167 | *.pot 168 | 169 | # Django stuff: 170 | *.log 171 | local_settings.py 172 | db.sqlite3 173 | db.sqlite3-journal 174 | 175 | # Flask stuff: 176 | instance/ 177 | .webassets-cache 178 | 179 | # Scrapy stuff: 180 | .scrapy 181 | 182 | # Sphinx documentation 183 | docs/_build/ 184 | 185 | # PyBuilder 186 | .pybuilder/ 187 | target/ 188 | 189 | # Jupyter Notebook 190 | .ipynb_checkpoints 191 | 192 | # IPython 193 | profile_default/ 194 | ipython_config.py 195 | 196 | # pyenv 197 | # For a library or package, you might want to ignore these files since the code is 198 | # intended to run in multiple environments; otherwise, check them in: 199 | # .python-version 200 | 201 | # pipenv 202 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 203 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 204 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 205 | # install all needed dependencies. 206 | #Pipfile.lock 207 | 208 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 209 | __pypackages__/ 210 | 211 | # Celery stuff 212 | celerybeat-schedule 213 | celerybeat.pid 214 | 215 | # SageMath parsed files 216 | *.sage.py 217 | 218 | # Environments 219 | .env 220 | .venv 221 | env/ 222 | venv/ 223 | ENV/ 224 | env.bak/ 225 | venv.bak/ 226 | 227 | # Spyder project settings 228 | .spyderproject 229 | .spyproject 230 | 231 | # Rope project settings 232 | .ropeproject 233 | 234 | # mkdocs documentation 235 | /site 236 | 237 | # mypy 238 | .mypy_cache/ 239 | .dmypy.json 240 | dmypy.json 241 | 242 | # Pyre type checker 243 | .pyre/ 244 | 245 | # pytype static type analyzer 246 | .pytype/ 247 | 248 | # Cython debug symbols 249 | cython_debug/ 250 | 251 | # End of https://www.toptal.com/developers/gitignore/api/python,intellij -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # Author: Bishal Sarang 2 | import json 3 | import os 4 | import pickle 5 | import time 6 | 7 | import bs4 8 | import colorama 9 | import requests 10 | from colorama import Back, Fore 11 | from ebooklib import epub 12 | from selenium import webdriver 13 | from selenium.webdriver.chrome.options import Options 14 | from selenium.webdriver.common.by import By 15 | from selenium.webdriver.support import expected_conditions as EC 16 | from selenium.webdriver.support.ui import WebDriverWait 17 | from utils import * 18 | import epub_writer 19 | 20 | # Initialize Colorama 21 | colorama.init(autoreset=True) 22 | 23 | options = Options() 24 | options.headless = True 25 | # Disable Warning, Error and Info logs 26 | # Show only fatal errors 27 | options.add_argument("--log-level=3") 28 | driver = webdriver.Chrome(options=options) 29 | 30 | # Get upto which problem it is already scraped from track.conf file 31 | completed_upto = read_tracker("track.conf") 32 | 33 | # Load chapters list that stores chapter info 34 | # Store chapter info 35 | with open('chapters.pickle', 'rb') as f: 36 | chapters = pickle.load(f) 37 | 38 | 39 | def download(problem_num, url, title, solution_slug): 40 | print( 41 | Fore.BLACK + Back.CYAN + f"Fetching problem num " + Back.YELLOW + f" {problem_num} " + Back.CYAN + " with url " + Back.YELLOW + f" {url} ") 42 | n = len(title) 43 | 44 | try: 45 | 46 | driver.get(url) 47 | # Wait 20 secs or until div with id initial-loading disappears 48 | element = WebDriverWait(driver, 30).until( 49 | EC.visibility_of_element_located((By.CLASS_NAME, "_1l1MA"))) 50 | # Get current tab page source 51 | html = driver.page_source 52 | soup = bs4.BeautifulSoup(html, "html.parser") 53 | 54 | # Construct HTML 55 | title_decorator = '*' * n 56 | problem_title_html = title_decorator + f'