From 0f894fae9ef75e5c1129c3d08a98f6ff26994157 Mon Sep 17 00:00:00 2001 From: GoingMyWay Date: Wed, 8 Feb 2023 18:16:54 +0800 Subject: [PATCH 1/6] add --no-problem-statement-files and --only-latest-submission --- leetcode_export/__main__.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/leetcode_export/__main__.py b/leetcode_export/__main__.py index 5f366bb..0649546 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-files", + dest="no_problem_statement_files", + action="store_true", + help="do not save problem statement files", + ) 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-latest-submission", + dest="only_latest_submission", + action="store_true", + help="only save the latest submission of the lang", + ) parser.add_argument( "--language", dest="language_unprocessed", @@ -145,6 +157,7 @@ def main(): os.chdir(args.folder) title_slug_to_problem_folder_name: dict[str, str] = dict() + title_slug_to_extension: dict[str, set] = dict() for submission in leetcode.get_submissions(): if args.only_accepted and submission.status_display != "Accepted": @@ -153,6 +166,16 @@ def main(): if args.language and submission.lang not in args.language: continue + if submission.title_slug in title_slug_to_extension and \ + submission.extension in title_slug_to_extension[submission.title_slug] and \ + args.only_latest_submission: + logging.info(f"Saving only the latest submission of the question {submission.title_slug} with " + f"{submission.extension} extension, skipping it") + continue + else: + title_slug_to_extension[submission.title_slug] = set() + title_slug_to_extension[submission.title_slug].add(submission.extension) + 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 +191,7 @@ def main(): problem_statement_filename = problem_statement_filename_template.substitute( **problem_statement.__dict__ ) - if not os.path.exists(problem_statement_filename): + if not os.path.exists(problem_statement_filename) and not args.no_problem_statement_files: with open(problem_statement_filename, "w+") as problem_statement_file: problem_statement_file.write( problem_statement_template.substitute( From 0e9d7a692f84d307dcfdaf4823dbcf8746225404 Mon Sep 17 00:00:00 2001 From: GoingMyWay Date: Wed, 8 Feb 2023 18:23:57 +0800 Subject: [PATCH 2/6] Update README.md for --no-problem-statement-files and --only-latest-submission --- README.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bf88744..203bc36 100644 --- a/README.md +++ b/README.md @@ -108,20 +108,23 @@ The script accepts the following arguments: ``` usage: leetcode_export [-h] [--cookies COOKIES] [--folder FOLDER] [--problem-folder-name PROBLEM_FOLDER_NAME] + [--no-problem-statement-files] [--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-latest-submission] + [--language LANGUAGE_UNPROCESSED] [-v] [-vv] [-V] Export LeetCode submissions -options: +optional arguments: -h, --help show this help message and exit --cookies COOKIES set LeetCode cookies --folder FOLDER set output folder --problem-folder-name PROBLEM_FOLDER_NAME problem folder name format + --no-problem-statement-files + do not save problem statement files --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-latest-submission + only save the latest submission of the lang --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) From 829b31fbcc84c48423203ce26d9c6d0fae40f604 Mon Sep 17 00:00:00 2001 From: GoingMyWay Date: Fri, 10 Feb 2023 14:14:07 +0800 Subject: [PATCH 3/6] add no-problem-statement-files 2. add only-latest-submission --- leetcode_export/__main__.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/leetcode_export/__main__.py b/leetcode_export/__main__.py index 0649546..06f28c4 100644 --- a/leetcode_export/__main__.py +++ b/leetcode_export/__main__.py @@ -31,10 +31,10 @@ def parse_args(): help="problem folder name format", ) parser.add_argument( - "--no-problem-statement-files", - dest="no_problem_statement_files", + "--no-problem-statement", + dest="no_problem_statement", action="store_true", - help="do not save problem statement files", + help="do not save problem statement", ) parser.add_argument( "--problem-statement-filename", @@ -159,6 +159,7 @@ def main(): title_slug_to_problem_folder_name: dict[str, str] = dict() title_slug_to_extension: dict[str, set] = dict() + submissions = [] for submission in leetcode.get_submissions(): if args.only_accepted and submission.status_display != "Accepted": continue @@ -166,14 +167,21 @@ def main(): if args.language and submission.lang not in args.language: continue - if submission.title_slug in title_slug_to_extension and \ - submission.extension in title_slug_to_extension[submission.title_slug] and \ - args.only_latest_submission: - logging.info(f"Saving only the latest submission of the question {submission.title_slug} with " - f"{submission.extension} extension, skipping it") - continue - else: + submissions.append(submission) + + for submission in sorted(submissions, key=lambda s: s.date_formatted, reverse=True): + if submission.title_slug not in title_slug_to_extension: title_slug_to_extension[submission.title_slug] = set() + + if ( + submission.extension in title_slug_to_extension[submission.title_slug] + and args.only_latest_submission + ): + logging.info( + f"Saving only the latest submission of the question {submission.title_slug} with " + f"{submission.extension} extension, skipping it" + ) + continue title_slug_to_extension[submission.title_slug].add(submission.extension) if submission.title_slug not in title_slug_to_problem_folder_name: @@ -191,7 +199,10 @@ def main(): problem_statement_filename = problem_statement_filename_template.substitute( **problem_statement.__dict__ ) - if not os.path.exists(problem_statement_filename) and not args.no_problem_statement_files: + if ( + not os.path.exists(problem_statement_filename) + and not args.no_problem_statement + ): with open(problem_statement_filename, "w+") as problem_statement_file: problem_statement_file.write( problem_statement_template.substitute( From 0117d641cc9628bc275d4ac5e1d97974de8f7f25 Mon Sep 17 00:00:00 2001 From: GoingMyWay Date: Fri, 10 Feb 2023 14:14:29 +0800 Subject: [PATCH 4/6] update the README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 203bc36..e34fb05 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ The script accepts the following arguments: ``` usage: leetcode_export [-h] [--cookies COOKIES] [--folder FOLDER] [--problem-folder-name PROBLEM_FOLDER_NAME] - [--no-problem-statement-files] + [--no-problem-statement] [--problem-statement-filename PROBLEM_STATEMENT_FILENAME] [--problem-statement-content PROBLEM_STATEMENT_CONTENT] [--submission-filename SUBMISSION_FILENAME] @@ -123,7 +123,7 @@ optional arguments: --folder FOLDER set output folder --problem-folder-name PROBLEM_FOLDER_NAME problem folder name format - --no-problem-statement-files + --no-problem-statement do not save problem statement files --problem-statement-filename PROBLEM_STATEMENT_FILENAME problem statement filename format From e86df26b989a3e94df725caf0062978f642310b0 Mon Sep 17 00:00:00 2001 From: Davide Cazzin <28535750+NeverMendel@users.noreply.github.com> Date: Mon, 13 Feb 2023 23:05:54 +0100 Subject: [PATCH 5/6] Change flag to `--only-last-submission` --- leetcode_export/__main__.py | 43 +++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/leetcode_export/__main__.py b/leetcode_export/__main__.py index 06f28c4..0a92276 100644 --- a/leetcode_export/__main__.py +++ b/leetcode_export/__main__.py @@ -62,10 +62,10 @@ def parse_args(): help="save accepted submissions only", ) parser.add_argument( - "--only-latest-submission", - dest="only_latest_submission", + "--only-last-submission", + dest="only_last_submission", action="store_true", - help="only save the latest submission of the lang", + help="only save the last submission for each programming language", ) parser.add_argument( "--language", @@ -157,32 +157,40 @@ def main(): os.chdir(args.folder) title_slug_to_problem_folder_name: dict[str, str] = dict() - title_slug_to_extension: dict[str, set] = dict() + title_slug_to_exported_languages: dict[str, set[str]] = dict() + + last_submission_timestamp: Optional[int] = None - submissions = [] 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 - submissions.append(submission) - - for submission in sorted(submissions, key=lambda s: s.date_formatted, reverse=True): - if submission.title_slug not in title_slug_to_extension: - title_slug_to_extension[submission.title_slug] = set() + if submission.title_slug not in title_slug_to_exported_languages: + title_slug_to_exported_languages[submission.title_slug] = set() if ( - submission.extension in title_slug_to_extension[submission.title_slug] - and args.only_latest_submission + 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"Saving only the latest submission of the question {submission.title_slug} with " - f"{submission.extension} extension, skipping it" + f"The latest submission for {submission.title_slug} in {submission.lang} has already been exported, skipping this submission" ) continue - title_slug_to_extension[submission.title_slug].add(submission.extension) + 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) @@ -199,9 +207,8 @@ def main(): problem_statement_filename = problem_statement_filename_template.substitute( **problem_statement.__dict__ ) - if ( - not os.path.exists(problem_statement_filename) - and not args.no_problem_statement + 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( From 3d543aaf951ded151ecc62b16cce1998379b7141 Mon Sep 17 00:00:00 2001 From: Davide Cazzin <28535750+NeverMendel@users.noreply.github.com> Date: Mon, 13 Feb 2023 23:06:12 +0100 Subject: [PATCH 6/6] Update script help message in README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e34fb05..8648f9e 100644 --- a/README.md +++ b/README.md @@ -112,19 +112,19 @@ usage: leetcode_export [-h] [--cookies COOKIES] [--folder FOLDER] [--problem-statement-filename PROBLEM_STATEMENT_FILENAME] [--problem-statement-content PROBLEM_STATEMENT_CONTENT] [--submission-filename SUBMISSION_FILENAME] - [--only-accepted] [--only-latest-submission] + [--only-accepted] [--only-last-submission] [--language LANGUAGE_UNPROCESSED] [-v] [-vv] [-V] Export LeetCode submissions -optional arguments: +options: -h, --help show this help message and exit --cookies COOKIES set LeetCode cookies --folder FOLDER set output folder --problem-folder-name PROBLEM_FOLDER_NAME problem folder name format --no-problem-statement - do not save problem statement files + do not save problem statement --problem-statement-filename PROBLEM_STATEMENT_FILENAME problem statement filename format --problem-statement-content PROBLEM_STATEMENT_CONTENT @@ -132,8 +132,8 @@ optional arguments: --submission-filename SUBMISSION_FILENAME submission filename format --only-accepted save accepted submissions only - --only-latest-submission - only save the latest submission of the lang + --only-last-submission + only save the last submission for each programming language --language LANGUAGE_UNPROCESSED save submissions for specified programming languages. syntax: --language=,,...