paste* backends: Accept an URL or an ID

Like most other backends, with the id2url decorator.
This commit is contained in:
Laurent Bachelier 2011-04-21 18:29:18 +02:00
commit dc35df83ef
6 changed files with 69 additions and 39 deletions

View file

@ -39,19 +39,23 @@ class PastealaconBackend(BaseBackend, ICapPaste):
BROWSER = PastealaconBrowser
def get_paste(self, _id):
return PastealaconPaste(_id)
with self.browser:
return self.browser.get_paste(_id)
def fill_paste(self, paste, fields):
# if we only want the contents
if fields == ['contents']:
if paste.contents is NotLoaded:
contents = self.browser.get_contents(paste.id)
paste.contents = contents
with self.browser:
contents = self.browser.get_contents(paste.id)
paste.contents = contents
elif fields:
self.browser.fill_paste(paste)
with self.browser:
self.browser.fill_paste(paste)
return paste
def post_paste(self, paste):
self.browser.post_paste(paste)
with self.browser:
self.browser.post_paste(paste)
OBJECTS = {PastealaconPaste: fill_paste}

View file

@ -23,15 +23,18 @@ import re
from weboob.tools.browser import BaseBrowser, BrowserUnavailable, BrowserHTTPNotFound
from weboob.capabilities.paste import PasteNotFound
from weboob.tools.browser.decorators import id2url
from .pages import PastePage, CaptchaPage, PostPage
from .paste import PastealaconPaste
__all__ = ['PastealaconBrowser']
class PastealaconBrowser(BaseBrowser):
DOMAIN = 'pastealacon.com'
ENCODING = 'ISO-8859-1'
PAGES = {'http://%s/(?P<id>\d+)' % DOMAIN: PastePage,
PASTE_URL = 'http://%s/(?P<id>\d+)' % DOMAIN
PAGES = {PASTE_URL: PastePage,
'http://%s/%s' % (DOMAIN, re.escape('pastebin.php?captcha=1')): CaptchaPage,
'http://%s/' % DOMAIN: PostPage}
@ -39,6 +42,11 @@ class PastealaconBrowser(BaseBrowser):
kwargs['factory'] = RobustFactory()
BaseBrowser.__init__(self, *args, **kwargs)
@id2url(PastealaconPaste.id2url)
def get_paste(self, url):
_id = re.match(self.PASTE_URL, url).groupdict()['id']
return PastealaconPaste(_id)
def fill_paste(self, paste):
"""
Get as much as information possible from the paste page

View file

@ -34,14 +34,14 @@ class PastealaconTest(BackendTest):
p = self.backend.get_paste(_id)
self.backend.fillobj(p, ['title'])
assert p.title == 'ouiboube'
assert p.page_url == 'http://pastealacon.com/'+_id
assert p.page_url.startswith('http://pastealacon.com/')
assert u'héhéhé' in p.contents
# raw method
p = self.backend.get_paste(_id)
self.backend.fillobj(p, ['contents'])
assert p.title is NotLoaded
assert p.page_url == 'http://pastealacon.com/'+_id
assert p.page_url.startswith('http://pastealacon.com/')
assert u'héhéhé' in p.contents
def test_post(self):
@ -52,17 +52,22 @@ class PastealaconTest(BackendTest):
assert p.title == 'ouiboube'
assert p.id in p.page_url
# test all get methods from the Paste we just created
self._get_paste(p.id)
# same but from the full URL
self._get_paste('http://pastealacon.com/'+p.id)
def test_spam(self):
p = PastealaconPaste(None, title='viagra', contents='http://example.com/')
self.assertRaises(BrowserUnavailable, self.backend.post_paste, p)
def test_notfound(self):
# html method
p = self.backend.get_paste('424242424242424242424242424242424242')
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['title'])
for _id in ('424242424242424242424242424242424242', 'http://pastealacon.com/424242424242424242424242424242424242'):
# html method
p = self.backend.get_paste(_id)
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['title'])
# raw method
p = self.backend.get_paste('424242424242424242424242424242424242')
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['contents'])
# raw method
p = self.backend.get_paste(_id)
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['contents'])

View file

@ -43,22 +43,26 @@ class PastebinBackend(BaseBackend, ICapPaste):
)
def get_paste(self, _id):
return PastebinPaste(_id)
with self.browser:
return self.browser.get_paste(_id)
def fill_paste(self, paste, fields):
# if we only want the contents
if fields == ['contents']:
if paste.contents is NotLoaded:
contents = self.browser.get_contents(paste.id)
with self.browser:
contents = self.browser.get_contents(paste.id)
paste.contents = contents
elif fields:
self.browser.fill_paste(paste)
with self.browser:
self.browser.fill_paste(paste)
return paste
def post_paste(self, paste):
if self.config['apikey']:
self.browser.api_post_paste(self.config['apikey'], paste)
else:
self.browser.post_paste(paste)
with self.browser:
if self.config['apikey']:
self.browser.api_post_paste(self.config['apikey'], paste)
else:
self.browser.post_paste(paste)
OBJECTS = {PastebinPaste: fill_paste}

View file

@ -19,10 +19,12 @@
from weboob.tools.browser import BaseBrowser, BrowserHTTPNotFound
from weboob.tools.browser.decorators import id2url
from weboob.capabilities.paste import PasteNotFound
from .pages import PastePage, PostPage
from .paste import PastebinPaste
import urllib
import re
@ -51,6 +53,11 @@ class PastebinBrowser(BaseBrowser):
except BrowserHTTPNotFound:
raise PasteNotFound()
@id2url(PastebinPaste.id2url)
def get_paste(self, url):
_id = re.match(self.PASTE_URL, url).groupdict()['id']
return PastebinPaste(_id)
def get_contents(self, _id):
"""
Get the contents from the raw URL

View file

@ -26,19 +26,20 @@ class PastebinTest(BackendTest):
BACKEND = 'pastebin'
def test_get_paste(self):
# html method
p = self.backend.get_paste('7HmXwzyt')
self.backend.fillobj(p, ['title'])
assert p.title == 'plop'
assert p.page_url == 'http://pastebin.com/7HmXwzyt'
assert p.contents == 'prout'
for _id in ('7HmXwzyt', 'http://pastebin.com/7HmXwzyt'):
# html method
p = self.backend.get_paste(_id)
self.backend.fillobj(p, ['title'])
assert p.title == 'plop'
assert p.page_url == 'http://pastebin.com/7HmXwzyt'
assert p.contents == 'prout'
# raw method
p = self.backend.get_paste('7HmXwzyt')
self.backend.fillobj(p, ['contents'])
assert p.title is NotLoaded
assert p.page_url == 'http://pastebin.com/7HmXwzyt'
assert p.contents == 'prout'
# raw method
p = self.backend.get_paste(_id)
self.backend.fillobj(p, ['contents'])
assert p.title is NotLoaded
assert p.page_url == 'http://pastebin.com/7HmXwzyt'
assert p.contents == 'prout'
def test_post(self):
p = PastebinPaste(None, title='ouiboube', contents='Weboob Test')
@ -60,10 +61,11 @@ class PastebinTest(BackendTest):
assert p2.contents == p1.contents
def test_notfound(self):
# html method
p = self.backend.get_paste('weboooooooooooooooooooooooooob')
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['title'])
for _id in ('weboooooooooooooooooooooooooob', 'http://pastebin.com/weboooooooooooooooooooooooooob'):
# html method
p = self.backend.get_paste(_id)
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['title'])
# raw method
p = self.backend.get_paste('weboooooooooooooooooooooooooob')
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['contents'])
# raw method
p = self.backend.get_paste(_id)
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['contents'])