Skip to content

Commit 678c78f

Browse files
Add --no-problem-statement and --only-last-submission flags (#2)
* Add --no-problem-statement and --only-last-submission flags * Update script help message in README.md --------- Co-authored-by: Davide Cazzin <28535750+NeverMendel@users.noreply.github.com>
1 parent 95db0dd commit 678c78f

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ The script accepts the following arguments:
108108
```
109109
usage: leetcode-export [-h] [--cookies COOKIES] [--folder FOLDER]
110110
[--problem-folder-name PROBLEM_FOLDER_NAME]
111+
[--no-problem-statement]
111112
[--problem-statement-filename PROBLEM_STATEMENT_FILENAME]
112113
[--problem-statement-content PROBLEM_STATEMENT_CONTENT]
113114
[--submission-filename SUBMISSION_FILENAME]
114-
[--only-accepted] [--language LANGUAGE_UNPROCESSED] [-v]
115-
[-vv] [-V]
115+
[--only-accepted] [--only-last-submission]
116+
[--language LANGUAGE_UNPROCESSED] [-v] [-vv] [-V]
116117
117118
Export LeetCode submissions
118119
@@ -122,13 +123,17 @@ options:
122123
--folder FOLDER set output folder
123124
--problem-folder-name PROBLEM_FOLDER_NAME
124125
problem folder name format
126+
--no-problem-statement
127+
do not save problem statement
125128
--problem-statement-filename PROBLEM_STATEMENT_FILENAME
126129
problem statement filename format
127130
--problem-statement-content PROBLEM_STATEMENT_CONTENT
128131
problem statement content format
129132
--submission-filename SUBMISSION_FILENAME
130133
submission filename format
131134
--only-accepted save accepted submissions only
135+
--only-last-submission
136+
only save the last submission for each programming language
132137
--language LANGUAGE_UNPROCESSED
133138
save submissions for specified programming languages.
134139
syntax: --language=<lang1>,<lang2>,...
@@ -225,4 +230,4 @@ The license of leetcode-cli is available [here](https://github.com/skygragon/lee
225230

226231
## License
227232

228-
[MIT License](LICENSE)
233+
[MIT License](LICENSE)

leetcode_export/__main__.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ def parse_args():
3030
default="${question_id}-${title_slug}",
3131
help="problem folder name format",
3232
)
33+
parser.add_argument(
34+
"--no-problem-statement",
35+
dest="no_problem_statement",
36+
action="store_true",
37+
help="do not save problem statement",
38+
)
3339
parser.add_argument(
3440
"--problem-statement-filename",
3541
type=str,
@@ -55,6 +61,12 @@ def parse_args():
5561
action="store_true",
5662
help="save accepted submissions only",
5763
)
64+
parser.add_argument(
65+
"--only-last-submission",
66+
dest="only_last_submission",
67+
action="store_true",
68+
help="only save the last submission for each programming language",
69+
)
5870
parser.add_argument(
5971
"--language",
6072
dest="language_unprocessed",
@@ -145,14 +157,41 @@ def main():
145157
os.chdir(args.folder)
146158

147159
title_slug_to_problem_folder_name: dict[str, str] = dict()
160+
title_slug_to_exported_languages: dict[str, set[str]] = dict()
161+
162+
last_submission_timestamp: Optional[int] = None
148163

149164
for submission in leetcode.get_submissions():
165+
if (
166+
last_submission_timestamp is not None
167+
and submission.timestamp > last_submission_timestamp
168+
):
169+
logging.warning(
170+
"Submissions are not in reverse chronological order, --only-last-submission flag might not work as expected if used. Please report this issue on GitHub attaching the debug.log file: https://github.com/NeverMendel/leetcode-export/issues"
171+
)
172+
last_submission_timestamp = submission.timestamp
173+
150174
if args.only_accepted and submission.status_display != "Accepted":
151175
continue
152176

153177
if args.language and submission.lang not in args.language:
154178
continue
155179

180+
if submission.title_slug not in title_slug_to_exported_languages:
181+
title_slug_to_exported_languages[submission.title_slug] = set()
182+
183+
if (
184+
args.only_last_submission
185+
and submission.title_slug in title_slug_to_exported_languages
186+
and submission.lang
187+
in title_slug_to_exported_languages[submission.title_slug]
188+
):
189+
logging.info(
190+
f"The latest submission for {submission.title_slug} in {submission.lang} has already been exported, skipping this submission"
191+
)
192+
continue
193+
title_slug_to_exported_languages[submission.title_slug].add(submission.lang)
194+
156195
if submission.title_slug not in title_slug_to_problem_folder_name:
157196
problem_statement = leetcode.get_problem_statement(submission.title_slug)
158197
problem_folder_name = problem_folder_name_template.substitute(
@@ -168,7 +207,9 @@ def main():
168207
problem_statement_filename = problem_statement_filename_template.substitute(
169208
**problem_statement.__dict__
170209
)
171-
if not os.path.exists(problem_statement_filename):
210+
if not args.no_problem_statement and not os.path.exists(
211+
problem_statement_filename
212+
):
172213
with open(problem_statement_filename, "w+") as problem_statement_file:
173214
problem_statement_file.write(
174215
problem_statement_template.substitute(

0 commit comments

Comments
 (0)