1- '''
1+ """
22LeetCode Export.
33
44Export your LeetCode submissions and related problem statements.
5- '''
5+ """
66import argparse
77import logging
88import os
1515
1616
1717def parse_args ():
18- parser = argparse .ArgumentParser (description = 'Export LeetCode submissions' , formatter_class = argparse .RawTextHelpFormatter )
19- parser .add_argument ('--cookies' , type = str , help = 'set LeetCode cookies' )
20- parser .add_argument ('--folder' , type = str , default = '.' , help = 'set output folder' )
21- parser .add_argument ('--problem-folder-name' , type = str , default = '${question_id}-${title_slug}' ,
22- help = 'problem folder name format' )
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 ,
26- default = '<h1>${question_id} - ${title}</h1><h2>Difficulty: ${difficulty} - ' +
27- '<a href="https://leetcode.com/problems/${title_slug}/">${title_slug}</a></h2>${content}' ,
28- help = 'problem statement content format' )
29- parser .add_argument ('--submission-filename' , type = str ,
30- default = '${date_formatted} - ${status_display} - runtime ${runtime} - memory ${memory}.${extension}' ,
31- help = 'submission filename format' )
32- parser .add_argument ('--only-accepted' , dest = 'only_accepted' , action = 'store_true' , help = 'save accepted submissions only' )
33- parser .add_argument ('--language' , dest = 'language_unprocessed' , type = str ,
34- help = "only save submissions for the specified programming languages (eg. '--language=python,python3,cpp,java,golang')\n " +
35- "supported languages: 'python', 'python3', 'c', 'cpp', 'csharp', 'java', 'kotlin', 'mysql', 'mssql', 'oraclesql',\n " +
36- " 'javascript', 'html', 'php', 'golang', 'scala', 'pythonml', 'rust', 'ruby', 'bash', 'swift'" )
37- parser .add_argument ('-v' , '--verbose' , dest = 'verbose' , action = 'store_true' , help = 'enable verbose logging details' )
38- parser .add_argument ('-vv' , '--extra-verbose' , dest = 'extra_verbose' , action = 'store_true' ,
39- help = 'enable more verbose logging details' )
40- parser .add_argument ('-V' , '--version' , action = 'version' ,
41- version = '%(prog)s {version}' .format (version = __version__ ))
18+ parser = argparse .ArgumentParser (
19+ description = "Export LeetCode submissions" ,
20+ formatter_class = argparse .RawTextHelpFormatter ,
21+ )
22+ parser .add_argument ("--cookies" , type = str , help = "set LeetCode cookies" )
23+ parser .add_argument ("--folder" , type = str , default = "." , help = "set output folder" )
24+ parser .add_argument (
25+ "--problem-folder-name" ,
26+ type = str ,
27+ default = "${question_id}-${title_slug}" ,
28+ help = "problem folder name format" ,
29+ )
30+ parser .add_argument (
31+ "--problem-statement-filename" ,
32+ type = str ,
33+ default = "${question_id}-${title_slug}.html" ,
34+ help = "problem statement filename format" ,
35+ )
36+ parser .add_argument (
37+ "--problem-statement-content" ,
38+ type = str ,
39+ default = "<h1>${question_id} - ${title}</h1><h2>Difficulty: ${difficulty} - "
40+ + '<a href="https://leetcode.com/problems/${title_slug}/">${title_slug}</a></h2>${content}' ,
41+ help = "problem statement content format" ,
42+ )
43+ parser .add_argument (
44+ "--submission-filename" ,
45+ type = str ,
46+ default = "${date_formatted} - ${status_display} - runtime ${runtime} - memory ${memory}.${extension}" ,
47+ help = "submission filename format" ,
48+ )
49+ parser .add_argument (
50+ "--only-accepted" ,
51+ dest = "only_accepted" ,
52+ action = "store_true" ,
53+ help = "save accepted submissions only" ,
54+ )
55+ parser .add_argument (
56+ "--language" ,
57+ dest = "language_unprocessed" ,
58+ type = str ,
59+ help = "only save submissions for the specified programming languages (eg. '--language=python,python3,cpp,java,golang')\n "
60+ + "supported languages: 'python', 'python3', 'c', 'cpp', 'csharp', 'java', 'kotlin', 'mysql', 'mssql', 'oraclesql',\n "
61+ + " 'javascript', 'html', 'php', 'golang', 'scala', 'pythonml', 'rust', 'ruby', 'bash', 'swift'" ,
62+ )
63+ parser .add_argument (
64+ "-v" ,
65+ "--verbose" ,
66+ dest = "verbose" ,
67+ action = "store_true" ,
68+ help = "enable verbose logging details" ,
69+ )
70+ parser .add_argument (
71+ "-vv" ,
72+ "--extra-verbose" ,
73+ dest = "extra_verbose" ,
74+ action = "store_true" ,
75+ help = "enable more verbose logging details" ,
76+ )
77+ parser .add_argument (
78+ "-V" ,
79+ "--version" ,
80+ action = "version" ,
81+ version = "%(prog)s {version}" .format (version = __version__ ),
82+ )
4283 parser .set_defaults (verbose = False , extra_verbose = False )
4384
4485 args = parser .parse_args ()
4586
4687 if args .language_unprocessed :
47- languages = args .language_unprocessed .split (',' )
88+ languages = args .language_unprocessed .split ("," )
4889 args .language = [lang .strip () for lang in languages ]
4990 for lang in languages :
5091 if lang not in VALID_PROGRAMMING_LANGUAGES :
@@ -69,10 +110,7 @@ def main():
69110 logging .basicConfig (
70111 level = level ,
71112 format = "%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d - %(message)s" ,
72- handlers = [
73- logging .FileHandler ("debug.log" ),
74- logging .StreamHandler ()
75- ]
113+ handlers = [logging .FileHandler ("debug.log" ), logging .StreamHandler ()],
76114 )
77115
78116 logging .info (args )
@@ -90,7 +128,8 @@ def main():
90128
91129 if not leetcode .set_cookies (cookies ):
92130 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" )
131+ "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"
132+ )
94133 exit (1 )
95134
96135 # Create output folder if it doesn't already exist
@@ -101,38 +140,52 @@ def main():
101140 title_slug_to_problem_folder_name : dict [str , str ] = dict ()
102141
103142 for submission in leetcode .get_submissions ():
104- if args .only_accepted and submission .status_display != ' Accepted' :
143+ if args .only_accepted and submission .status_display != " Accepted" :
105144 continue
106145
107146 if args .language and submission .lang not in args .language :
108147 continue
109148
110149 if submission .title_slug not in title_slug_to_problem_folder_name :
111150 problem_statement = leetcode .get_problem_statement (submission .title_slug )
112- problem_folder_name = problem_folder_name_template .substitute (** problem_statement .__dict__ )
113- title_slug_to_problem_folder_name [submission .title_slug ] = problem_folder_name
151+ problem_folder_name = problem_folder_name_template .substitute (
152+ ** problem_statement .__dict__
153+ )
154+ title_slug_to_problem_folder_name [
155+ submission .title_slug
156+ ] = problem_folder_name
114157 if not os .path .exists (problem_folder_name ):
115158 os .mkdir (problem_folder_name )
116159 os .chdir (problem_folder_name )
117160
118- problem_statement_filename = problem_statement_filename_template .substitute (** problem_statement .__dict__ )
161+ problem_statement_filename = problem_statement_filename_template .substitute (
162+ ** problem_statement .__dict__
163+ )
119164 if not os .path .exists (problem_statement_filename ):
120- with open (problem_statement_filename , 'w+' ) as problem_statement_file :
121- problem_statement_file .write (problem_statement_template .substitute (** problem_statement .__dict__ ))
165+ with open (problem_statement_filename , "w+" ) as problem_statement_file :
166+ problem_statement_file .write (
167+ problem_statement_template .substitute (
168+ ** problem_statement .__dict__
169+ )
170+ )
122171 else :
123172 os .chdir (title_slug_to_problem_folder_name [submission .title_slug ])
124173
125- submission_filename = submission_filename_template .substitute (** submission .__dict__ )
174+ submission_filename = submission_filename_template .substitute (
175+ ** submission .__dict__
176+ )
126177 if not os .path .exists (submission_filename ):
127178 logging .info (f"Writing { submission .title_slug } /{ submission_filename } " )
128- sub_file = open (submission_filename , 'w+' )
179+ sub_file = open (submission_filename , "w+" )
129180 sub_file .write (submission .code )
130181 sub_file .close ()
131182 else :
132- logging .info (f"{ submission .title_slug } /{ submission_filename } already exists, skipping it" )
183+ logging .info (
184+ f"{ submission .title_slug } /{ submission_filename } already exists, skipping it"
185+ )
133186
134187 os .chdir (".." )
135188
136189
137- if __name__ == ' __main__' :
190+ if __name__ == " __main__" :
138191 main ()
0 commit comments