|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import hashlib
import keyboard
import requests
import time
import tkinter as tk
import pyautogui as gui
import pyperclip as lip
from fake_useragent import UserAgent
"""翻译软件"""
def main(word): # 爬虫获取有道翻译的翻译内容
string = 'fanyideskweb' + word + str(int(time.time() * 10000)) + 'Ygy_4c=r#e#4EX^NUGUc5'
sign = hashlib.md5(string.encode('utf-8')).hexdigest() # MD5解密
fake_ua = UserAgent()
fake_rand = fake_ua.random # 随机获取useragent
bv = hashlib.md5(fake_rand.encode('utf-8')).hexdigest() # MD5解密
url0 = 'https://fanyi.youdao.com/?keyfrom=dict2.index'
url = 'https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
headers = {
'Host': 'fanyi.youdao.com',
'Origin': 'https://fanyi.youdao.com',
'Referer': 'https://fanyi.youdao.com/',
# 随机获取user agent
'User-Agent': fake_rand
}
data = {
'i': word,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': int(time.time() * 10000),
'sign': sign,
'lts': int(time.time() * 1000),
'bv': bv,
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME'
}
requests.packages.urllib3.disable_warnings() # 用来消除一部分网站会报错,这个是在网上查的
resp1 = requests.get(url=url0, headers=headers, verify=False) # <Response [200]>请求成功
cook_ies = resp1.cookies.get_dict() # 获取cookie
resp = requests.post(url=url, data=data, cookies=cook_ies, headers=headers, verify=False)
lists = resp.json()['translateResult'][0]
m = ''
word = word.split('\n')
for w in range(0, len(lists)):
try:
k = resp.json()['translateResult'][0][w]['tgt']
if word == k:
k = resp.json()['translateResult'][0][w]['src']
m = m + k # 对字符串进行拼接
except:
pass
return m
def key1():
keyboard.wait('ctrl+/') # 没按下设置的按键就等待,按下按键后进行翻译
if_button()
def if_button():
gui.hotkey('ctrl', 'c') # 对选中的内容进行复制
value = lip.paste() # 获取剪贴板的内容
global allof # 随便写一个比较复杂的数
if allof == value: # 如果两个值相等,就睡0.2秒,不进行下一步
time.sleep(0.2)
else: # 如果两个值不相等,就运行下面的程序,弹出一个窗口
none_str = ''
for i in range(0, len(value)): # 去掉\n和\r,转换为空格
if value[i] != '\r' and value[i] != '\n':
none_str = none_str + value[i]
else:
none_str = none_str + ' '
translate = main(none_str)
length = len(translate)
window1 = tk.Tk()
'''将剪切板内的单词进行翻译,并在光标下方写出翻译内容'''
'''创建一个窗口'''
'''设置窗口的名称和尺寸'''
window1.title('翻译软件')
x, y = gui.position()
if translate.isascii() and length <= 80: # 如果没有中文
window1.geometry(f'{int(length * 17)}x50+{x - 200}+{y + 20}') # 修改界面大小随翻译字数而改变
elif translate.isascii():
window1.geometry(f'1200x{50 * (length // 80 + 1)}+{x - 200}+{y + 20}') # 大于80个字符就多行显示
else: # 有中文
if length <= 40:
window1.geometry(f'{int(length * 32)}x50+{x - 200}+{y + 20}') # 修改界面大小随翻译字数而改变
else:
window1.geometry(f'1280x{50 * (length // 40 + 1)}+{x - 200}+{y + 20}') # 修改界面大小随翻译字数而改变
'''改变窗口中的内容'''
var = tk.StringVar()
var.set(translate)
if translate.isascii():
w1 = tk.Label(window1, textvariable=var, font='黑体 18', width=80, height=10, anchor='nw', wraplength=1200)
w1.grid(column=0, columnspan=10)
else:
w1 = tk.Label(window1, textvariable=var, font='黑体 18', width=80, height=10, anchor='nw', wraplength=1160)
w1.grid(column=0, columnspan=10)
window1.mainloop()
allof = value # 给全局变量赋值,方便只运行一次,这里有点问题,不能连续翻译相同的东西
allof = '3658796543'
if __name__ == '__main__':
while True:
key1()
time.sleep(0.2)
|
|