several fixes in the new ICapTranslate capability, module and application
This commit is contained in:
parent
4b05f77329
commit
a8166a4719
5 changed files with 96 additions and 78 deletions
|
|
@ -18,21 +18,32 @@
|
|||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||
"backend for http://translate.google.com"
|
||||
|
||||
from weboob.capabilities.translate import ICapTranslate
|
||||
from weboob.tools.backend import BaseBackend, BackendConfig
|
||||
|
||||
from weboob.capabilities.translate import ICapTranslate, Translation, TranslationFail
|
||||
from weboob.tools.backend import BaseBackend
|
||||
|
||||
from .browser import GoogleTranslateBrowser
|
||||
|
||||
|
||||
__all__ = ['GoogleTranslateBackend']
|
||||
|
||||
|
||||
class GoogleTranslateBackend(BaseBackend, ICapTranslate):
|
||||
MAINTAINER = 'Lucien Loiseau'
|
||||
EMAIL = 'loiseau.lucien@gmail.com'
|
||||
VERSION = '0.c'
|
||||
LICENSE = 'AGPLv3+'
|
||||
STORAGE = {'seen': {}}
|
||||
NAME = 'googletranslate'
|
||||
DESCRIPTION = u'Google translation web service'
|
||||
BROWSER = GoogleTranslateBrowser
|
||||
|
||||
def translate(self, lan_from, lan_to, text):
|
||||
return self.browser.translate(lan_from, lan_to, text)
|
||||
translation = Translation(0)
|
||||
translation.lang_src = lan_from
|
||||
translation.lang_dst = lan_to
|
||||
translation.text = self.browser.translate(lan_from, lan_to, text)
|
||||
|
||||
if translation.text is None:
|
||||
raise TranslationFail()
|
||||
|
||||
return translation
|
||||
|
|
|
|||
|
|
@ -18,16 +18,16 @@
|
|||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from weboob.tools.browser import BaseBrowser, BrowserHTTPNotFound, BrowserIncorrectPassword
|
||||
from weboob.tools.browser.decorators import id2url, check_url
|
||||
from weboob.capabilities.translate import TranslationFail
|
||||
import urllib
|
||||
|
||||
from weboob.tools.browser import BaseBrowser
|
||||
|
||||
from .pages import TranslatePage
|
||||
|
||||
import urllib
|
||||
|
||||
__all__ = ['GoogleTranslateBrowser']
|
||||
|
||||
|
||||
class GoogleTranslateBrowser(BaseBrowser):
|
||||
DOMAIN = 'translate.google.com'
|
||||
ENCODING = 'UTF-8'
|
||||
|
|
@ -43,22 +43,17 @@ class GoogleTranslateBrowser(BaseBrowser):
|
|||
"""
|
||||
translate 'text' from 'source' language to 'to' language
|
||||
"""
|
||||
try:
|
||||
d = {
|
||||
'sl': source,
|
||||
'tl': to,
|
||||
'js': 'n',
|
||||
'prev': '_t',
|
||||
'hl': 'en',
|
||||
'ie': 'UTF-8',
|
||||
'layout': '2',
|
||||
'eotf': '1',
|
||||
'text': text,
|
||||
}
|
||||
self.location('http://'+self.DOMAIN, urllib.urlencode(d))
|
||||
translation = self.page.get_translation()
|
||||
return translation
|
||||
|
||||
except TranslationFail:
|
||||
return "no translation available"
|
||||
|
||||
d = {
|
||||
'sl': source.encode('utf-8'),
|
||||
'tl': to.encode('utf-8'),
|
||||
'js': 'n',
|
||||
'prev': '_t',
|
||||
'hl': 'en',
|
||||
'ie': 'UTF-8',
|
||||
'layout': '2',
|
||||
'eotf': '1',
|
||||
'text': text.encode('utf-8'),
|
||||
}
|
||||
self.location('http://'+self.DOMAIN, urllib.urlencode(d))
|
||||
translation = self.page.get_translation()
|
||||
return translation
|
||||
|
|
|
|||
|
|
@ -17,15 +17,17 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from datetime import date
|
||||
|
||||
from weboob.tools.browser import BasePage
|
||||
|
||||
|
||||
__all__ = ['TranslatePage']
|
||||
|
||||
|
||||
class TranslatePage(BasePage):
|
||||
def get_translation(self):
|
||||
for span in self.document.getiterator('span'):
|
||||
if (span.attrib.get('id', '') == 'result_box'):
|
||||
for children in span.getchildren():
|
||||
return children.text
|
||||
|
||||
boxes = self.parser.select(self.document.getroot(), 'span#result_box', 1).findall('span')
|
||||
if len(boxes) == 0:
|
||||
return None
|
||||
|
||||
return u'\n'.join([unicode(box.text) for box in boxes])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue