44from leetcode .models .graphql_get_question_detail import GetQuestionDetail
55from leetcode .models .graphql_question_content import QuestionContent
66from leetcode .models .graphql_question_info_table import QuestionInfoTable
7+ from leetcode .models .graphql_problemset_question_list import ProblemTotalCount , ProblemsetQuestionList
78
89
910class ProblemInfo (QueryTemplate ):
@@ -53,22 +54,42 @@ def parse_args(self, args):
5354 self .fileFlag = True
5455
5556 def execute (self , args ):
56- self .parse_args (args )
57- try :
58- with Loader ('Fetching problem info...' , '' ):
59- self .result = self .leet_api .get_request (self .API_URL )
60- if getattr (args , 'id' ):
61- for item in self .result .get ('stat_status_pairs' , []):
62- if item ['stat' ].get ('question_id' ) == args .id :
63- self .title_slug = item ['stat' ].get ('question__title_slug' , '' )
64- break
65- if not self .title_slug :
66- raise ValueError ("Invalid ID has been provided. Please try again." )
67- self .show ()
68- except Exception as e :
69- console .print (f"{ e .__class__ .__name__ } : { e } " , style = ALERT )
70- if self .fileFlag :
71- self .create_submission_file ()
57+ self .parse_args (args )
58+ if getattr (args , 'random' ):
59+ total = ProblemTotalCount ({'status' : 'NOT_STARTED' }).__call__ ()
60+
61+ from random import randint
62+ with Loader ('Selecting random problem...' , '' ):
63+ choosen_number = randint (1 , total )
64+ while True :
65+ list_instance = ProblemsetQuestionList ({'status' : 'NOT_STARTED' }, limit = 1 , skip = choosen_number - 1 )
66+ problem = list_instance .get_data ()['problemsetQuestionList' ]['questions' ][0 ]
67+ if not problem ['paidOnly' ]:
68+ break
69+ choosen_number = randint (1 , total )
70+
71+ with Loader ('Fetching problem contents...' , '' ):
72+ question_info_table = QuestionInfoTable (problem ['titleSlug' ])
73+ question_content = QuestionContent (problem ['titleSlug' ])
74+ console .print (question_info_table )
75+ console .print (question_content )
76+
77+ else :
78+ try :
79+ with Loader ('Fetching problem info...' , '' ):
80+ self .result = self .leet_api .get_request (self .API_URL )
81+ if getattr (args , 'id' ):
82+ for item in self .result .get ('stat_status_pairs' , []):
83+ if item ['stat' ].get ('question_id' ) == args .id :
84+ self .title_slug = item ['stat' ].get ('question__title_slug' , '' )
85+ break
86+ if not self .title_slug :
87+ raise ValueError ("Invalid ID has been provided. Please try again." )
88+ self .show ()
89+ except Exception as e :
90+ console .print (f"{ e .__class__ .__name__ } : { e } " , style = ALERT )
91+ if self .fileFlag :
92+ self .create_submission_file ()
7293
7394 def create_submission_file (self ):
7495 question = GetQuestionDetail (self .title_slug )
@@ -88,4 +109,4 @@ def show(self):
88109 question_info_table = QuestionInfoTable (self .title_slug )
89110 console .print (question_info_table )
90111 question_content = QuestionContent (self .title_slug )
91- console .print (question_content )
112+ console .print (question_content )
0 commit comments