diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index 867ede81..99149c2c 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -41,6 +41,7 @@ class MAINUI() : def __init__(self,app) -> None: super().__init__() self.lasttranslatorindex=0 + self.usefultranslators=0 self.app=app self.translators={} self.cishus={} @@ -167,7 +168,7 @@ class MAINUI() : return if text=='' or len(text)>100000: if embedcallback: - embedcallback('zhs', text) + embedcallback('') return try: @@ -187,7 +188,7 @@ class MAINUI() : if text=='' or (is_auto_run and (text==self.currenttext or len(text)>(max(globalconfig['maxoriginlength'],globalconfig['maxlength'])))): if embedcallback: - embedcallback('zhs', text) + embedcallback('') return @@ -237,6 +238,7 @@ class MAINUI() : skip=is_auto_run and (len(text_solved)globalconfig['maxlength'] ) self.premtalready=['premt'] + self.usefultranslators=len(self.translators) if 'premt' in self.translators: try: res=self.translators['premt'].translate(text_solved) @@ -270,16 +272,26 @@ class MAINUI() : break - def GetTranslationCallback(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,embedcallback): - if embedcallback is None and currentsignature!=self.currentsignature: - return + self.usefultranslators-=1 + if embedcallback is None and currentsignature!=self.currentsignature:return + + embedtrans=self.GetTranslationCallback_(onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res) - if currentsignature==self.currentsignature: - if type(res)==str: - if res.startswith(''): + + if embedtrans is None and self.usefultranslators==0: + embedtrans='' + if embedcallback and embedtrans is not None: + embedcallback(embedtrans) + + def GetTranslationCallback_(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res): + + + if type(res)==str: + if res.startswith(''): + if currentsignature==self.currentsignature: self.translation_ui.displaystatus.emit(globalconfig['fanyi'][classname]['name']+' '+res[len(''):],'red',onlytrans,False) - return + return res=self.solveaftertrans(res,optimization_params) @@ -295,16 +307,16 @@ class MAINUI() : if currentsignature==self.currentsignature and globalconfig['showfanyi']: self.translation_ui.displayres.emit(globalconfig['fanyi'][classname]['name'],globalconfig['fanyi'][classname]['color'],res,onlytrans) + - if embedcallback: - - if globalconfig['embedded']['as_fast_as_posible'] or classname==list(globalconfig['fanyi'])[globalconfig['embedded']['translator']]: - - embedcallback('zhs', kanjitrans(zhconv.convert(res,'zh-tw')) if globalconfig['embedded']['trans_kanji'] else res) - try: self.textsource.sqlqueueput((contentraw,classname,res)) except:pass + + if globalconfig['embedded']['as_fast_as_posible'] or classname==list(globalconfig['fanyi'])[globalconfig['embedded']['translator']]: + + return (kanjitrans(zhconv.convert(res,'zh-tw')) if globalconfig['embedded']['trans_kanji'] else res) + @threader def autoreadcheckname(self): try: diff --git a/LunaTranslator/LunaTranslator/network/requests_common.py b/LunaTranslator/LunaTranslator/network/requests_common.py index c7aa131f..200ccd1f 100644 --- a/LunaTranslator/LunaTranslator/network/requests_common.py +++ b/LunaTranslator/LunaTranslator/network/requests_common.py @@ -205,17 +205,27 @@ class Sessionbase: headers,dataptr,datalen=self._parsedata(data,headers,json) proxy= proxies.get(scheme,None) if proxies else None if timeout: - timeout = int(timeout * 1000) # convert to milliseconds + if isinstance(timeout,(float,int)): + timeout = int(timeout * 1000) # convert to milliseconds + else: + try: + timeout=max(int(_ * 1000) for _ in timeout) + except: + print("Error invalid timeout",timeout) + timeout=None _= self.request_impl(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout) - if _.status_code==301: + if allow_redirects and (_.status_code==301 or _.status_code==302): location=_.headers['Location'] if location.startswith('/'):#vndb url=url=scheme+'://'+server+location param=location - _= self.request_impl(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify) + elif location.startswith('http'):#https://api.github.com/repos/XXX/XXX/zipball + scheme,server,port,param,url=self._parseurl(location,None) else: - raise Exception('301 redirect '+location) + raise Exception('redirect {}: {}'.format(_.status_code,location)) + _= self.request_impl(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout) + return _ def get(self, url, **kwargs): return self.request("GET", url, **kwargs) diff --git a/LunaTranslator/LunaTranslator/textsource/texthook.py b/LunaTranslator/LunaTranslator/textsource/texthook.py index b8dc9ff0..59bac865 100644 --- a/LunaTranslator/LunaTranslator/textsource/texthook.py +++ b/LunaTranslator/LunaTranslator/textsource/texthook.py @@ -215,7 +215,7 @@ class texthook(basetext ): if self.checkisusingembed(tp.addr,tp.ctx,tp.ctx2): self.newline.put((text,False, functools.partial(self.embedcallback,text),True)) - def embedcallback(self,text,_unused,trans): + def embedcallback(self,text,trans): for pid in self.connectedpids: self.Luna_embedcallback(pid,text,trans)