1- import os
21import time
32import click
43from bs4 import BeautifulSoup
109import leetcode
1110import leetcode .auth
1211import requests
12+ import os
13+ import shutil
14+ import glob
1315
1416
1517def non_lib_configuration (): # had to change name becasue of python-leetcode lib
@@ -61,21 +63,42 @@ def print_question_data(question):
6163
6264def print_test_result (test_data , data_input ):
6365 status_msg = test_data .get ("status_msg" )
64- status_runtime = test_data .get ("status_runtime" )
65- code_answer = test_data .get ("code_answer" )
66- expected_code_answer = test_data .get ("expected_code_answer" )
67- status_color = Colors .GREEN if status_msg == "Accepted" else Colors .RED
68-
69- print ("" .center (40 , "=" ))
70- print (f"{ status_color } { status_msg } { Colors .RESET } ({ status_runtime } )" )
71- print ("" .center (40 , "=" ))
72- print ("input" .center (40 , "-" ))
73- print (data_input )
74- print ("your code output" .center (40 , "-" ))
75- print (code_answer )
76- print ("expected output" .center (40 , "-" ))
77- print (expected_code_answer )
78- print ("" .center (40 , "=" ))
66+
67+ if status_msg == "Accepted" :
68+ status_runtime = test_data .get ("status_runtime" )
69+ code_answer = test_data .get ("code_answer" )
70+ expected_code_answer = test_data .get ("expected_code_answer" )
71+ status_color = Colors .GREEN
72+
73+ print ("" .center (40 , "=" ))
74+ print (f"{ status_color } { status_msg } { Colors .RESET } ({ status_runtime } )" )
75+ print ("" .center (40 , "=" ))
76+ print ("input" .center (40 , "-" ))
77+ print (data_input )
78+ print ("your code output" .center (40 , "-" ))
79+ print (code_answer )
80+ print ("expected output" .center (40 , "-" ))
81+ print (expected_code_answer )
82+ print ("" .center (40 , "=" ))
83+ else :
84+ runtime_error = test_data .get ("runtime_error" )
85+ full_runtime_error = test_data .get ("full_runtime_error" )
86+ status_color = Colors .RED
87+
88+ # Use BeautifulSoup to convert the runtime error message from HTML to plain text
89+ soup = BeautifulSoup (full_runtime_error , "html.parser" )
90+ plain_runtime_error = soup .get_text ()
91+
92+ print ("" .center (40 , "=" ))
93+ print (f"{ status_color } { status_msg } { Colors .RESET } " )
94+ print ("" .center (40 , "=" ))
95+ print ("input" .center (40 , "-" ))
96+ print (data_input )
97+ print ("runtime error" .center (40 , "-" ))
98+ print (runtime_error )
99+ print ("full runtime error" .center (40 , "-" ))
100+ print (plain_runtime_error )
101+ print ("" .center (40 , "=" ))
79102
80103
81104def print_submission_result (submission ): # used python-leetocde library
@@ -144,7 +167,8 @@ def print_submission_result(submission): # used python-leetocde library
144167def initialize_leetcode_api_instance (
145168 leetcode_session , leetcode_csrf_token
146169): # used python-leetocde library
147- configuration = leetcode .Configuration ()
170+ configuration = leetcode \
171+ .Configuration ()
148172 csrf_token = leetcode_csrf_token
149173
150174 configuration .api_key ["x-csrftoken" ] = csrf_token
@@ -153,12 +177,15 @@ def initialize_leetcode_api_instance(
153177 configuration .api_key ["Referer" ] = "https://leetcode.com"
154178 configuration .debug = False
155179
156- api_instance = leetcode .DefaultApi (leetcode .ApiClient (configuration ))
180+ api_instance = leetcode \
181+ .DefaultApi (leetcode
182+ .ApiClient (configuration ))
157183 return api_instance
158184
159185
160186def interpret_solution (title_slug , payload , api_instance ):
161- test_submission = leetcode .TestSubmission (
187+ test_submission = leetcode \
188+ .TestSubmission (
162189 data_input = payload ["data_input" ],
163190 typed_code = payload ["typed_code" ],
164191 question_id = payload ["question_id" ],
@@ -199,7 +226,6 @@ def submit_solution(
199226
200227def process_test_file (leetcode_api_instance , api_instance , test ):
201228 title_slug , lang_name = title_and_file_extension (test )
202- print (lang_name )
203229 question_detail_data = get_question_detail (api_instance , title_slug )
204230 if question_detail_data :
205231 question_id = question_detail_data .get ("questionId" )
@@ -477,6 +503,8 @@ def write_code_snippet_to_file(question_detail_data, lang, title_slug):
477503 if code :
478504 lang_extension = LANG_EXTENSIONS .get (lang )
479505 if lang_extension :
506+ if not os .path .exists ("code_editor" ):
507+ os .makedirs ("code_editor" )
480508 file_path = os .path .join (
481509 "code_editor" ,
482510 f"{ question_detail_data ['questionFrontendId' ]} _{ title_slug } .{ lang_extension } " ,
@@ -540,30 +568,56 @@ def title_and_file_extension(file):
540568
541569def print_help_usage ():
542570 help_message = """
571+ IMPORTANT: python lc.py --lib
572+
543573 Usage:
544- python leetcode .py --config
545- python leetcode .py --config --user-lang <language>
546- python leetcode .py --question/-q <question_id_or_title>
547- python leetcode .py --solve <question_id_or_title>
548- python leetcode .py --test/-t <filename>
549- python leetcode .py --submit/-sb <filename>
574+ python lc .py --config
575+ python lc .py --config --user-lang <language>
576+ python lc .py --question/-q <question_id_or_title>
577+ python lc .py --solve <question_id_or_title>
578+ python lc .py --test/-t <filename>
579+ python lc .py --submit/-sb <filename>
550580
551581 Examples:
552- python leetcode .py --config --user-lang=python3
553- python leetcode .py --question 1
554- python leetcode .py --question add-two-numbers
555- python leetcode .py --question 10:20
556- python leetcode .py --solve/-s add-two-numbers
557- python leetcode .py --solve 1
558- python leetcode .py --test test_file.py
559- python leetcode .py --submit submit_file.py
582+ python lc .py --config --user-lang=python3
583+ python lc .py --question 1
584+ python lc .py --question add-two-numbers
585+ python lc .py --question 10:20
586+ python lc .py --solve/-s add-two-numbers
587+ python lc .py --solve 1
588+ python lc .py --test test_file.py
589+ python lc .py --submit submit_file.py
560590
561591 For any issues or feature requests, please visit:
562592 https://github.com/hrdkmishra/leetcode.py
563593 """
564594 print (help_message )
565595
566596
597+ def replace_files ():
598+ source_dir = "custom_lib_file/"
599+ destination_dir = "venv/Lib/site-packages/leetcode/models/"
600+ file_paths = glob .glob (os .path .join (source_dir , "*" ))
601+
602+ if not file_paths :
603+ print (f"No files found in the source directory '{ source_dir } '." )
604+ return
605+
606+ for src_path in file_paths :
607+ filename = os .path .basename (src_path )
608+ dest_path = os .path .join (destination_dir , filename )
609+
610+ if os .path .exists (dest_path ):
611+ try :
612+ os .remove (dest_path )
613+ shutil .copy (src_path , dest_path )
614+ print (f"File '{ src_path } ' replaced successfully." )
615+ except Exception as e :
616+ print (f"An error occurred while replacing the file: { e } " )
617+ else :
618+ print (f"Destination path '{ dest_path } ' does not exist." )
619+
620+
567621@click .command ()
568622@click .option ("--config" , is_flag = True , help = "Enter credentials and save to config" )
569623@click .option (
@@ -572,6 +626,9 @@ def print_help_usage():
572626 default = "" ,
573627 help = "Set user preferred language (e.g., python3)" ,
574628)
629+ @click .option (
630+ "--lib" , is_flag = True , default = False , help = "Show usage information"
631+ )
575632@click .option (
576633 "--question" ,
577634 "-q" ,
@@ -603,13 +660,16 @@ def print_help_usage():
603660@click .option (
604661 "--help" , "-h" , is_flag = True , default = False , help = "Show usage information"
605662)
606- def main (config , user_lang , question , solve , test , submit , help ):
663+ def main (config , user_lang , question , solve , test , submit , help , lib ):
664+ if lib :
665+ replace_files ()
666+ exit ()
607667 if config :
608668 leetcode_session , csrf_token = non_lib_configuration ()
609669 # If the --user-lang option is provided, save it to config
610670 if user_lang :
611671 save_user_data_to_config (user_lang )
612- exit ()
672+ exit ()
613673 else :
614674 leetcode_session , csrf_token = load_credentials_from_config ()
615675
@@ -647,4 +707,4 @@ def main(config, user_lang, question, solve, test, submit, help):
647707
648708
649709if __name__ == "__main__" :
650- main () # main
710+ main ()
0 commit comments