diff --git a/LunaTranslator/LunaTranslator/network/requests_common.py b/LunaTranslator/LunaTranslator/network/requests_common.py index 81888798..74b07ab0 100644 --- a/LunaTranslator/LunaTranslator/network/requests_common.py +++ b/LunaTranslator/LunaTranslator/network/requests_common.py @@ -190,9 +190,11 @@ class Sessionbase: def request(self, method, url, params=None, data=None, headers=None,proxies=None, json=None,cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, hooks=None, stream=None, verify=False, cert=None, ): + _h=self.headers.copy() + if headers: + _h.update(headers) + headers=_h - headers=CaseInsensitiveDict(headers if headers else {}) - headers.update(self.headers) if auth and isinstance(auth,tuple) and len(auth)==2: headers['Authorization']="Basic " + ( base64.b64encode(b":".join((auth[0].encode("latin1"), auth[1].encode("latin1")))).strip() ).decode() @@ -204,9 +206,12 @@ class Sessionbase: _= self.request_impl(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify) return _ - get=partialmethod(request,"GET") - post=partialmethod(request,"POST") - options=partialmethod(request,"OPTIONS") + def get(self, url, **kwargs): + return self.request("GET", url, **kwargs) + def post(self, url, **kwargs): + return self.request("POST", url, **kwargs) + def options(self, url, **kwargs): + return self.request("OPTIONS", url, **kwargs) Sessionimpl=[Sessionbase] def request(method, url, **kwargs): with Sessionimpl[0]() as session: diff --git a/LunaTranslator/LunaTranslator/translator/aliyunapi.py b/LunaTranslator/LunaTranslator/translator/aliyunapi.py index 2d26780b..f959ae16 100644 --- a/LunaTranslator/LunaTranslator/translator/aliyunapi.py +++ b/LunaTranslator/LunaTranslator/translator/aliyunapi.py @@ -13,21 +13,21 @@ class TS(basetrans): self.checkempty(['SECRET_KEY']) access_key_id = self.multiapikeycurrent['Access_Key'] access_key_secret = self.multiapikeycurrent['SECRET_KEY'] - url = 'http://mt.cn-hangzhou.aliyuncs.com/api/translate/web/ecommerce' + url = 'http://mt.cn-hangzhou.aliyuncs.com/api/translate/web/general' req_body = { "Action": "Translate", "FormatType": "text", "SourceLanguage": self.srclang, "TargetLanguage": self.tgtlang, "SourceText": query, - "Scene": "title" + "Scene": "general" } req_body = json.dumps(req_body) date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT') nonce = str(uuid.uuid4()) md5 = hashlib.md5(req_body.encode('utf-8')) content_md5 = base64.b64encode(md5.digest()).decode('utf-8') - stringToSign = "{method}\n{accept}\n{md5}\n{content_type}\n{date}\nx-acs-signature-method:HMAC-SHA1\nx-acs-signature-nonce:{nonce}\nx-acs-version:{version}\n{path}".format(method='POST', accept='application/json', md5=content_md5, content_type='application/json; charset=utf-8', date=date, nonce=nonce, version='2019-01-02', path='/api/translate/web/ecommerce') + stringToSign = "{method}\n{accept}\n{md5}\n{content_type}\n{date}\nx-acs-signature-method:HMAC-SHA1\nx-acs-signature-nonce:{nonce}\nx-acs-version:{version}\n{path}".format(method='POST', accept='application/json', md5=content_md5, content_type='application/json; charset=utf-8', date=date, nonce=nonce, version='2019-01-02', path='/api/translate/web/general') key = access_key_secret.encode('utf-8') message = stringToSign.encode('utf-8') signed = hmac.new(key, message, digestmod=hashlib.sha1).digest()