forked from EpicJhon/Hexchat-Google-Translator-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_translator.py
351 lines (264 loc) · 11.2 KB
/
google_translator.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# -*- coding: utf-8 -*-
# ############################
#
# Imports
#
# ############################
from translate import Translator
import threading
import hexchat
import os
# ############################
#
# Module name
#
# ############################
__module_name__ = "google translator"
__module_version__ = "1.2"
__module_description__ = "Translates from one language to others using Google Translate."
__module_author__ = "EpicJhon"
# ############################
#
# Globals
#
# ############################
Bold = '\002'
Color = '\003'
Hidden = '\010'
Underline = '\037'
NormalText = '\017'
ReverseColor = '\026'
Beep = '\007'
Italics = '\035'
default_from = 'en'
default_to = 'es'
AUTOUSER = {}
AUTOCHANNEL = {}
# ############################
#
# Persistence
#
# ############################
def save_config(filename, dic):
config_dir = hexchat.get_info('configdir') + '/google_translator/'
if not os.path.exists(config_dir):
os.makedirs(config_dir)
f = open(config_dir + filename + '.db', 'w')
f.write(str(dic))
f.close()
def load_config(filename):
config_dir = hexchat.get_info('configdir') + '/google_translator/'
if not os.path.exists(config_dir):
os.makedirs(config_dir)
f = open(config_dir + filename + '.db', 'r')
dic = f.read()
f.close()
return eval(dic)
# ############################
#
# Translate function
#
# ############################
def translate(message, _from=default_from, to=default_to):
if _from == '':
_from = 'auto'
t = Translator()
return t.translate(message, from_lang=_from, to_lang=to)
# ############################
#
# Worker functions for thread
#
# ############################
# watchlist AUTOCHANNEL and AUTOUSER
def worker_hook_print_message(context, message, nick, _from=default_from, to=default_to):
# translate message
translation = translate(message, _from, to)
# delete above line and uncomment next for auto detect 'from' language
# translation = translate(message,'',to)
# chucked out nick_name for neat viewing
context.prnt(NormalText + Color + '08' + translation)
# "@@" and /TR
def worker_hook_tr(message, _from=default_from, to=default_to):
# translate message
translation = translate(message, _from, to)
# print(Bold + hexchat.get_info('nick') + NormalText + Color + '08 ' + translation)
print(NormalText + Color + '08' + translation)
# "!!" translate your message and send to server
def worker_hook_str(context, message, _from=default_from, to=default_to, user_mention=""):
# user mention
if message[:1] == '@' :
user_mention = message.split(' ',1)[0][1:] + ' '
message = message.split(' ',1)[1]
# translate message
final_text = user_mention + translate(message, to, _from)
# print and send translated message to server
context.command('SAY ' + final_text)
# ############################
#
# Hook functions
#
# ############################
def hook_add_channel(word, word_eol, userdata):
dest_lang = default_to
src_lang = default_from
if len(word) > 1:
channel = word[1]
else:
channel = hexchat.get_info('channel')
if len(word) > 2:
dest_lang = word[2]
if len(word) > 3:
src_lang = word[3]
AUTOCHANNEL[hexchat.get_info('network') + ' ' + channel.lower()] = (dest_lang, src_lang)
save_config('google_tr_auto_channel', str(AUTOCHANNEL))
hexchat.prnt("Added channel %s to the watch list." % channel)
return hexchat.EAT_ALL
def hook_remove_channel(word, word_eol, userdata):
if len(word) > 1:
channel = hexchat.strip(word[1])
else:
channel = hexchat.get_info('channel')
if AUTOCHANNEL.pop(hexchat.get_info('network') + ' ' + channel.lower(), None) is not None:
save_config('google_tr_auto_channel', str(AUTOCHANNEL))
hexchat.prnt('Channel ' + channel + ' has been removed from the watch list.')
return hexchat.EAT_ALL
def hook_add_user(word, word_eol, userdata):
if len(word) < 2:
hexchat.prnt("You must specify a user.")
return hexchat.EAT_ALL
user = word[1]
dest_lang = default_to
src_lang = ''
if len(word) > 2:
dest_lang = word[2]
if len(word) > 3:
src_lang = word[3]
AUTOUSER[hexchat.get_info('network') + ' ' + hexchat.get_info('channel') + ' ' + user.lower()] = (dest_lang, src_lang)
save_config('google_tr_auto_user', str(AUTOUSER))
hexchat.prnt("Added user %s to the watch list." % user)
return hexchat.EAT_ALL
def hook_remove_user(word, word_eol, userdata):
if len(word) < 2:
hexchat.prnt("You must specify a user.")
return hexchat.EAT_ALL
user = hexchat.strip(word[1])
if AUTOUSER.pop(hexchat.get_info('network') + ' ' + hexchat.get_info('channel') + ' ' + user.lower(), None) is not None:
save_config('google_tr_auto_user', str(AUTOUSER))
hexchat.prnt('User ' + user + ' has been removed from the watch list.')
return hexchat.EAT_ALL
def hook_print_user_list(word, word_eol, userdata):
hexchat.prnt("Auto translate users")
for key, value in AUTOUSER.items():
hexchat.prnt("\t" + key + ' => %s' % (value,))
return hexchat.EAT_ALL
def hook_print_channel_list(word, word_eol, userdata):
hexchat.prnt("Auto translate channels")
for key, value in AUTOCHANNEL.items():
hexchat.prnt("\t" + key + ' => %s' % (value,))
return hexchat.EAT_ALL
def hook_tr(word, word_eol, userdata):
_from = default_from
to = default_to
message = word_eol[1]
threading.Thread(target=worker_hook_tr, args=(message, _from, to)).start()
return hexchat.EAT_ALL
def hook_tra(word, word_eol, userdata):
_from = word[1]
to = word[2]
message = word_eol[3]
threading.Thread(target=worker_hook_tr, args=(message, _from, to)).start()
return hexchat.EAT_ALL
def hook_str(word, word_eol, userdata):
message = word_eol[1]
context = hexchat.get_context()
print(Bold + hexchat.get_info('nick') + NormalText + Color + '08> ' + message)
threading.Thread(target=worker_hook_str, args=(context, message,)).start()
return hexchat.EAT_ALL
## user input ##
def hook_say(word, word_eol, userdata):
message = hexchat.strip(word_eol[0])
channel = hexchat.get_info('channel')
context = hexchat.get_context()
key = hexchat.get_info('network') + ' ' + channel.lower()
# !! shortcut for sending message in other language
if key in AUTOCHANNEL and message[:2] == '!!':
print(NormalText + Color + '13 > ' + message[2:])
dest_lang, src_lang = AUTOCHANNEL[key]
threading.Thread(target=worker_hook_str, args=(context, message[2:], src_lang, dest_lang)).start()
return hexchat.EAT_ALL
# @@ shortcut for translating the message if channel or user not in the list
if message[:2] == '@@':
worker_hook_tr(message[2:])
return hexchat.EAT_ALL
return hexchat.EAT_NONE
# output from other users
def hook_print_message(word, word_eol, userdata):
nick = hexchat.strip(word[0])
message = hexchat.strip(word[1])
channel = hexchat.get_info('channel')
context = hexchat.get_context()
# AUTOCHANNEL Check
key = hexchat.get_info('network') + ' ' + channel.lower()
if key in AUTOCHANNEL:
dest_lang, src_lang = AUTOCHANNEL[key]
threading.Thread(target=worker_hook_print_message, args=(context, message, nick, src_lang, dest_lang)).start()
#worker_hook_tr(message,'de','en')
#worker_hook_print_message(context, message, nick, src_lang, dest_lang)
return hexchat.EAT_NONE
# AUTOUSER Check
key_user = hexchat.get_info('network') + ' ' + channel + ' ' + nick.lower()
if key in AUTOUSER:
dest_lang, src_lang = AUTOUSER[key_user]
worker_hook_tr(message, src_lang, dest_lang)
return hexchat.EAT_NONE
def hook_unload(userdata):
print('google Translator unloaded!')
# ############################
#
# Hook definitions
#
# ############################
# commands
hexchat.command('MENU ADD "$TAB/[+] AutoTranslate" "ADDTRC %s"')
help_message = '/ADDTRC <channel> <target_language> <source_language> - adds the channel to the watch list for automatic translations. If target_language is not specified, then the DEFAULT_LANG set will be used. If source_language is not specified, then language detection will be used. Starts your message with "!!" to auto translate.'
hexchat.hook_command('ADDTRC', hook_add_channel, help=help_message, priority=hexchat.PRI_HIGHEST)
hexchat.command('MENU ADD "$TAB/[-] AutoTranslate" "RMTRC %s"')
help_message = '/RMTRC <channel> - removes channel from the watch list for automatic translations.'
hexchat.hook_command('RMTRC', hook_remove_channel, help=help_message, priority=hexchat.PRI_HIGHEST)
hexchat.command('MENU ADD "$NICK/[+] AutoTranslate" "ADDTR %s"')
help_message = '/ADDTR <user_nick> <target_language> <source_language> - adds the user to the watch list for automatic translations. If target_language is not specified, then the DEFAULT_LANG set will be used. If source_language is not specified, then language detection will be used.'
hexchat.hook_command('ADDTR', hook_add_user, help=help_message, priority=hexchat.PRI_HIGHEST)
hexchat.command('MENU ADD "$NICK/[-] AutoTranslate" "RMTR %s"')
help_message = '/RMTR <user_nick> - removes user_nick from the watch list for automatic translations.'
hexchat.hook_command('RMTR', hook_remove_user, help=help_message, priority=hexchat.PRI_HIGHEST)
help_message = '/LSUSERS - prints out all users on the watch list for automatic translations to the screen locally.'
hexchat.hook_command('LSUSERS', hook_print_user_list, help=help_message, priority=hexchat.PRI_HIGHEST)
help_message = '/LSCHANNELS - prints out all channels on the watch list for automatic translations to the screen locally.'
hexchat.hook_command('LSCHANNELS', hook_print_channel_list, help=help_message, priority=hexchat.PRI_HIGHEST)
help_message = '/TR <message> - translates message into the language according to form "to-from". This auto detects the source language.'
hexchat.hook_command('TR', hook_tr, help=help_message, priority=hexchat.PRI_HIGHEST)
help_message = '/TRA <source language> <target language> <message> - translates message into the language specified. This auto detects the source language.'
hexchat.hook_command('TRA', hook_tra, help=help_message, priority=hexchat.PRI_HIGHEST)
help_message = '/STR <message> - sends a message translated according to form "to-from", where "from" is the default language of origin and "to" is the default language destination.'
hexchat.hook_command('STR', hook_str, help=help_message, priority=hexchat.PRI_HIGHEST)
hexchat.hook_command('', hook_say, priority=hexchat.PRI_HIGHEST)
# prints
hexchat.hook_print('Private Message to Dialog', hook_print_message, priority=hexchat.PRI_HIGHEST)
hexchat.hook_print("Channel Message", hook_print_message, priority=hexchat.PRI_HIGHEST)
hexchat.hook_print("Channel Msg Hilight", hook_print_message, priority=hexchat.PRI_HIGHEST)
# unload
hexchat.hook_unload(hook_unload)
# ############################
#
# Main
#
# ############################
print('Google Translator loaded!')
try:
AUTOUSER = load_config('google_tr_auto_user')
except:
pass
try:
AUTOCHANNEL = load_config('google_tr_auto_channel')
except:
pass