summarylogtreecommitdiffstats
path: root/deepl-clipboard
blob: 4c69bc2f3e435d68ca61439c0e1566341a80fcbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/python3

import os, json, http.client
from pyperclip import paste


def deepl_api(text):
    if not text.replace("\n", "").replace("\r", ""):
        os.system("zenity --warning --text='剪贴板为空,请选择文本'")
        exit(1)
    try:
        conn = http.client.HTTPSConnection("deepl.aya1.pro")
        payload = json.dumps({"text": text, "target_lang": "ZH"})
        conn.request("POST", "/api", payload)
        res = conn.getresponse().read()
        return json.loads(res).get("data")
    except:
        os.system("zenity --error --text='翻译出错,请稍后再试'")
        exit(1)


if __name__ == "__main__":
    text = paste()  # 获取剪贴板
    translate_results = deepl_api(text)  # 调用翻译接口
    print(translate_results, file=open("/tmp/deepl.txt", "w"))  # 将翻译结果写入文件
    os.system(f"zenity --title=Deepl --text-info --filename=/tmp/deepl.txt")  # 显示翻译结果