mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2024-12-29 00:24:13 +08:00
issues/488
This commit is contained in:
parent
d5b3f73cef
commit
734e6782d7
@ -190,9 +190,11 @@ class Sessionbase:
|
|||||||
def request(self,
|
def request(self,
|
||||||
method, url, params=None, data=None, headers=None,proxies=None, json=None,cookies=None, files=None,
|
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, ):
|
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:
|
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()
|
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)
|
_= self.request_impl(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify)
|
||||||
|
|
||||||
return _
|
return _
|
||||||
get=partialmethod(request,"GET")
|
def get(self, url, **kwargs):
|
||||||
post=partialmethod(request,"POST")
|
return self.request("GET", url, **kwargs)
|
||||||
options=partialmethod(request,"OPTIONS")
|
def post(self, url, **kwargs):
|
||||||
|
return self.request("POST", url, **kwargs)
|
||||||
|
def options(self, url, **kwargs):
|
||||||
|
return self.request("OPTIONS", url, **kwargs)
|
||||||
Sessionimpl=[Sessionbase]
|
Sessionimpl=[Sessionbase]
|
||||||
def request(method, url, **kwargs):
|
def request(method, url, **kwargs):
|
||||||
with Sessionimpl[0]() as session:
|
with Sessionimpl[0]() as session:
|
||||||
|
@ -13,21 +13,21 @@ class TS(basetrans):
|
|||||||
self.checkempty(['SECRET_KEY'])
|
self.checkempty(['SECRET_KEY'])
|
||||||
access_key_id = self.multiapikeycurrent['Access_Key']
|
access_key_id = self.multiapikeycurrent['Access_Key']
|
||||||
access_key_secret = self.multiapikeycurrent['SECRET_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 = {
|
req_body = {
|
||||||
"Action": "Translate",
|
"Action": "Translate",
|
||||||
"FormatType": "text",
|
"FormatType": "text",
|
||||||
"SourceLanguage": self.srclang,
|
"SourceLanguage": self.srclang,
|
||||||
"TargetLanguage": self.tgtlang,
|
"TargetLanguage": self.tgtlang,
|
||||||
"SourceText": query,
|
"SourceText": query,
|
||||||
"Scene": "title"
|
"Scene": "general"
|
||||||
}
|
}
|
||||||
req_body = json.dumps(req_body)
|
req_body = json.dumps(req_body)
|
||||||
date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
|
date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
|
||||||
nonce = str(uuid.uuid4())
|
nonce = str(uuid.uuid4())
|
||||||
md5 = hashlib.md5(req_body.encode('utf-8'))
|
md5 = hashlib.md5(req_body.encode('utf-8'))
|
||||||
content_md5 = base64.b64encode(md5.digest()).decode('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')
|
key = access_key_secret.encode('utf-8')
|
||||||
message = stringToSign.encode('utf-8')
|
message = stringToSign.encode('utf-8')
|
||||||
signed = hmac.new(key, message, digestmod=hashlib.sha1).digest()
|
signed = hmac.new(key, message, digestmod=hashlib.sha1).digest()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user