Skip to content

Add --no-problem-statement and --only-last-submission flags #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ The script accepts the following arguments:
```
usage: leetcode_export [-h] [--cookies COOKIES] [--folder FOLDER]
[--problem-folder-name PROBLEM_FOLDER_NAME]
[--no-problem-statement]
[--problem-statement-filename PROBLEM_STATEMENT_FILENAME]
[--problem-statement-content PROBLEM_STATEMENT_CONTENT]
[--submission-filename SUBMISSION_FILENAME]
[--only-accepted] [--language LANGUAGE_UNPROCESSED] [-v]
[-vv] [-V]
[--only-accepted] [--only-last-submission]
[--language LANGUAGE_UNPROCESSED] [-v] [-vv] [-V]

Export LeetCode submissions

Expand All @@ -122,13 +123,17 @@ options:
--folder FOLDER set output folder
--problem-folder-name PROBLEM_FOLDER_NAME
problem folder name format
--no-problem-statement
do not save problem statement
--problem-statement-filename PROBLEM_STATEMENT_FILENAME
problem statement filename format
--problem-statement-content PROBLEM_STATEMENT_CONTENT
problem statement content format
--submission-filename SUBMISSION_FILENAME
submission filename format
--only-accepted save accepted submissions only
--only-last-submission
only save the last submission for each programming language
--language LANGUAGE_UNPROCESSED
save submissions for specified programming languages.
syntax: --language=<lang1>,<lang2>,...
Expand Down Expand Up @@ -225,4 +230,4 @@ The license of leetcode-cli is available [here](https://github.com/skygragon/lee

## License

[MIT License](LICENSE)
[MIT License](LICENSE)
43 changes: 42 additions & 1 deletion leetcode_export/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def parse_args():
default="${question_id}-${title_slug}",
help="problem folder name format",
)
parser.add_argument(
"--no-problem-statement",
dest="no_problem_statement",
action="store_true",
help="do not save problem statement",
)
parser.add_argument(
"--problem-statement-filename",
type=str,
Expand All @@ -55,6 +61,12 @@ def parse_args():
action="store_true",
help="save accepted submissions only",
)
parser.add_argument(
"--only-last-submission",
dest="only_last_submission",
action="store_true",
help="only save the last submission for each programming language",
)
parser.add_argument(
"--language",
dest="language_unprocessed",
Expand Down Expand Up @@ -145,14 +157,41 @@ def main():
os.chdir(args.folder)

title_slug_to_problem_folder_name: dict[str, str] = dict()
title_slug_to_exported_languages: dict[str, set[str]] = dict()

last_submission_timestamp: Optional[int] = None

for submission in leetcode.get_submissions():
if (
last_submission_timestamp is not None
and submission.timestamp > last_submission_timestamp
):
logging.warning(
"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"
)
last_submission_timestamp = submission.timestamp

if args.only_accepted and submission.status_display != "Accepted":
continue

if args.language and submission.lang not in args.language:
continue

if submission.title_slug not in title_slug_to_exported_languages:
title_slug_to_exported_languages[submission.title_slug] = set()

if (
args.only_last_submission
and submission.title_slug in title_slug_to_exported_languages
and submission.lang
in title_slug_to_exported_languages[submission.title_slug]
):
logging.info(
f"The latest submission for {submission.title_slug} in {submission.lang} has already been exported, skipping this submission"
)
continue
title_slug_to_exported_languages[submission.title_slug].add(submission.lang)

if submission.title_slug not in title_slug_to_problem_folder_name:
problem_statement = leetcode.get_problem_statement(submission.title_slug)
problem_folder_name = problem_folder_name_template.substitute(
Expand All @@ -168,7 +207,9 @@ def main():
problem_statement_filename = problem_statement_filename_template.substitute(
**problem_statement.__dict__
)
if not os.path.exists(problem_statement_filename):
if not args.no_problem_statement and not os.path.exists(
problem_statement_filename
):
with open(problem_statement_filename, "w+") as problem_statement_file:
problem_statement_file.write(
problem_statement_template.substitute(
Expand Down