-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtranslate.py
More file actions
59 lines (41 loc) · 1.78 KB
/
translate.py
File metadata and controls
59 lines (41 loc) · 1.78 KB
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
import urllib.request
import urllib.parse
import urllib.error
import plugin
class Translator(object):
def __init__(self, bing_app_id):
self.bing_app_id = bing_app_id
def translate(self, source, target, phrase):
"""
Translates phrase from source to target with Bing's translation API.
"""
query = urllib.parse.quote(phrase.replace('"', '').replace('\\', ''))
page = urllib.request.urlopen(
'http://api.microsofttranslator.com/V2/Ajax.svc/'
'Translate?appId=%s&from=%s&to=%s&text="%s"' % (self.bing_app_id,
source, target,
query))
result = page.read().decode('utf-8')
result = result.replace('"', '').replace('\\', '')
if 'ArgumentOutOfRangeException:' in result:
raise NameError("that's not a language, silly")
print(' -- %s' % result)
return result
class Plugin(plugin.Plugin):
def prepare(self):
self.translator = Translator(self.conf['bing_app_id'])
def register_commands(self):
self.commands = [
('translate <from> <to> <<phrase>>', self.translate),
]
def translate(self, message, args):
"""
Translates `<phrase>` from `<from>` to `<to>` using the Bing translate
API. `<from>` and `<to>` must be any of the language codes listed
[here](http://msdn.microsoft.com/en-us/library/hh456380.aspx).
$<comchar>translate fr en le jambon est mort
>the ham is dead
"""
translation = self.translator.translate(args['from'], args['to'],
args['phrase'])
self.irc.privmsg(message.source, translation)