-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcordr.py
180 lines (165 loc) · 5.27 KB
/
cordr.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
__author__ = "Ferris Linde"
__copyright__ = "Copyright (C) 2018 Ferris Linde"
__license__ = "Public Domain"
__version__ = "2.0"
__github__ = "https://github.com/ferris"
'''
Cordr, a macOS hidden translator tool.
For educational purposes only. Don't use to cheat!
In order for the keylogging functions to work,
this script must be granted root or accessiblity access.
The 'pynput' module must be installed to run thie file.
'''
from pynput import keyboard
import re
import urllib.parse
import urllib.request
import html
import os
def passIn(phrase, lanTo, lanFrom):
global newPhrase
newPhrase = translate(phrase, lanTo, lanFrom)
print("Translated: " + phrase + " ; to: " + newPhrase)
notify("{} to {}".format(lanFrom, lanTo), newPhrase)
os.system('afplay /System/Library/Sounds/Bottle.aiff')
def notify(title, text):
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(text, title))
# PYNPUT
def on_release(key):
global going
global starter
global userInput
global kb
global newPhrase
try:
print('{0} released'.format(key.char))
if key.char == '`' or key.char == '\\':
print("activator pressed!")
if not going:
print("going = true")
going = True
os.system('afplay /System/Library/Sounds/Frog.aiff')
starter = key.char
else:
print("going = false")
going = False
if starter == "`":
print("translate(\'{}\', \"es\", \"en\")".format(userInput))
passIn(userInput, "es", "en")
else:
print("translate(\'{}\', \"en\", \"es\")".format(userInput))
passIn(userInput, "en", "es")
userInput = ""
elif going:
userInput += key.char
except:
print('special key {0} pressed'.format(key))
if going:
# special modifiers for user input
if key == keyboard.Key.space:
userInput += " "
elif key == keyboard.Key.backspace:
userInput = userInput[:-1]
elif key == keyboard.Key.caps_lock:
typeOut(newPhrase)
def typeOut(s):
# TODO: try to somehow add punctuation
s = s.lower()
for char in s:
if ord(char) >= 97 and ord(char) <= 122:
# lower case a-z
kb.press(char)
kb.release(char)
elif ord(char) == 32:
# space
kb.press(keyboard.Key.space)
kb.release(keyboard.Key.space)
elif ord(char) == 225:
# á
with kb.pressed(keyboard.Key.alt):
kb.press('e')
kb.release('e')
kb.press('a')
kb.release('a')
elif ord(char) == 233:
# é
with kb.pressed(keyboard.Key.alt):
kb.press('e')
kb.release('e')
kb.press('e')
kb.release('e')
elif ord(char) == 237:
# í
with kb.pressed(keyboard.Key.alt):
kb.press('e')
kb.release('e')
kb.press('i')
kb.release('i')
elif ord(char) == 243:
# ó
with kb.pressed(keyboard.Key.alt):
kb.press('e')
kb.release('e')
kb.press('o')
kb.release('o')
elif ord(char) == 250:
# ú
with kb.pressed(keyboard.Key.alt):
kb.press('e')
kb.release('e')
kb.press('u')
kb.release('u')
elif ord(char) == 241:
# ñ
with kb.pressed(keyboard.Key.alt):
kb.press('n')
kb.release('n')
kb.press('n')
kb.release('n')
else:
print('could not type {}'.format(char))
# GOOGLE TRANSLATE
agent = {'User-Agent':
"Mozilla/4.0 (\
compatible;\
MSIE 6.0;\
Windows NT 5.1;\
SV1;\
.NET CLR 1.1.4322;\
.NET CLR 2.0.50727;\
.NET CLR 3.0.04506.30\
)"}
def translate(to_translate, to_language="auto", from_language="auto"):
# language is shortcut (es is spanish, fr is french, en is english)
base_link = "http://translate.google.com/m?hl=%s&sl=%s&q=%s"
to_translate = urllib.parse.quote_plus(to_translate)
link = base_link % (to_language, from_language, to_translate)
request = urllib.request.Request(link, headers=agent)
raw_data = urllib.request.urlopen(request).read()
data = raw_data.decode("utf-8")
expr = r'class="t0">(.*?)<'
re_result = re.findall(expr, data)
if (len(re_result) == 0):
result = ""
else:
result = html.unescape(re_result[0])
return (result)
# \\\\\\\\\\\\\\\\\\\\\\\\
# |||||||||main|||||||||||
# ////////////////////////
if __name__ == '__main__':
global going
global starter
global userInput
global kb
global newPhrase
going = False
starter = ""
userInput = ""
newPhrase = ""
# Collect events until released
kb = keyboard.Controller()
with keyboard.Listener(on_release=on_release) as listener:
listener.join()