22import argparse
33import webbrowser
44from termcolor import colored
5- from .utility import Utility , SearchError
65import sys as sys
76
7+ from .error import SearchError
8+ from .utility import Utility
9+ from .save import SaveSearchResults
10+
811
912class Prompt ():
1013 def __init__ (self , message ):
@@ -24,33 +27,42 @@ def __init__(self, arguments):
2427
2528 def search_args (self ):
2629 if self .arguments .search :
27- queries = ["What do you want to search" , "Tags" ]
28- query_solutions = []
29-
30- # ask quesiton
31- for each_query in queries :
32- # Be careful if there are
33- # KeyBpard Interupts or EOErrors
34- try :
35- prompt = Prompt (str (each_query )).prompt ()
36- except :
37- sys .exit ()
38-
39- query_solutions .append (prompt )
40-
41- question , tags = query_solutions [0 ], query_solutions [1 ]
42- json_output = self .utility_object .make_request (question , tags )
43- questions = self .utility_object .get_que (json_output )
44- if questions == []:
45- # evoke an error
46- search_error = SearchError ("No answer found" ,
47- "Please try reddit" )
48- else :
49- self .utility_object .get_ans (questions )
50-
30+ self .search_for_results ()
31+ elif self .arguments .file :
32+ self .search_for_results (True )
5133 elif self .arguments .new :
5234 url = "https://stackoverflow.com/questions/ask"
5335 if type (self .arguments .new ) == str :
5436 webbrowser .open (f"{ url } ?title={ self .arguments .new } " )
5537 else :
5638 webbrowser .open (url )
39+
40+ def search_for_results (self , save = False ):
41+ queries = ["What do you want to search" , "Tags" ]
42+ query_solutions = []
43+
44+ # ask quesiton
45+ for each_query in queries :
46+ # Be careful if there are
47+ # KeyBpard Interupts or EOErrors
48+ try :
49+ prompt = Prompt (str (each_query )).prompt ()
50+ except :
51+ sys .exit ()
52+
53+ query_solutions .append (prompt )
54+
55+ question , tags = query_solutions [0 ], query_solutions [1 ]
56+ json_output = self .utility_object .make_request (question , tags )
57+ questions = self .utility_object .get_que (json_output )
58+ if questions == []:
59+ # evoke an error
60+ search_error = SearchError ("No answer found" , "Please try reddit" )
61+ else :
62+ data = self .utility_object .get_ans (questions )
63+
64+ if save :
65+ filename = SaveSearchResults (data )
66+ print (
67+ colored (f"Answers successfully saved into { filename } " ,
68+ "green" ))
0 commit comments