8000 add solutions folder · liusoon/leetcode@abd2257 · GitHub
[go: up one dir, main page]

Skip to content

Commit abd2257

Browse files
committed
add solutions folder
1 parent 7ca9bc3 commit abd2257

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

leetcode_generate.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
# Usage: Leetcode solution downloader and auto generate readme
77
#
88
import requests
9-
import configparser
109
import os
10+
import configparser
1111
import json
1212
import time
1313
import datetime
1414
import re
1515
import sys
1616
import html
1717

18+
from pathlib import Path
1819
from selenium import webdriver
1920
from collections import namedtuple, OrderedDict
2021

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')
2325
COOKIE_PATH = 'cookies.json'
2426
BASE_URL = 'https://leetcode.com'
2527
# If you have proxy, change PROXIES below
@@ -85,8 +87,8 @@ def rep_unicode_in_code(code):
8587

8688

8789
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)
9092

9193

9294
ProgLang = namedtuple('ProgLang', ['language', 'ext', 'annotation'])
@@ -292,7 +294,7 @@ def _generate_items_from_api(self, json_data):
292294
def is_login(self):
293295
""" validate if the cookie exists and not overtime """
294296
api_url = self.base_url + '/api/problems/algorithms/' # NOQA
295-
if not os.path.exists(COOKIE_PATH):
297+
if not Path.exists(COOKIE_PATH):
296298
return False
297299

298300
with open(COOKIE_PATH, 'r') as f:
@@ -462,15 +464,15 @@ def _download_code_by_quiz(self, quiz):
462464
)
463465
return
464466

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)
469471
for slt in slts:
470472
fname = '{title}.{ext}'.format(
471473
title=qtitle, ext=self.prolangdict[slt['lang']].ext
472474
)
473-
filename = os.path.join(path, fname)
475+
filename = Path.joinpath(path, fname)
474476
content = self._get_code_with_anno(slt)
475477
import codecs
476478

@@ -549,7 +551,7 @@ def write_readme(self):
549551
language = ':lock:'
550552
else:
551553
if item.solutions:
552-
dirname = '{id}-{title}'.format(
554+
dirname = 'solutions/{id}-{title}'.format(
553555
id=str(item.question_id).zfill(3),
554556
title=item.question__title_slug,
555557
)

0 commit comments

Comments
 (0)
0