@@ -28,36 +28,49 @@ def from_dict(cls, data):
2828 return cls (submissions = submissions )
2929
3030
31+
32+ # TODO: Emojis/colors for status display (Accepted, Wrong Answer, Runtime Error)
33+ # TODO: Handle empty submissions table
3134class SubmissionList (QueryTemplate ):
3235 def __init__ (self ):
3336 super ().__init__ ()
3437 # Instance specific variables
35- self .question_slug = ""
36- self .list_view = False
38+ self .question_id : int = None
39+ self .show_terminal = False
3740 self .submission_download = False
3841
3942 self .graphql_query = None
4043 self .result = None
41- self .params = {'offset' : 0 , 'limit' : 20 , 'lastKey' : None , 'questionSlug' : "" }
44+ self .params = {'offset' : 0 , 'limit' : 20 , 'lastKey' : None , 'questionSlug' : None }
4245
4346 def parse_args (self , args ):
44- self .params ['questionSlug' ] = args .question_slug
45- if args .list :
46- self .list_view = True
47- else :
47+ self .question_id = args .id
48+ self .params ['questionSlug' ] = ProblemInfo .get_title_slug (self .question_id )
49+
50+ if getattr (args , 'show' ):
51+ self .show_terminal = True
52+
53+ if getattr (args , 'download' ):
4854 self .submission_download = True
4955
5056 def execute (self , args ):
51- self .parse_args (args )
52-
53- self .graphql_query = GraphQLQuery (self .query , self .params )
54- self .result = self .leet_API .post_query (self .graphql_query )
55- self .result = QuestionSubmisstionList .from_dict (self .result ['data' ])
56- if self .list_view :
57+ try :
58+ with Loader ('Fetching submission list...' , '' ):
59+ self .parse_args (args )
60+ self .graphql_query = GraphQLQuery (self .query , self .params )
61+ self .result = self .leet_API .post_query (self .graphql_query )
62+ self .result = QuestionSubmisstionList .from_dict (self .result ['data' ])
63+ if not self .result .submissions :
64+ raise ValueError ("Apparently you don't have any submissions for this problem." )
5765 self .show ()
58- if self .submission_download :
59- print (self .get_code ())
60-
66+
67+ if self .show_terminal :
68+ self .show_code ()
69+
70+ if self .submission_download :
71+ self .download_submission ()
72+ except Exception as e :
73+ console .print (f"{ e .__class__ .__name__ } : { e } " , style = ALERT )
6174
6275 def show (self ):
6376 table = LeetTable ()
@@ -72,9 +85,55 @@ def show(self):
7285 for x in submissions :
7386 table .add_row (x .id , x .title , x .statusDisplay , x .runtime , x .memory , x .langName )
7487 console .print (table )
88+
89+
90+ @staticmethod
91+ def fetch_accepted (submissions ):
92+ return next ((x for x in submissions if x .statusDisplay == 'Accepted' ), None )
7593
76- def get_code (self ):
77- # TODO: returning the code of the first submission for now
78- submission_details = SubmissionDetails (self .result .submissions [0 ].id )
79- submission_details .execute ()
80- return submission_details .result .code
94+ def show_code (self ):
95+ try :
96+ with Loader ('Fetching latest accepted code...' , '' ):
97+ acc_submission = self .fetch_accepted (self .result .submissions )
98+
99+ if not acc_submission :
100+ raise ValueError ("No accepted submissions found." )
101+
102+ submission_id = acc_submission .id
103+
104+ query = self .parser .extract_query ('SubmissionDetails' )
105+ params = {'submissionId' : submission_id }
106+ graphql_query = GraphQLQuery (query , params )
107+ result = self .leet_API .post_query (graphql_query )
108+
109+ code = result ['data' ]['submissionDetails' ]['code' ]
110+
111+ console .print (rich .rule .Rule ('Latest accepted code' , style = 'bold blue' ), width = 100 )
112+ console .print (rich .syntax .Syntax (code , 'python' , theme = 'monokai' , line_numbers = True ), width = 100 )
113+ except Exception as e :
114+ console .print (f"{ e .__class__ .__name__ } : { e } " , style = ALERT )
115+
116+ def download_submission (self ):
117+ try :
118+ with Loader ('Downloading latest accepted code...' , '' ):
119+ acc_submission = self .fetch_accepted (self .result .submissions )
120+
121+ if not acc_submission :
122+ raise ValueError ("No accepted submissions found." )
123+
124+ query = self .parser .extract_query ('SubmissionDetails' )
125+ params = {'submissionId' : acc_submission .id }
126+ graphql_query = GraphQLQuery (query , params )
127+ result = self .leet_API .post_query (graphql_query )
128+
129+ code = result ['data' ]['submissionDetails' ]['code' ]
130+ file_name = f"{ acc_submission .titleSlug } .{ acc_submission .id } .py"
131+ with open (file_name , 'w' ) as file :
132+ file .write (code )
133+
134+ console .print (f"File saved as { file_name } " )
135+ except Exception as e :
136+ console .print (f"{ e .__class__ .__name__ } : { e } " , style = ALERT )
137+
138+
139+
0 commit comments