This commit is contained in:
恍兮惚兮 2024-10-04 16:34:28 +08:00
parent 3b52d28f5b
commit 05df805d71
2 changed files with 44 additions and 23 deletions

View File

@ -143,22 +143,34 @@ class OCR(baseocr):
} }
def initocr(self): def initocr(self):
self.keys = {} self.access = {}
if self.config["接口"] != 5: if self.config["接口"] != 5:
self.getaccess() self.getaccess()
def get_access_token(self, API_KEY, SECRET_KEY):
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {
"grant_type": "client_credentials",
"client_id": API_KEY,
"client_secret": SECRET_KEY,
}
js = self.proxysession.post(url, params=params).json()
try:
return js["access_token"]
except:
raise Exception(js)
def getaccess(self): def getaccess(self):
self.checkempty(["API Key", "Secret Key"]) self.checkempty(["API Key", "Secret Key"])
pair = (self.config["API Key"], self.config["Secret Key"]) SECRET_KEY, API_KEY = (
if pair not in self.keys: self.config["Secret Key"],
accstoken = self.proxysession.get( self.config["API Key"],
"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" )
+ self.appid if not self.access.get((API_KEY, SECRET_KEY)):
+ "&client_secret=" acss = self.get_access_token(API_KEY, SECRET_KEY)
+ self.secretKey self.access[(API_KEY, SECRET_KEY)] = acss
).json()["access_token"] return self.access[(API_KEY, SECRET_KEY)]
self.keys[pair] = accstoken
return self.keys[pair]
def ocr(self, imagebinary): def ocr(self, imagebinary):
if self.config["接口"] in [0, 1, 2, 3]: if self.config["接口"] in [0, 1, 2, 3]:

View File

@ -6,7 +6,7 @@ import random
class TS(basetrans): class TS(basetrans):
def inittranslator(self): def inittranslator(self):
self.keys = {} self.access = {}
if self.config["interface"] == 1: if self.config["interface"] == 1:
self.getaccess() self.getaccess()
@ -31,21 +31,30 @@ class TS(basetrans):
return self.translate_bce(query) return self.translate_bce(query)
raise Exception("unknown") raise Exception("unknown")
def get_access_token(self, API_KEY, SECRET_KEY):
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {
"grant_type": "client_credentials",
"client_id": API_KEY,
"client_secret": SECRET_KEY,
}
js = self.proxysession.post(url, params=params).json()
try:
return js["access_token"]
except:
raise Exception(js)
def getaccess(self): def getaccess(self):
self.checkempty(["API Key", "Secret Key"]) self.checkempty(["API Key", "Secret Key"])
pair = ( SECRET_KEY, API_KEY = (
self.multiapikeycurrent["API Key"],
self.multiapikeycurrent["Secret Key"], self.multiapikeycurrent["Secret Key"],
self.multiapikeycurrent["API Key"],
) )
if pair not in self.keys: if not self.access.get((API_KEY, SECRET_KEY)):
accstoken = self.proxysession.get( acss = self.get_access_token(API_KEY, SECRET_KEY)
"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" self.access[(API_KEY, SECRET_KEY)] = acss
+ self.appid return self.access[(API_KEY, SECRET_KEY)]
+ "&client_secret="
+ self.secretKey
).json()["access_token"]
self.keys[pair] = accstoken
return self.keys[pair]
def translate_bce(self, q): def translate_bce(self, q):
accstoken = self.getaccess() accstoken = self.getaccess()