Commit dead11c0 authored by Laurent Bachelier's avatar Laurent Bachelier 🐧 Committed by Vincent A

ebonics: Port to Browser2 and Python3

parent 7d3d73d4
# -*- coding: utf-8 -*-
# Copyright(C) 2012 Romain Bignon
# Copyright(C) 2012-2018 Romain Bignon, Laurent Bachelier
#
# This file is part of weboob.
#
......@@ -18,16 +18,29 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.translate import CapTranslate, Translation, TranslationFail, LanguageNotSupported
from weboob.browser import URL, PagesBrowser
from weboob.browser.filters.html import FormValue
from weboob.browser.pages import HTMLPage
from weboob.capabilities.translate import CapTranslate, LanguageNotSupported, Translation, TranslationFail
from weboob.tools.backend import Module
from weboob.tools.compat import urlencode
from weboob.deprecated.browser import StandardBrowser
__all__ = ['EbonicsModule']
class TranslatorPage(HTMLPage):
def get_text(self):
return FormValue('//textarea[@name="Ebonics"]')(self.doc)
class Ebonics(PagesBrowser):
translator = URL('http://joel.net/EBONICS/Translator', TranslatorPage)
def translate(self, text):
return self.open(self.translator.build(),
data={'English': text},
data_encoding='UTF-8').page.get_text()
class EbonicsModule(Module, CapTranslate):
NAME = 'ebonics'
MAINTAINER = u'Romain Bignon'
......@@ -35,26 +48,19 @@ class EbonicsModule(Module, CapTranslate):
VERSION = '1.4'
LICENSE = 'AGPLv3+'
DESCRIPTION = u'English to Ebonics translation service'
BROWSER = StandardBrowser
BROWSER = Ebonics
def translate(self, lan_from, lan_to, text):
if lan_from != 'English' or lan_to != 'Nigger!':
raise LanguageNotSupported()
with self.browser:
data = {'English': text.encode('utf-8')}
doc = self.browser.location('http://joel.net/EBONICS/Translator', urlencode(data))
try:
text = doc.getroot().cssselect('div.translateform div.bubble1 div.bubblemid')[0].text
except IndexError:
raise TranslationFail()
if text is None:
translated_text = self.browser.translate(text)
if not translated_text:
raise TranslationFail()
translation = Translation(0)
translation.lang_src = unicode(lan_from)
translation.lang_dst = unicode(lan_to)
translation.text = unicode(text).strip()
translation.lang_src = lan_from
translation.lang_dst = lan_to
translation.text = translated_text
return translation
......@@ -34,6 +34,7 @@ creditdunord/
cuisineaz/
delubac/
dlfp/
ebonics/
entreparticuliers/
erehsbc/
esalia/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment