22import requests
33from rich .console import Console
44import sys as sys
5+ import os
6+ import time
7+ from collections import defaultdict
58
69from .error import SearchError
710from .save import SaveSearchResults
@@ -42,7 +45,6 @@ def __get_search_url(self, question, tags):
4245 def make_request (self , que , tag : str ):
4346 """
4447 This function uses the requests library to make the rest api call to the stackexchange server.
45-
4648 :param que: The user questions that servers as a question in the api.
4749 :type que: String
4850 :param tag: The tags that user wants for searching the relevant answers. For e.g. TypeError might be for multiple languages so is tag is used as "Python" then the api will return answers based on the tags and question.
@@ -75,35 +77,56 @@ def get_ans(self, questions_list):
7577 This function prints the answer to the queries
7678 (question and tags) provided by the user. It does so
7779 in the following manner :
78-
79- 1) Takes in account only th first question id from the
80- list of question ids
81-
82- 2) Tries to get a response from the url obtained by appending
83- question id to the search_content_url variable
84-
85- 3) Use the data received from the above request and loop
86- through it to print the answer
80+ 1) Gets the details of all the relevant question and stores their title, link and id in "question_data" list.
81+ 2) I have introduced the concept of active question, i.e. , the question whose answer is currently being displayed.
82+ 3) The index of the active question in "question_data" array is stored in "question_posx"
83+ 2) By Default, shows the answer to the first question. Creates an breakable infinite loop asking the user answer to which question he wants to see.
84+ 4) The value of "question_posx" changes according to user input, displaying answer to different questions in "questions_data" list.
85+ 3) The answers to the questions requested by the user are stored in cache for faster access time during subsequent calls.
8786 """
88- ans = []
89- for questions in range (1 ):
90- try :
91- resp = requests .get (
92- f"{ self .search_content_url } /2.2/questions/{ questions_list [questions ]} /answers?order=desc&sort=votes&site=stackoverflow&filter=!--1nZwsgqvRX"
93- )
94- except :
95- SearchError ("\U0001F613 Search Failed" , "\U0001F4BB Try connecting to the internet" )
96- sys .exit ()
97- json_ans_data = resp .json ()
98-
99- for data in json_ans_data ["items" ]:
100- output_content = [
101- colored (
102- "--------------------------------------------------------" ,
103- 'red' ), data ["body_markdown" ],
104- f"\U0001F517 Link to the answer:{ data ['link' ]} "
105- ]
106-
87+ # Create batch request to get details of all the questions
88+ batch_ques_id = ""
89+ for question_id in questions_list :
90+ batch_ques_id += str (question_id ) + ";"
91+ try :
92+ resp = requests .get (
93+ f"{ self .search_content_url } /2.2/questions/{ batch_ques_id [:- 1 ]} ?order=desc&sort=votes&site=stackoverflow&filter=!--1nZwsgqvRX"
94+ )
95+ except :
96+ SearchError ("Search Failed" , "Try connecting to the internet" )
97+ sys .exit ()
98+ json_ques_data = resp .json ()
99+ """
100+ Store the received questions data into the following data format:
101+ list( list( question_title, question_link, question_id ) )
102+ """
103+ questions_data = [[item ['title' ], item ['link' ], item ['question_id' ]] for item in json_ques_data ["items" ] ]
104+ # Clear terminal
105+ os .system ('cls' if os .name == 'nt' else 'clear' )
106+
107+ # cache array to store the requested answers. Format of storage { question_id:[answer_body, answer_link] }
108+ downloaded_questions_cache = defaultdict (lambda : False )
109+
110+ # Stores the currently showing question index in questions_data
111+ question_posx = 0
112+
113+ while True :
114+ os .system ('cls' if os .name == 'nt' else 'clear' )
115+ # Printing all the questions. The active question is printed GREEN.
116+ console .rule ('[bold blue] Relevant Questions' , style = "bold red" )
117+ for idx , question_data in enumerate (questions_data ):
118+ if question_posx == idx :
119+ console .print ("[green]{}. {} | {}" .format (idx + 1 , question_data [0 ], question_data [1 ]))
120+ else :
121+ console .print ("{}. {} | {}" .format (idx + 1 , question_data [0 ], question_data [1 ]))
122+ console .rule ("[bold blue] Answer of question {}" .format (question_posx + 1 ), style = "bold red" )
123+
124+ # Gets the question_id of active question
125+ current_question_id = questions_data [question_posx ][2 ]
126+
127+ # Searches for the id in cache. If present then prints it
128+ if (downloaded_questions_cache [current_question_id ]):
129+ output_content = downloaded_questions_cache [current_question_id ]
107130 for output_index , output_text in enumerate (output_content ):
108131 """
109132 Loop through the output_text and print the element
@@ -113,19 +136,43 @@ def get_ans(self, questions_list):
113136 if text is markdown , render the markdown
114137 """
115138 if output_index == len (output_content ) - 1 :
116- console .print (output_text )
117-
118- console .print (output_content [0 ])
139+ console .print ("Link to the answer: " + output_text )
119140 break
120141
121142 if output_index == len (output_content ) - 2 :
122- renderer = MarkdownRenderer (output_text )
123-
143+ MarkdownRenderer (output_text )
124144 continue
125145
126146 console .print (output_text )
127- ans .append (json_ans_data ["items" ])
128- return ans
147+ # If the cache has no entry for the said question id, then downloads the answer
148+ # and makes an for it entry in the cache array in the said format and restarts the loop.
149+ else :
150+ try :
151+ resp = requests .get (
152+ f"{ self .search_content_url } /2.2/questions/{ current_question_id } /answers?order=desc&sort=votes&site=stackoverflow&filter=!--1nZwsgqvRX"
153+ )
154+ except :
155+ SearchError ("Search Failed" , "Try connecting to the internet" )
156+ sys .exit ()
157+ json_ans_data = resp .json ()
158+ print (json_ans_data ["items" ])
159+ most_upvoted = json_ans_data ["items" ][0 ]
160+ downloaded_questions_cache [current_question_id ] = [most_upvoted ["body_markdown" ], most_upvoted ['link' ]]
161+ del most_upvoted
162+ continue
163+
164+ console .rule ("[bold blue]" , style = "bold red" , align = "right" )
165+ # Asks the user for next question number. Makes it the active question and the loop restarts
166+ while True :
167+ posx = int (input ("Enter the question number you want to view (Press 0 to quit): " )) - 1
168+ if (posx == - 1 ):
169+ return
170+ elif (0 <= posx < len (questions_data )):
171+ question_posx = posx
172+ break
173+ else :
174+ console .print ("Please enter a valid question number" )
175+ continue
129176
130177 # Get an access token and extract to a JSON file "access_token.json"
131178 @classmethod
0 commit comments