1
1
from leetcode .models import *
2
2
from leetcode .models .graphql_question_content import QuestionContent
3
3
from leetcode .models .graphql_question_info_table import QuestionInfoTable
4
+ from leetcode .models .graphql_get_question_detail import GetQuestionDetail
4
5
5
6
@dataclass
6
7
class QueryResult (JSONWizard ):
@@ -40,6 +41,7 @@ def __init__(self):
40
41
# Instance specific variables
41
42
self .contentFlag : bool = False
42
43
self .browserFlag : bool = False
44
+ self .fileFlag : bool = False
43
45
self .title_slug : str = None
44
46
45
47
self .data = None
@@ -72,7 +74,11 @@ def _execute(self, args) -> None:
72
74
self .data = self .leet_API .post_query (self .graphql_query )
73
75
self .data = QueryResult .from_dict (self .data ['data' ])
74
76
self .title_slug = self .data .question .titleSlug
77
+
75
78
self .show ()
79
+
80
+ if self .fileFlag :
81
+ self .create_file (self .title_slug )
76
82
77
83
def show (self ) -> None :
78
84
""" Shows the question information and content or opens the question in a browser.
@@ -91,6 +97,15 @@ def show(self) -> None:
91
97
self .open_in_browser (link )
92
98
else :
93
99
print (question_info_table )
100
+
101
+ @classmethod
102
+ def create_file (cls , title_slug : str ) -> None :
103
+ """ Creates a file with the question content. """
104
+ question = GetQuestionDetail (title_slug )
105
+ filename = f"{ question .question_id } .{ question .title_slug } .py"
106
+ with open (filename , 'w' ) as file :
107
+ file .write (question .code_snippet )
108
+ console .print (f"File '{ filename } ' has been created." )
94
109
95
110
def __parse_args (self , args ) -> None :
96
111
""" Parses the command line arguments.
@@ -102,6 +117,9 @@ def __parse_args(self, args) -> None:
102
117
self .browserFlag = True
103
118
if getattr (args , 'contents' ):
104
119
self .contentFlag = True
120
+ if getattr (args , 'file' ):
121
+ self .fileFlag = True
122
+
105
123
106
124
107
125
0 commit comments