-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
55 lines (44 loc) · 1.81 KB
/
main.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import time
from sys import platform
import pyperclip
from googletrans import Translator
from PyDictionary import PyDictionary
translator = Translator()
dictionary = PyDictionary()
class Record:
def __init__(self):
self.content = pyperclip.paste()
def refresh(self):
self.content = pyperclip.paste()
def check_change(self):
return self.content != pyperclip.paste()
def notify(title, text):
match platform:
case "linux":
os.system(f"notify-send '{title}' '{text}'")
case "win32":
os.system(f"powershell.exe -Command Add-Type –AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('{text}', '{title}')")
case "darwin":
os.system("""osascript -e 'display notification "{}" with title "{}"'""".format(text, title))
case _:
raise ValueError("Unsupported platform")
def main(target_language: str = "zh-cn"):
title: str = translator.translate("Translation", dest=target_language).text
record = Record()
while True:
time.sleep(0.5)
if record.check_change():
if len(pyperclip.paste()) == 1:
content = pyperclip.paste().replace(",", "")
translation = translator.translate(content, dest=target_language).text
meaning = str(dictionary.meaning(content)).replace("'", "")
meaning = translator.translate(meaning, dest=translator.detect(content).lang).text
notify(title, "\n".join([translation, meaning]))
else:
content = pyperclip.paste().replace(",", "")
translation = translator.translate(content, dest=target_language).text
notify(title, translation)
record.refresh()
if __name__ == "__main__":
main("en")