@@ -37,6 +37,7 @@ def parse_flags(supported_sites):
3737
3838 parser .add_argument ('-s' , '--site' ,
3939 dest = 'site' ,
40+ choices = supported_sites ,
4041 help = 'The competitive programming platform, e.g. codeforces, codechef etc' )
4142
4243 parser .add_argument ('-c' , '--contest' ,
@@ -63,7 +64,7 @@ def parse_flags(supported_sites):
6364
6465 parser .add_argument ('--set-workdir' ,
6566 dest = 'workdir' ,
66- help = 'ABSOLUTE PATH to working directory' )
67+ help = 'ABSOLUTE PATH to working directory' )
6768
6869 parser .set_defaults (force = False )
6970
@@ -118,12 +119,12 @@ def set_constants(key, value, supported_sites=None):
118119 for site in supported_sites :
119120 if not os .path .isdir (os .path .join (workdir , site )):
120121 os .makedirs (os .path .join (workdir , site ))
121- choice = raw_input ('Remove all files from previous working directory %s? (y/N) : ' % (previous_path ))
122+ choice = raw_input (
123+ 'Remove all files from previous working directory %s? (y/N) : ' % (previous_path ))
122124 if choice == 'y' :
123125 from shutil import rmtree
124126 rmtree (previous_path )
125127
126-
127128 @staticmethod
128129 def create_workdir_structure (site , contest ):
129130 """
@@ -243,21 +244,19 @@ def cleanup(num_cases):
243244 os .remove ('temp_output' + str (i ))
244245
245246 @staticmethod
246- def handle_kbd_interrupt (args ):
247+ def handle_kbd_interrupt (site , contest , problem ):
247248 """
248249 Method to handle keyboard interrupt
249250 """
250251 from shutil import rmtree
251- print 'Interrupted manually. Cleaning up...'
252+ print 'Cleaning up...'
252253
253- if args ['problem' ] is not None :
254- path = os .path .join (Utilities .cache_dir , args ['site' ], args [
255- 'contest' ], args ['problem' ])
254+ if problem is not None :
255+ path = os .path .join (Utilities .cache_dir , site , contest , problem )
256256 if os .path .isdir (path ):
257257 rmtree (path )
258258 else :
259- path = os .path .join (Utilities .cache_dir , args [
260- 'site' ], args ['contest' ])
259+ path = os .path .join (Utilities .cache_dir , site , contest )
261260 if os .path .isdir (path ):
262261 rmtree (path )
263262
@@ -454,6 +453,12 @@ def parse_html(self, req):
454453 inputs = soup .findAll ('div' , {'class' : 'input' })
455454 outputs = soup .findAll ('div' , {'class' : 'output' })
456455
456+ if len (inputs ) == 0 or len (outputs ) == 0 :
457+ print 'Problem not found..'
458+ Utilities .handle_kbd_interrupt (
459+ self .site , self .contest , self .problem )
460+ sys .exit (0 )
461+
457462 repls = ('<br>' , '\n ' ), ('<br/>' , '\n ' ), ('</br>' , '' )
458463
459464 formatted_inputs , formatted_outputs = [], []
@@ -483,6 +488,13 @@ def get_problem_links(self, req):
483488 soup = bs (req .text , 'html.parser' )
484489
485490 table = soup .find ('table' , {'class' : 'problems' })
491+
492+ if table is None :
493+ print 'Contest not found..'
494+ Utilities .handle_kbd_interrupt (
495+ self .site , self .contest , self .problem )
496+ sys .exit (0 )
497+
486498 links = ['http://codeforces.com' +
487499 td .find ('a' )['href' ] for td in table .findAll ('td' , {'class' : 'id' })]
488500
@@ -561,8 +573,14 @@ def parse_html(self, req):
561573 Method to parse the html and get test cases
562574 from a codechef problem
563575 """
564- data = json .loads (req .text )
565- soup = bs (data ['body' ], 'html.parser' )
576+ try :
577+ data = json .loads (req .text )
578+ soup = bs (data ['body' ], 'html.parser' )
579+ except (KeyError , ValueError ):
580+ print 'Problem not found..'
581+ Utilities .handle_kbd_interrupt (
582+ self .site , self .contest , self .problem )
583+ sys .exit (0 )
566584
567585 test_cases = soup .findAll ('pre' )
568586 formatted_inputs , formatted_outputs = [], []
@@ -603,6 +621,13 @@ def get_problem_links(self, req):
603621 soup = bs (req .text , 'html.parser' )
604622
605623 table = soup .find ('table' , {'class' : 'dataTable' })
624+
625+ if table is None :
626+ print 'Contest not found..'
627+ Utilities .handle_kbd_interrupt (
628+ self .site , self .contest , self .problem )
629+ sys .exit (0 )
630+
606631 links = [div .find ('a' )['href' ]
607632 for div in table .findAll ('div' , {'class' : 'problemname' })]
608633 links = ['https://codechef.com/api/contests/' + self .contest +
@@ -750,8 +775,15 @@ def parse_html(self, req):
750775 Method to parse the html and get test cases
751776 from a hackerrank problem
752777 """
753- data = json .loads (req .text )
754- soup = bs (data ['model' ]['body_html' ], 'html.parser' )
778+
779+ try :
780+ data = json .loads (req .text )
781+ soup = bs (data ['model' ]['body_html' ], 'html.parser' )
782+ except (KeyError , ValueError ):
783+ print 'Problem not found..'
784+ Utilities .handle_kbd_interrupt (
785+ self .site , self .contest , self .problem )
786+ sys .exit (0 )
755787
756788 input_divs = soup .findAll ('div' , {'class' : 'challenge_sample_input' })
757789 output_divs = soup .findAll ('div' , {'class' : 'challenge_sample_output' })
@@ -798,8 +830,16 @@ def get_problem_links(self, req):
798830 Method to get the links for the problems
799831 in a given hackerrank contest
800832 """
801- data = json .loads (req .text )
802- data = data ['models' ]
833+
834+ try :
835+ data = json .loads (req .text )
836+ data = data ['models' ]
837+ except (KeyError , ValueError ):
838+ print 'Contest not found..'
839+ Utilities .handle_kbd_interrupt (
840+ self .site , self .contest , self .problem )
841+ sys .exit (0 )
842+
803843 links = ['https://www.hackerrank.com/rest/contests/' + self .contest +
804844 '/challenges/' + problem ['slug' ] for problem in data ]
805845
0 commit comments