@@ -565,7 +565,12 @@ def exit_with_error(code, message, title="Error"):
565565 time .sleep (2 )
566566 sys .exit (code )
567567
568- def utfprint (str ):
568+ def utfprint (str , importance = 2 ): #0 = only debugmode, 1 = except quiet, 2 = always print
569+ if args .debugmode < 1 :
570+ if importance == 1 and (args .debugmode == - 1 or args .quiet ):
571+ return
572+ if importance == 0 :
573+ return
569574 maxlen = 32000
570575 if args .debugmode >= 1 :
571576 maxlen = 64000
@@ -1285,18 +1290,26 @@ def websearch(query):
12851290 import urllib .parse
12861291 import urllib .request
12871292 import difflib
1293+ import random
12881294 from html .parser import HTMLParser
12891295 from concurrent .futures import ThreadPoolExecutor
12901296 num_results = 3
12911297 searchresults = []
1292- if args .debugmode != - 1 and not args .quiet :
1293- print ("Performing new websearch..." )
1294-
1295- def fetch_searched_webpage (url ):
1298+ utfprint ("Performing new websearch..." ,1 )
1299+
1300+ def fetch_searched_webpage (url , random_agent = False ):
1301+ uagent = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
1302+ if random_agent :
1303+ agents = ["Mozilla/5.0 (Macintosh; Intel Mac OS X 13_2) Gecko/20100101 Firefox/114.0" ,
1304+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" ,
1305+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0" ,
1306+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.1823.79 Safari/537.36 Edg/114.0.1823.79" ,
1307+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.5938.132 Safari/537.36" ]
1308+ uagent = random .choice (agents )
12961309 if args .debugmode :
12971310 utfprint (f"WebSearch URL: { url } " )
12981311 try :
1299- req = urllib .request .Request (url , headers = {'User-Agent' : 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' })
1312+ req = urllib .request .Request (url , headers = {'User-Agent' : uagent })
13001313 with urllib .request .urlopen (req , timeout = 15 ) as response :
13011314 html_content = response .read ().decode ('utf-8' , errors = 'ignore' )
13021315 return html_content
@@ -1307,12 +1320,10 @@ def fetch_searched_webpage(url):
13071320 html_content = response .read ().decode ('utf-8' , errors = 'ignore' )
13081321 return html_content
13091322 except Exception as e :
1310- if args .debugmode != - 1 and not args .quiet :
1311- print (f"Error fetching text from URL { url } : { e } " )
1323+ utfprint (f"Error fetching text from URL { url } : { e } " ,1 )
13121324 return ""
13131325 except Exception as e :
1314- if args .debugmode != - 1 and not args .quiet :
1315- print (f"Error fetching text from URL { url } : { e } " )
1326+ utfprint (f"Error fetching text from URL { url } : { e } " ,1 )
13161327 return ""
13171328 def fetch_webpages_parallel (urls ):
13181329 with ThreadPoolExecutor () as executor :
@@ -1389,13 +1400,15 @@ def handle_data(self, data):
13891400 search_url = f"https://html.duckduckgo.com/html/?q={ encoded_query } "
13901401
13911402 try :
1392- search_html = fetch_searched_webpage (search_url )
1403+ search_html = fetch_searched_webpage (search_url , random_agent = True )
13931404 parser = ExtractResultsParser ()
13941405 parser .feed (search_html )
13951406 titles = parser .titles [:num_results ]
13961407 searchurls = parser .urls [:num_results ]
13971408 descs = parser .descs [:num_results ]
13981409 fetchedcontent = fetch_webpages_parallel (searchurls )
1410+ if len (descs )== 0 :
1411+ utfprint ("No results found! Maybe something went wrong..." ,1 )
13991412 for i in range (len (descs )):
14001413 # dive into the results to try and get even more details
14011414 title = titles [i ]
@@ -1425,8 +1438,7 @@ def handle_data(self, data):
14251438 searchresults .append ({"title" :title ,"url" :url ,"desc" :desc ,"content" :pagedesc })
14261439
14271440 except Exception as e :
1428- if args .debugmode != - 1 and not args .quiet :
1429- print (f"Error fetching URL { search_url } : { e } " )
1441+ utfprint (f"Error fetching URL { search_url } : { e } " ,1 )
14301442 return ""
14311443 if len (searchresults ) > 0 :
14321444 websearch_lastquery = query
@@ -1770,8 +1782,7 @@ def run_blocking(): # api format 1=basic,2=kai,3=oai,4=oai-chat
17701782 global last_non_horde_req_time
17711783 last_non_horde_req_time = time .time ()
17721784
1773- if (args .debugmode != - 1 and not is_quiet ) or args .debugmode >= 1 :
1774- utfprint ("\n Output: " + recvtxt )
1785+ utfprint ("\n Output: " + recvtxt ,1 )
17751786
17761787 if api_format == 1 :
17771788 res = {"data" : {"seqs" : [recvtxt ]}}
@@ -2554,9 +2565,7 @@ def do_POST(self):
25542565 }}).encode ())
25552566 return
25562567
2557- is_quiet = args .quiet
2558- if (args .debugmode != - 1 and not is_quiet ) or args .debugmode >= 1 :
2559- utfprint ("\n Input: " + json .dumps (genparams ))
2568+ utfprint ("\n Input: " + json .dumps (genparams ),1 )
25602569
25612570 if args .foreground :
25622571 bring_terminal_to_foreground ()
@@ -2577,8 +2586,7 @@ def do_POST(self):
25772586 self .end_headers (content_type = 'application/json' )
25782587 self .wfile .write (genresp )
25792588 except Exception as ex :
2580- if args .debugmode :
2581- print (ex )
2589+ utfprint (ex ,0 )
25822590 print ("Generate: The response could not be sent, maybe connection was terminated?" )
25832591 handle .abort_generate ()
25842592 time .sleep (0.2 ) #short delay
@@ -2604,8 +2612,7 @@ def do_POST(self):
26042612 self .end_headers (content_type = 'application/json' )
26052613 self .wfile .write (genresp )
26062614 except Exception as ex :
2607- if args .debugmode :
2608- print (ex )
2615+ utfprint (ex ,0 )
26092616 print ("Generate Image: The response could not be sent, maybe connection was terminated?" )
26102617 time .sleep (0.2 ) #short delay
26112618 return
@@ -2618,8 +2625,7 @@ def do_POST(self):
26182625 self .end_headers (content_type = 'application/json' )
26192626 self .wfile .write (genresp )
26202627 except Exception as ex :
2621- if args .debugmode :
2622- print (ex )
2628+ utfprint (ex ,0 )
26232629 print ("Transcribe: The response could not be sent, maybe connection was terminated?" )
26242630 time .sleep (0.2 ) #short delay
26252631 return
0 commit comments