133 lines
5.0 KiB
Python
133 lines
5.0 KiB
Python
|
|
from BaiduTrans import transBaidu
|
|
import requests
|
|
import random
|
|
import os
|
|
import re
|
|
import time
|
|
from tclient import tenTrans
|
|
from pypinyin import pinyin, lazy_pinyin, Style
|
|
|
|
def getinfo(oldWord):
|
|
print('查询多语言------------------------ ')
|
|
searchUrl='http://vision.xytech.com/api/language/index'
|
|
searchPayload = {'bizId': '48', 'current': '1','pageSize': '10','search':oldWord}
|
|
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36','Accept':'application/json, text/plain, */*','Content-Type':'application/json; charset=utf-8','Cookie':'_clck=1becfu3|1|f11|0; locale=zh_CN; XY_USER=t0LuUri548F8CcdkpOeuJw==s'}
|
|
r = requests.get(searchUrl,headers=headers, params=searchPayload)
|
|
return r
|
|
|
|
def postData(oldWord='',newWord='',Idword='',newkey=''):
|
|
print('!!!!!!===新增多语言====!!!!!!!! ')
|
|
print('中文:',oldWord)
|
|
print('英文:',newWord)
|
|
# print('印尼:',Idword)
|
|
print('key编码:',newkey)
|
|
print('-------------------------------')
|
|
postUrl='http://vision.xytech.com/api/language/create'
|
|
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36','Accept':'application/json, text/plain, */*','Content-Type':'application/json; charset=utf-8','Cookie':'_clck=1becfu3|1|f11|0; locale=zh_CN; XY_USER=t0LuUri548F8CcdkpOeuJw==s'}
|
|
# in_ID印尼语=>'in_ID':Idword,
|
|
postPayload={'bizId':'48','en_US':newWord,'key':newkey,'zh_CN':oldWord,'in_ID':Idword,'id':45809}
|
|
r = requests.post(postUrl,headers=headers, params=postPayload)
|
|
return r
|
|
|
|
|
|
def transI18(oldWord:str,newWord:str,Idword:str):
|
|
# print('调用多语言接口------------------------ ')
|
|
# 生成自由编码
|
|
newkey=''
|
|
try:
|
|
# newkey=newWord.replace(' ','-') +'-'+str(random.randint(1,10000) )
|
|
newkey=newWord.replace(' ','_') +'_'+str(random.randint(1,10000) ) # 下划线容易复制
|
|
# 截取70个字符
|
|
newkey=newkey[0:71]
|
|
newkey=newkey+'_'+str(random.randint(1,10000))
|
|
|
|
|
|
except:
|
|
# lazy_pinyin('中心') ['zhong', 'xin']
|
|
newkey='_'.join(lazy_pinyin(oldWord)) +'_'+str(random.randint(1,10000))
|
|
# 截取70个字符
|
|
newkey=newkey[0:71]
|
|
newkey=newkey+'_'+str(random.randint(1,10000))
|
|
|
|
# newkey=oldWord+'-'+str(random.randint(1,10000))
|
|
print('生成key: ',newkey)
|
|
r = getinfo(oldWord)
|
|
newlist=r.json()["data"]
|
|
|
|
if(len(newlist)>0):
|
|
itemArr=[]
|
|
for item in newlist:
|
|
itemArr.append(item["key"])
|
|
itemArr.append(item["zh_CN"])
|
|
if item["zh_CN"]==oldWord:
|
|
print('中文数据存在------------------------ ')
|
|
newkey=item["key"]
|
|
print('newkey-----',newkey)
|
|
break
|
|
elif item["key"]==oldWord:
|
|
# pass
|
|
print('key数据存在------------------------ ')
|
|
newkey=item["key"]
|
|
break
|
|
else:
|
|
pass
|
|
# print('数据库不存在------------------------ ')
|
|
if oldWord not in itemArr:
|
|
print('数据库不存在------------------------ ')
|
|
#post新增接口
|
|
if newWord != None:
|
|
print('newWord',newWord)
|
|
r =postData(oldWord,newWord,Idword,newkey)
|
|
else:
|
|
print('请检查代理是否关闭')
|
|
|
|
else:
|
|
# print('没有查询结果')
|
|
#post新增接口
|
|
if newWord != None:
|
|
print('newWord',newWord)
|
|
r =postData(oldWord,newWord,Idword,newkey)
|
|
else:
|
|
print('请检查代理是否关闭')
|
|
|
|
|
|
|
|
# print('即将替换的key值------------------------ ',newkey)
|
|
return newkey
|
|
|
|
|
|
# oldWord=input('请输入要翻译的单词:')
|
|
# newWord=transBaidu(oldWord) # 执行翻译
|
|
# key=transI18(oldWord,newWord)
|
|
# globalKey='`${'+'window.i18n("{key}")'.format(key=key)+'}`'
|
|
|
|
def transWord():
|
|
print('windows请先关闭charles和clash代理')
|
|
oldWord=input('请输入要翻译的单词:')
|
|
newWord=transBaidu(oldWord,'zh','en') # 百度-执行中文翻译
|
|
newWord=tenTrans(oldWord,'zh','en') # 执行中文翻译
|
|
Idword=tenTrans(oldWord,'zh','id') # 执行印尼翻译 --api不支持
|
|
key=transI18(oldWord,newWord,Idword)
|
|
win='window.i18n("{key}")'.format(key=key)
|
|
globalKey='`${'+'window.i18n("{key}")'.format(key=key)+'}`'+', //'+'{oldWord}'.format(oldWord=oldWord)
|
|
objStr='{`'+'window.i18n("{key}")'.format(key=key)+'`}'
|
|
print('-----------------')
|
|
print("中文: ",oldWord)
|
|
print("英语: ",newWord)
|
|
print("印尼: ",Idword)
|
|
print('-----------------')
|
|
print("key:",key)
|
|
print("win语句:",win)
|
|
print("str语句:",globalKey)
|
|
print("obj语句:",objStr)
|
|
|
|
|
|
print('-----------------')
|
|
print(' ')
|
|
|
|
|
|
# 主程序-打包exe
|
|
if __name__=='__main__':
|
|
while True:
|
|
transWord() |