diff --git a/weboob/applications/pastoob/pastoob.py b/weboob/applications/pastoob/pastoob.py index 81c266d2..3b0b9c18 100644 --- a/weboob/applications/pastoob/pastoob.py +++ b/weboob/applications/pastoob/pastoob.py @@ -91,7 +91,7 @@ class Pastoob(ReplApplication): params = self._get_params() backends = {} for backend in self.weboob.iter_backends(): - score = backend.can_post(**params) + score = backend.can_post(contents, **params) if score: backends.setdefault(score, []).append(backend) # select a random backend from the best scores diff --git a/weboob/backends/pastealacon/backend.py b/weboob/backends/pastealacon/backend.py index 57a79ce0..a8d08549 100644 --- a/weboob/backends/pastealacon/backend.py +++ b/weboob/backends/pastealacon/backend.py @@ -49,7 +49,11 @@ class PastealaconBackend(BaseBackend, BasePasteBackend): def new_paste(self, *args, **kwargs): return PastealaconPaste(*args, **kwargs) - def can_post(self, public=None, max_age=None): + def can_post(self, contents, public=None, max_age=None): + try: + contents.encode(self.browser.ENCODING) + except UnicodeEncodeError: + return 0 if public is False: return 0 if max_age is not None: diff --git a/weboob/backends/pastealacon/test.py b/weboob/backends/pastealacon/test.py index cd6e190c..e49109ec 100644 --- a/weboob/backends/pastealacon/test.py +++ b/weboob/backends/pastealacon/test.py @@ -81,11 +81,13 @@ class PastealaconTest(BackendTest): assert self.backend.get_paste('nJG9ZFG8') is None def test_can_post(self): - assert 0 == self.backend.can_post(public=False) - assert 1 == self.backend.can_post(public=True) - assert 0 == self.backend.can_post(public=True, max_age=600) - assert 1 == self.backend.can_post(public=True, max_age=3600*24) - assert 1 == self.backend.can_post(public=True, max_age=3600*24*3) - assert 1 == self.backend.can_post(public=True, max_age=False) - assert 1 == self.backend.can_post(public=None, max_age=False) - assert 1 == self.backend.can_post(public=True, max_age=3600*24*40) + assert 0 == self.backend.can_post('hello', public=False) + assert 1 == self.backend.can_post('hello', public=True) + assert 0 == self.backend.can_post('hello', public=True, max_age=600) + assert 1 == self.backend.can_post('hello', public=True, max_age=3600*24) + assert 1 == self.backend.can_post('hello', public=True, max_age=3600*24*3) + assert 1 == self.backend.can_post('hello', public=True, max_age=False) + assert 1 == self.backend.can_post('hello', public=None, max_age=False) + assert 1 == self.backend.can_post('hello', public=True, max_age=3600*24*40) + assert 1 == self.backend.can_post(u'héhé', public=True) + assert 0 == self.backend.can_post(u'hello ♥', public=True) diff --git a/weboob/backends/pastebin/backend.py b/weboob/backends/pastebin/backend.py index ce71f4f3..2917a916 100644 --- a/weboob/backends/pastebin/backend.py +++ b/weboob/backends/pastebin/backend.py @@ -63,7 +63,7 @@ class PastebinBackend(BaseBackend, BasePasteBackend): def new_paste(self, *args, **kwargs): return PastebinPaste(*args, **kwargs) - def can_post(self, public=None, max_age=None): + def can_post(self, contents, public=None, max_age=None): if max_age is not None: if self.get_closest_expiration(max_age) is None: return 0 diff --git a/weboob/backends/pastebin/test.py b/weboob/backends/pastebin/test.py index 365fddf2..484d6969 100644 --- a/weboob/backends/pastebin/test.py +++ b/weboob/backends/pastebin/test.py @@ -79,12 +79,14 @@ class PastebinTest(BackendTest): assert self.backend.get_paste('http://pastealacon.com/1') is None def test_can_post(self): - assert self.backend.can_post(public=None) > 0 - assert self.backend.can_post(public=True) > 0 - assert self.backend.can_post(public=False) > 0 - assert self.backend.can_post(public=True, max_age=600) > 0 - assert self.backend.can_post(public=True, max_age=3600*24) > 0 - assert self.backend.can_post(public=True, max_age=3600*24*3) > 0 - assert self.backend.can_post(public=True, max_age=False) > 0 - assert self.backend.can_post(public=None, max_age=False) > 0 - assert self.backend.can_post(public=True, max_age=3600*24*40) > 0 + assert self.backend.can_post('hello', public=None) > 0 + assert self.backend.can_post('hello', public=True) > 0 + assert self.backend.can_post('hello', public=False) > 0 + assert self.backend.can_post('hello', public=True, max_age=600) > 0 + assert self.backend.can_post('hello', public=True, max_age=3600*24) > 0 + assert self.backend.can_post('hello', public=True, max_age=3600*24*3) > 0 + assert self.backend.can_post('hello', public=True, max_age=False) > 0 + assert self.backend.can_post('hello', public=None, max_age=False) > 0 + assert self.backend.can_post('hello', public=True, max_age=3600*24*40) > 0 + assert self.backend.can_post(u'héhé', public=True) > 0 + assert self.backend.can_post(u'hello ♥', public=True) > 0 diff --git a/weboob/capabilities/paste.py b/weboob/capabilities/paste.py index 2ce31f12..e3aba399 100644 --- a/weboob/capabilities/paste.py +++ b/weboob/capabilities/paste.py @@ -64,13 +64,15 @@ class ICapPaste(IBaseCap): """ raise NotImplementedError() - def can_post(self, public=None, max_age=None): + def can_post(self, contents, public=None, max_age=None): """ Checks if the paste can be pasted by this backend. Some properties are considered required (public/private, max_age) while others are just bonuses (language). - public: True must be public, False must be private, None do not care + contents: Can be used to check encodability, maximum length, etc. + public: True must be public, False must be private, None do not care. + max_age: Maximum time to live in seconds. A score of 0 means the backend is not suitable. A score of 1 means the backend is suitable.