pastealacon backend: Support posting
The captcha is not handled but an exception is properly raised.
This commit is contained in:
parent
e5597424fc
commit
4dc3e56d18
4 changed files with 47 additions and 4 deletions
|
|
@ -51,4 +51,7 @@ class PastealaconBackend(BaseBackend, ICapPaste):
|
|||
self.browser.fill_paste(paste)
|
||||
return paste
|
||||
|
||||
def post_paste(self, paste):
|
||||
self.browser.post_paste(paste)
|
||||
|
||||
OBJECTS = {PastealaconPaste: fill_paste}
|
||||
|
|
|
|||
|
|
@ -17,17 +17,25 @@
|
|||
# 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 mechanize import RobustFactory
|
||||
import re
|
||||
|
||||
from weboob.tools.browser import BaseBrowser
|
||||
from weboob.tools.browser import BaseBrowser, BrowserUnavailable
|
||||
|
||||
from .pages import PastePage
|
||||
from .pages import PastePage, CaptchaPage, PostPage
|
||||
|
||||
__all__ = ['PastealaconBrowser']
|
||||
|
||||
class PastealaconBrowser(BaseBrowser):
|
||||
DOMAIN = 'pastealacon.com'
|
||||
ENCODING = 'ISO-8859-1'
|
||||
PAGES = {'http://%s/(?P<id>\d+)' % DOMAIN: PastePage}
|
||||
PAGES = {'http://%s/(?P<id>\d+)' % DOMAIN: PastePage,
|
||||
'http://%s/%s' % (DOMAIN, re.escape('pastebin.php?captcha=1')): CaptchaPage,
|
||||
'http://%s/' % DOMAIN: PostPage}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['factory'] = RobustFactory()
|
||||
BaseBrowser.__init__(self, *args, **kwargs)
|
||||
|
||||
def fill_paste(self, paste):
|
||||
"""
|
||||
|
|
@ -43,3 +51,11 @@ class PastealaconBrowser(BaseBrowser):
|
|||
Returns unicode.
|
||||
"""
|
||||
return self.readurl('http://%s/pastebin.php?dl=%s' % (self.DOMAIN, _id)).decode(self.ENCODING)
|
||||
|
||||
def post_paste(self, paste):
|
||||
self.home()
|
||||
self.page.post(paste)
|
||||
if self.is_on_page(CaptchaPage):
|
||||
raise BrowserUnavailable("Detected as spam and unable to handle the captcha")
|
||||
paste.id = self.page.get_id()
|
||||
self.fill_paste(paste)
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ from weboob.tools.browser import BasePage
|
|||
from urlparse import urlparse
|
||||
import re
|
||||
|
||||
__all__ = ['PastePage']
|
||||
__all__ = ['PastePage', 'PostPage', 'CaptchaPage']
|
||||
|
||||
class PastePage(BasePage):
|
||||
def fill_paste(self, paste):
|
||||
|
|
@ -40,3 +40,14 @@ class PastePage(BasePage):
|
|||
"""
|
||||
path = urlparse(self.url).path
|
||||
return path[1:]
|
||||
|
||||
class PostPage(BasePage):
|
||||
def post(self, paste):
|
||||
self.browser.select_form(name='editor')
|
||||
self.browser['code2'] = paste.contents.encode(self.browser.ENCODING)
|
||||
self.browser['poster'] = paste.title.encode(self.browser.ENCODING)
|
||||
self.browser['expiry'] = ['m']
|
||||
self.browser.submit()
|
||||
|
||||
class CaptchaPage(BasePage):
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
from weboob.tools.test import BackendTest
|
||||
from weboob.capabilities.base import NotLoaded
|
||||
from weboob.tools.browser import BrowserUnavailable
|
||||
from .paste import PastealaconPaste
|
||||
|
||||
class PastealaconTest(BackendTest):
|
||||
BACKEND = 'pastealacon'
|
||||
|
|
@ -37,3 +39,14 @@ class PastealaconTest(BackendTest):
|
|||
assert p.title is NotLoaded
|
||||
assert p.page_url == 'http://pastealacon.com/27184'
|
||||
assert u'coucou\r\ncoucou\r\nhéhéhé' == p.contents
|
||||
|
||||
def test_post(self):
|
||||
p = PastealaconPaste(None, title='ouiboube', contents='Weboob Test')
|
||||
self.backend.post_paste(p)
|
||||
assert p.id
|
||||
assert p.title == 'ouiboube'
|
||||
assert p.id in p.page_url
|
||||
|
||||
def test_spam(self):
|
||||
p = PastealaconPaste(None, title='viagra', contents='http://example.com/')
|
||||
self.assertRaises(BrowserUnavailable, self.backend.post_paste, p)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue