diff --git a/README.md b/README.md index bf88744..8648f9e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -122,6 +123,8 @@ 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 @@ -129,6 +132,8 @@ options: --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=,,... @@ -225,4 +230,4 @@ The license of leetcode-cli is available [here](https://github.com/skygragon/lee ## License -[MIT License](LICENSE) \ No newline at end of file +[MIT License](LICENSE) diff --git a/leetcode_export/__main__.py b/leetcode_export/__main__.py index 5f366bb..0a92276 100644 --- a/leetcode_export/__main__.py +++ b/leetcode_export/__main__.py @@ -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, @@ -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", @@ -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( @@ -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(