-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmultitran_api.py
More file actions
40 lines (34 loc) · 1.26 KB
/
Copy pathmultitran_api.py
File metadata and controls
40 lines (34 loc) · 1.26 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
import urllib
from lxml import etree
from twisted.web.client import getPage
from twisted.internet.defer import Deferred, inlineCallbacks, returnValue
class MultitranAPI(object):
langs = {'en': ('English', 1),
'de': ('German', 3),
'fr': ('French', 4),
'es': ('Spain', 5),
'it': ('Italian', 23),
'nl': ('Dutch', 24),
'lv': ('Latvian', 27),
'et': ('Estonian', 26),
'ja': ('Japanese', 28),
'af': ('African', 31),
'eo': ('Esperanto', 34),
'xal': ('Kalmyk', 35)
}
def get_languages(self):
return dict(map(lambda x: (x[0], x[1][0]), self.langs.items()))
@inlineCallbacks
def translate(self, word, lang):
page = 'http://www.multitran.ru/c/m.exe?CL=1&s=%s&l1=%d'%(word, lang)
page = yield getPage(page)
translation = ''
html = etree.HTML(page)
trs = html.xpath('//form[@id="translation"]/../table[2]/tr')
for tr in trs:
tds = tr.xpath('td')
for td in tds:
for elem in td.xpath('descendant::text()'):
translation += '%s' % elem.rstrip('\r\n')
translation += '\n'
returnValue(translation)