Skip to content

Commit

Permalink
add: FREE_RIDER mode
Browse files Browse the repository at this point in the history
  • Loading branch information
CberYellowstone committed Apr 30, 2022
1 parent 47894ac commit e13c3d3
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 51 deletions.
28 changes: 23 additions & 5 deletions Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,37 @@

configPath = f'{os.path.dirname(sys.executable)}/TranslatorConfig.yaml' if isPacked else f'{os.path.dirname(os.path.abspath(__file__))}/TranslatorConfig.yaml'

def writeConfig(data:dict) -> None:
with open(configPath,mode='w',encoding='utf-8') as f:
yaml_dump(data,f,CDumper)
def writeConfig(data: dict) -> None:
with open(configPath, mode='w', encoding='utf-8') as f:
yaml_dump(data, f, CDumper)

def readConfig() -> dict:
with open(configPath,encoding='utf-8') as f:
return yaml_load(f,CLoader)

def initConfig() -> None:
emptyData = {'YOUDAO_KEY': '', 'YOUDAO_SECRET': '', 'CAIYUN_TOKEN': '', 'BAIDU_APPID': '', 'BAIDU_SECRETKEY': '', 'TENCENT_SECERTID': '', 'TENCENT_SECERTKEY': '', 'XIAONIU_KEY':'', 'ALIYUN_KEY':'', 'ALIYUN_SECRET':'', 'SELECTED_TRANSLATORS':[], 'Hotkey_OCR':'Ctrl + Space', 'OCR_APPID':'c788b7aa', 'OCR_SECRET': 'ODFmODQwZWJmZDhlNTIzOTljNGI3OTcy', 'OCR_KEY': '7ec53833f14724cffb810c14e72eef0d'}
emptyData = {'YOUDAO_KEY': '',
'YOUDAO_SECRET': '',
'CAIYUN_TOKEN': '',
'BAIDU_APPID': '',
'BAIDU_SECRETKEY': '',
'TENCENT_SECERTID': '',
'TENCENT_SECERTKEY': '',
'XIAONIU_KEY': '',
'ALIYUN_KEY': '',
'ALIYUN_SECRET': '',
'SELECTED_TRANSLATORS': [],
'Hotkey_OCR': 'Ctrl + Space',
'OCR_APPID': 'c788b7aa',
'OCR_SECRET': 'ODFmODQwZWJmZDhlNTIzOTljNGI3OTcy',
'OCR_KEY': '7ec53833f14724cffb810c14e72eef0d',
'YOUDAO_FREE_RIDER': False,
'CAIYUN_FREE_RIDER': False,
'BAIDU_FREE_RIDER': False,
'TENCENT_FREE_RIDER': False}
writeConfig(emptyData)

if(not os.path.exists(configPath)):
isInit = False
print('Config file not found, creating a new one...')
initConfig()
initConfig()
12 changes: 10 additions & 2 deletions GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def __init__(self, parent) -> None:
'OCR_SECRET': self.LineEdit_OCRSECRET,
'OCR_KEY': self.LineEdit_OCRKEY,
}
self.FreeRiderMapping = {
'YOUDAO_FREE_RIDER': self.CheckBox_Youdao,
'CAIYUN_FREE_RIDER': self.CheckBox_CaiYun,
'BAIDU_FREE_RIDER': self.CheckBox_Baidu,
'TENCENT_FREE_RIDER': self.CheckBox_Tencent,
}
self.getSecretWidget = getSecretWidget_class(self)

def closeEvent(self, event):
Expand All @@ -128,8 +134,8 @@ def replaceWithCurrentConfig(self):
self.ListWidget_SelectedSource.clear()
self.Label_ShortcutKeyText.setText(self.parent.Hotkey_OCR)
configDict = readConfig()
for each in self.LineEditMapping:
self.LineEditMapping[each].setText(configDict[each])
[self.LineEditMapping[each].setText(configDict[each]) for each in self.LineEditMapping]
[self.FreeRiderMapping[each].setChecked(configDict[each]) for each in self.FreeRiderMapping]
for eachTranslator in [each for each in TranslatorMapping if each not in configDict['SELECTED_TRANSLATORS']]:
self.ListWidget_SelectableSource.addItem(eachTranslator)
for each in configDict['SELECTED_TRANSLATORS']:
Expand Down Expand Up @@ -193,6 +199,7 @@ def saveConfig(self):
QtWidgets.QMessageBox.critical(self,"配置有误","至少选择一个翻译源!")
return
data = {each: self.LineEditMapping[each].text() for each in self.LineEditMapping}
data.update({each: self.FreeRiderMapping[each].isChecked() for each in self.FreeRiderMapping})
data['SELECTED_TRANSLATORS'] = self.getCurrentSelectedTranslator()
data['Hotkey_OCR'] = self.Hotkey_OCR
writeConfig(data)
Expand Down Expand Up @@ -276,6 +283,7 @@ def __init__(self, source: str, translatorType: str, aimTextEdit: int):

def run(self):
MutexList[self.aimTextEdit].lock()
self._signal.emit(self.aimTextEdit, '')
result = TranslatorMapping[self.translatorType](self.source)
self._signal.emit(self.aimTextEdit, result)
MutexList[self.aimTextEdit].unlock()
Expand Down
17 changes: 12 additions & 5 deletions TranslatorAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def truncate(input):
CAIYUN_ERRORCODE_DICT = {'Invalid token': f'Token无效{CONFIG_REMIND}'}

def YoudaoTranslator(QueryText: str) -> str:
YOUDAO_URL = "https://openapi.youdao.com/api"
YOUDAO_URL = "https://aidemo.youdao.com/trans" if configs['YOUDAO_FREE_RIDER'] else "https://openapi.youdao.com/api"
curtime = str(int(time.time()))
salt = str(uuid.uuid1())
signStr = configs['YOUDAO_KEY'] + truncate(QueryText) + salt + curtime + configs['YOUDAO_SECRET']
Expand Down Expand Up @@ -143,6 +143,9 @@ def YoudaoTranslator(QueryText: str) -> str:

def CaiYunTranslator(QueryText: str) -> str:
url = "https://api.interpreter.caiyunai.com/v1/translator"
CAIYUN_TOKEN = configs['CAIYUN_TOKEN']
if(configs['CAIYUN_FREE_RIDER']):
CAIYUN_TOKEN = 'ukiw3nrioeilf0mlpam7'
QueryText = [QueryText]
payload = {
"source": QueryText,
Expand All @@ -152,7 +155,7 @@ def CaiYunTranslator(QueryText: str) -> str:
}
headers = {
"content-type": "application/json",
"x-authorization": f"token {configs['CAIYUN_TOKEN']}",
"x-authorization": f"token {CAIYUN_TOKEN}",
}

try:
Expand All @@ -169,14 +172,18 @@ def CaiYunTranslator(QueryText: str) -> str:


def BaiduTranslator(QueryText:str) -> str:
BAIDU_APPID = configs['BAIDU_APPID']
BAIDU_SECRETKEY = configs['BAIDU_SECRETKEY']
BAIDU_URL = 'https://api.fanyi.baidu.com/api/trans/vip/translate'
if(configs['BAIDU_FREE_RIDER']):
BAIDU_APPID = '20151211000007653'
BAIDU_SECRETKEY = 'IFJB6jBORFuMmVGDRude'
fromLang = 'auto'
toLang = 'zh'
salt = random.randint(32768, 65536)
sign = configs['BAIDU_APPID'] + QueryText + str(salt) + configs['BAIDU_SECRETKEY']
sign = BAIDU_APPID + QueryText + str(salt) + BAIDU_SECRETKEY
sign = hashlib.md5(sign.encode()).hexdigest()
request_url = (f'{BAIDU_URL}?appid=' + configs['BAIDU_APPID']+ '&q=' + urllib.parse.quote(QueryText)+ '&from=' + fromLang+ '&to=' + toLang+ '&salt=' + str(salt)+ '&sign=' + sign)

request_url = f'{BAIDU_URL}?appid={BAIDU_APPID}&q={urllib.parse.quote(QueryText)}&from={fromLang}&to={toLang}&salt={str(salt)}&sign={sign}'

response = requests.get(request_url, verify=False, proxies=NOPROXIES)
return_dict = json.loads(response.content.decode('utf-8'))
Expand Down
54 changes: 29 additions & 25 deletions config_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setupUi(self, Config):
self.scrollArea.setWidgetResizable(False)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, -551, 450, 1050))
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, -480, 450, 1050))
self.scrollAreaWidgetContents.setMinimumSize(QtCore.QSize(450, 600))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.Label_MasterTitle = QtWidgets.QLabel(self.scrollAreaWidgetContents)
Expand Down Expand Up @@ -359,34 +359,36 @@ def setupUi(self, Config):
self.LineEdit_AliYunKEY.setFont(font)
self.LineEdit_AliYunKEY.setFocusPolicy(QtCore.Qt.FocusPolicy.ClickFocus)
self.LineEdit_AliYunKEY.setObjectName("LineEdit_AliYunKEY")
self.checkBox = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.checkBox.setGeometry(QtCore.QRect(330, 90, 111, 20))
self.CheckBox_Youdao = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.CheckBox_Youdao.setGeometry(QtCore.QRect(330, 90, 111, 20))
font = QtGui.QFont()
font.setPointSize(10)
self.checkBox.setFont(font)
self.checkBox.setFocusPolicy(QtCore.Qt.FocusPolicy.ClickFocus)
self.checkBox.setObjectName("checkBox")
self.checkBox_2 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.checkBox_2.setGeometry(QtCore.QRect(330, 170, 111, 20))
self.CheckBox_Youdao.setFont(font)
self.CheckBox_Youdao.setFocusPolicy(QtCore.Qt.FocusPolicy.ClickFocus)
self.CheckBox_Youdao.setObjectName("CheckBox_Youdao")
self.CheckBox_CaiYun = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.CheckBox_CaiYun.setGeometry(QtCore.QRect(330, 170, 111, 20))
font = QtGui.QFont()
font.setPointSize(10)
self.checkBox_2.setFont(font)
self.checkBox_2.setFocusPolicy(QtCore.Qt.FocusPolicy.ClickFocus)
self.checkBox_2.setObjectName("checkBox_2")
self.checkBox_3 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.checkBox_3.setGeometry(QtCore.QRect(330, 220, 111, 20))
self.CheckBox_CaiYun.setFont(font)
self.CheckBox_CaiYun.setFocusPolicy(QtCore.Qt.FocusPolicy.ClickFocus)
self.CheckBox_CaiYun.setObjectName("CheckBox_CaiYun")
self.CheckBox_Baidu = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.CheckBox_Baidu.setGeometry(QtCore.QRect(330, 220, 111, 20))
font = QtGui.QFont()
font.setPointSize(10)
self.checkBox_3.setFont(font)
self.checkBox_3.setFocusPolicy(QtCore.Qt.FocusPolicy.ClickFocus)
self.checkBox_3.setObjectName("checkBox_3")
self.checkBox_4 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.checkBox_4.setGeometry(QtCore.QRect(330, 300, 111, 20))
self.CheckBox_Baidu.setFont(font)
self.CheckBox_Baidu.setFocusPolicy(QtCore.Qt.FocusPolicy.ClickFocus)
self.CheckBox_Baidu.setObjectName("CheckBox_Baidu")
self.CheckBox_Tencent = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.CheckBox_Tencent.setEnabled(False)
self.CheckBox_Tencent.setGeometry(QtCore.QRect(330, 300, 111, 20))
font = QtGui.QFont()
font.setPointSize(10)
self.checkBox_4.setFont(font)
self.checkBox_4.setFocusPolicy(QtCore.Qt.FocusPolicy.ClickFocus)
self.checkBox_4.setObjectName("checkBox_4")
self.CheckBox_Tencent.setFont(font)
self.CheckBox_Tencent.setFocusPolicy(QtCore.Qt.FocusPolicy.ClickFocus)
self.CheckBox_Tencent.setCheckable(True)
self.CheckBox_Tencent.setObjectName("CheckBox_Tencent")
self.Line_4 = QtWidgets.QFrame(self.scrollAreaWidgetContents)
self.Line_4.setGeometry(QtCore.QRect(10, 870, 441, 21))
self.Line_4.setFrameShadow(QtWidgets.QFrame.Shadow.Sunken)
Expand Down Expand Up @@ -453,6 +455,8 @@ def setupUi(self, Config):
self.PushButton_SourceUP.clicked.connect(Config.upTranslator) # type: ignore
self.PushButton_SourceDown.clicked.connect(Config.downTranslator) # type: ignore
self.PushButton_SourceEnable_2.clicked.connect(Config.showGetSecretWidget) # type: ignore
self.ListWidget_SelectableSource.doubleClicked['QModelIndex'].connect(Config.addTranslator) # type: ignore
self.ListWidget_SelectedSource.doubleClicked['QModelIndex'].connect(Config.removeTranslator) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Config)

def retranslateUi(self, Config):
Expand Down Expand Up @@ -496,10 +500,10 @@ def retranslateUi(self, Config):
self.Label_AliYun.setText(_translate("Config", "阿里云翻译API"))
self.LineEdit_AliYunSECRET.setPlaceholderText(_translate("Config", "请输入阿里云翻译API_SECRET"))
self.LineEdit_AliYunKEY.setPlaceholderText(_translate("Config", "请输入阿里云翻译API_KEY"))
self.checkBox.setText(_translate("Config", "白嫖有道翻译"))
self.checkBox_2.setText(_translate("Config", "白嫖彩云小译"))
self.checkBox_3.setText(_translate("Config", "白嫖百度翻译"))
self.checkBox_4.setText(_translate("Config", "白嫖腾讯翻译君"))
self.CheckBox_Youdao.setText(_translate("Config", "白嫖有道翻译"))
self.CheckBox_CaiYun.setText(_translate("Config", "白嫖彩云小译"))
self.CheckBox_Baidu.setText(_translate("Config", "白嫖百度翻译"))
self.CheckBox_Tencent.setText(_translate("Config", "白嫖腾讯翻译君"))
self.Label_TranslatorAPISecretSetting_2.setText(_translate("Config", "文字识别API密钥设置:"))
self.LineEdit_OCRAPPID.setPlaceholderText(_translate("Config", "请输入科大讯飞OCR AppId"))
self.LineEdit_OCRSECRET.setPlaceholderText(_translate("Config", "请输入科大讯飞OCR Secret"))
Expand Down
Loading

0 comments on commit e13c3d3

Please sign in to comment.