1+ '''
2+ LeetCode Export.
3+
4+ Export your LeetCode submissions and related problem statements.
5+ '''
16import argparse
27import logging
38import os
1015
1116
1217def parse_args ():
13- parser = argparse .ArgumentParser (description = 'Export LeetCode solutions ' , formatter_class = argparse .RawTextHelpFormatter )
18+ parser = argparse .ArgumentParser (description = 'Export LeetCode submissions ' , formatter_class = argparse .RawTextHelpFormatter )
1419 parser .add_argument ('--cookies' , type = str , help = 'set LeetCode cookies' )
1520 parser .add_argument ('--folder' , type = str , default = '.' , help = 'set output folder' )
1621 parser .add_argument ('--problem-folder-name' , type = str , default = '${question_id}-${title_slug}' ,
1722 help = 'problem folder name format' )
18- parser .add_argument ('--problem-filename' , type = str , default = '${question_id}-${title_slug}.html' ,
19- help = 'problem description filename format' )
20- parser .add_argument ('--problem-content' , type = str ,
23+ parser .add_argument ('--problem-statement- filename' , type = str , default = '${question_id}-${title_slug}.html' ,
24+ help = 'problem statement filename format' )
25+ parser .add_argument ('--problem-statement- content' , type = str ,
2126 default = '<h1>${question_id} - ${title}</h1><h2>Difficulty: ${difficulty} - ' +
2227 '<a href="https://leetcode.com/problems/${title_slug}/">${title_slug}</a></h2>${content}' ,
23- help = 'problem description content format' )
28+ help = 'problem statement content format' )
2429 parser .add_argument ('--submission-filename' , type = str ,
2530 default = '${date_formatted} - ${status_display} - runtime ${runtime} - memory ${memory}.${extension}' ,
2631 help = 'submission filename format' )
@@ -73,9 +78,9 @@ def main():
7378 logging .info (args )
7479
7580 problem_folder_name_template = Template (args .problem_folder_name )
76- problem_statement_template = Template (args .problem_filename )
77- problem_content_template = Template (args .problem_content )
78- submission_template = Template (args .submission_filename )
81+ problem_statement_filename_template = Template (args .problem_statement_filename )
82+ problem_statement_template = Template (args .problem_statement_content )
83+ submission_filename_template = Template (args .submission_filename )
7984
8085 leetcode = LeetCode ()
8186 cookies = args .cookies
@@ -84,8 +89,8 @@ def main():
8489 cookies = input ("Insert LeetCode cookies: " )
8590
8691 if not leetcode .set_cookies (cookies ):
87- print (
88- "Cookies not valid. Copy them from the Network tab of your browser by clicking on any leetcode.com request and going in Request Headers > cookie." )
92+ logging . error (
93+ "Cookies not valid. Copy them from the Network tab of your browser by clicking on any leetcode.com request and going in Request Headers > cookie. Check README.md file for further information " )
8994 exit (1 )
9095
9196 # Create output folder if it doesn't already exist
@@ -103,28 +108,28 @@ def main():
103108 continue
104109
105110 if submission .title_slug not in title_slug_to_problem_folder_name :
106- problem_statement = leetcode .get_problem (submission .title_slug )
111+ problem_statement = leetcode .get_problem_statement (submission .title_slug )
107112 problem_folder_name = problem_folder_name_template .substitute (** problem_statement .__dict__ )
108113 title_slug_to_problem_folder_name [submission .title_slug ] = problem_folder_name
109114 if not os .path .exists (problem_folder_name ):
110115 os .mkdir (problem_folder_name )
111116 os .chdir (problem_folder_name )
112117
113- problem_statement_filename = problem_statement_template .substitute (** problem_statement .__dict__ )
118+ problem_statement_filename = problem_statement_filename_template .substitute (** problem_statement .__dict__ )
114119 if not os .path .exists (problem_statement_filename ):
115120 with open (problem_statement_filename , 'w+' ) as problem_statement_file :
116- problem_statement_file .write (problem_content_template .substitute (** problem_statement .__dict__ ))
121+ problem_statement_file .write (problem_statement_template .substitute (** problem_statement .__dict__ ))
117122 else :
118123 os .chdir (title_slug_to_problem_folder_name [submission .title_slug ])
119124
120- sub_filename = submission_template .substitute (** submission .__dict__ )
121- if not os .path .exists (sub_filename ):
122- logging .info (f"Writing { submission .title_slug } /{ sub_filename } " )
123- sub_file = open (sub_filename , 'w+' )
125+ submission_filename = submission_filename_template .substitute (** submission .__dict__ )
126+ if not os .path .exists (submission_filename ):
127+ logging .info (f"Writing { submission .title_slug } /{ submission_filename } " )
128+ sub_file = open (submission_filename , 'w+' )
124129 sub_file .write (submission .code )
125130 sub_file .close ()
126131 else :
127- logging .info (f"{ submission .title_slug } /{ sub_filename } already exists, skipping it" )
132+ logging .info (f"{ submission .title_slug } /{ submission_filename } already exists, skipping it" )
128133
129134 os .chdir (".." )
130135
0 commit comments