11import requests
22from termcolor import colored
3+ from rich .console import Console
4+ from rich .markdown import Markdown
5+ import sys as sys
36
7+ # the rich console
8+ console = Console ()
49
5- class Utility :
10+ # render markdown text in the terminal
11+ # def print_markdown(markdown):
12+ # md = Markdown(markdown)
13+ # console.print(md)
14+
15+
16+ class MarkdownRenderer (object ):
17+ def __init__ (self , markdown_text , console_print = True ):
18+ assert isinstance (markdown_text , str ), "Expected a string"
19+
20+ self .markdown_text = markdown_text
21+ self .do_console_print = bool (console_print )
22+
23+ self .console = Console () # rich console
24+
25+ self .render = self .print_mark_down_text ()
26+
27+ def print_mark_down_text (self ):
28+ rendered_markdown = Markdown (self .markdown_text )
29+
30+ if self .do_console_print :
31+ self .console .print (rendered_markdown )
32+
33+ return rendered_markdown
34+
35+ def __repr__ (self ):
36+ return str (self .render )
37+
38+
39+ class SearchError ():
40+ def __init__ (self , error_statement , suggestion = "Try again" ):
41+ # the error statement
42+ self .error_statement = error_statement
43+
44+ # the suggestion statement
45+ self .suggestion = suggestion
46+
47+ self .evoke_search_error (self .error_statement )
48+
49+ def evoke_search_error (self , error_statement ):
50+ print_text = [
51+ colored (error_statement , 'red' ),
52+ colored (self .suggestion , 'green' )
53+ ]
54+ for text_to_print in print_text :
55+ print (text_to_print )
56+
57+
58+ class Utility ():
59+ def __init__ (self ):
60+ # the parent url
61+ self .search_content_url = "https://api.stackexchange.com/"
62+
63+ def __get_search_url (self , question , tags ):
64+ return f"{ self .search_content_url } /2.2/search/advanced?order=desc&sort=relevance&tagged={ tags } &title={ question } &site=stackoverflow"
665
766 def make_request (self , que , tag : str ):
867 """
@@ -16,8 +75,11 @@ def make_request(self, que, tag: str):
1675 :rtype: Json format data
1776 """
1877 print ("Searching for the answer" )
19- resp = requests .get ("https://api.stackexchange.com/" +
20- "/2.2/search/advanced?order=desc&sort=relevance&tagged={}&title={}&site=stackoverflow" .format (tag , que ))
78+ try :
79+ resp = requests .get (self .__get_search_url (que , tag ))
80+ except :
81+ SearchError ("Search Failed" , "Try connecting to the internet" )
82+ sys .exit ()
2183 return resp .json ()
2284
2385 def get_que (self , json_data ):
@@ -30,14 +92,40 @@ def get_que(self, json_data):
3092 def get_ans (self , questions_list ):
3193 # ans = []
3294 for questions in range (1 ):
33- resp = requests .get ("https://api.stackexchange.com/" +
34- "/2.2/questions/{}/answers?order=desc&sort=activity&site=stackoverflow&filter=!--1nZwsgqvRX" .format (questions_list [questions ]))
95+ try :
96+ resp = requests .get (
97+ f"{ self .search_content_url } /2.2/questions/{ questions_list [questions ]} /answers?order=desc&sort=activity&site=stackoverflow&filter=!--1nZwsgqvRX"
98+ )
99+ except :
100+ SearchError ("Search Failed" , "Try connecting to the internet" )
101+ sys .exit ()
35102 json_ans_data = resp .json ()
36- print (
37- colored ("--------------------------------------------------------" , 'red' ))
38- for data in json_ans_data ['items' ]:
39- print (data ["body_markdown" ])
40- print ("Link to answer : " , end = " " )
41- print (data ["link" ])
42- print (
43- colored ("--------------------------------------------------------" , 'red' ))
103+
104+ for data in json_ans_data ["items" ]:
105+ output_content = [
106+ colored (
107+ "--------------------------------------------------------" ,
108+ 'red' ), data ["body_markdown" ],
109+ f"Link to the answer:{ data ['link' ]} "
110+ ]
111+
112+ for output_index , output_text in enumerate (output_content ):
113+ """
114+ Loop through the output_text and print the element
115+ if it the last one, the text[0] is printed
116+ along with text[-1]
117+
118+ if text is markdown , render the markdown
119+ """
120+ if output_index == len (output_content ) - 1 :
121+ console .print (output_text )
122+
123+ console .print (output_content [0 ])
124+ break
125+
126+ if output_index == len (output_content ) - 2 :
127+ renderer = MarkdownRenderer (output_text )
128+
129+ continue
130+
131+ console .print (output_text )
0 commit comments