|
6 | 6 | # Usage: Leetcode solution downloader and auto generate readme
|
7 | 7 | #
|
8 | 8 | import requests
|
9 |
| -import configparser |
10 | 9 | import os
|
| 10 | +import configparser |
11 | 11 | import json
|
12 | 12 | import time
|
13 | 13 | import datetime
|
14 | 14 | import re
|
15 | 15 | import sys
|
16 | 16 | import html
|
17 | 17 |
|
| 18 | +from pathlib import Path |
18 | 19 | from selenium import webdriver
|
19 | 20 | from collections import namedtuple, OrderedDict
|
20 | 21 |
|
21 |
| -HOME = os.getcwd() |
22 |
| -CONFIG_FILE = os.path.join(HOME, 'config.cfg') |
| 22 | +HOME = Path.cwd() |
| 23 | +SOLUTION_FOLDER = Path.joinpath(HOME, 'solutions') |
| 24 | +CONFIG_FILE = Path.joinpath(HOME, 'config.cfg') |
23 | 25 | COOKIE_PATH = 'cookies.json'
|
24 | 26 | BASE_URL = 'https://leetcode.com'
|
25 | 27 | # If you have proxy, change PROXIES below
|
@@ -85,8 +87,8 @@ def rep_unicode_in_code(code):
|
85 | 87 |
|
86 | 88 |
|
87 | 89 | def check_and_make_dir(dirname):
|
88 |
| - if not os.path.exists(dirname): |
89 |
| - os.mkdir(dirname) |
| 90 | + if not Path.exists(dirname): |
| 91 | + Path.mkdir(dirname) |
90 | 92 |
|
91 | 93 |
|
92 | 94 | ProgLang = namedtuple('ProgLang', ['language', 'ext', 'annotation'])
|
@@ -292,7 +294,7 @@ def _generate_items_from_api(self, json_data):
|
292 | 294 | def is_login(self):
|
293 | 295 | """ validate if the cookie exists and not overtime """
|
294 | 296 | api_url = self.base_url + '/api/problems/algorithms/' # NOQA
|
295 |
| - if not os.path.exists(COOKIE_PATH): |
| 297 | + if not Path.exists(COOKIE_PATH): |
296 | 298 | return False
|
297 | 299 |
|
298 | 300 | with open(COOKIE_PATH, 'r') as f:
|
@@ -462,15 +464,15 @@ def _download_code_by_quiz(self, quiz):
|
462 | 464 | )
|
463 | 465 | return
|
464 | 466 |
|
465 |
| - dirname = '{id}-{title}'.format(id=str(qid).zfill(3), title=qtitle) |
466 |
| - print('begin download ' + dirname) |
467 |
| - check_and_make_dir(dirname) |
468 |
| - path = os.path.join(HOME, dirname) |
| 467 | + qname = '{id}-{title}'.format(id=str(qid).zfill(3), title=qtitle) |
| 468 | + print('begin download ' + qname) |
| 469 | + path = Path.joinpath(SOLUTION_FOLDER, ,qname) |
| 470 | + check_and_make_dir(path) |
469 | 471 | for slt in slts:
|
470 | 472 | fname = '{title}.{ext}'.format(
|
471 | 473 | title=qtitle, ext=self.prolangdict[slt['lang']].ext
|
472 | 474 | )
|
473 |
| - filename = os.path.join(path, fname) |
| 475 | + filename = Path.joinpath(path, fname) |
474 | 476 | content = self._get_code_with_anno(slt)
|
475 | 477 | import codecs
|
476 | 478 |
|
@@ -549,7 +551,7 @@ def write_readme(self):
|
549 | 551 | language = ':lock:'
|
550 | 552 | else:
|
551 | 553 | if item.solutions:
|
552 |
| - dirname = '{id}-{title}'.format( |
| 554 | + dirname = 'solutions/{id}-{title}'.format( |
553 | 555 | id=str(item.question_id).zfill(3),
|
554 | 556 | title=item.question__title_slug,
|
555 | 557 | )
|
|
0 commit comments