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 BROWSER = PastealaconBrowser
def get_paste(self, _id): def get_paste(self, _id):
return PastealaconPaste(_id) with self.browser:
return self.browser.get_paste(_id)
def fill_paste(self, paste, fields): def fill_paste(self, paste, fields):
# if we only want the contents # if we only want the contents
if fields == ['contents']: if fields == ['contents']:
if paste.contents is NotLoaded: if paste.contents is NotLoaded:
with self.browser:
contents = self.browser.get_contents(paste.id) contents = self.browser.get_contents(paste.id)
paste.contents = contents paste.contents = contents
elif fields: elif fields:
with self.browser:
self.browser.fill_paste(paste) self.browser.fill_paste(paste)
return paste return paste
def post_paste(self, paste): def post_paste(self, paste):
with self.browser:
self.browser.post_paste(paste) self.browser.post_paste(paste)
OBJECTS = {PastealaconPaste: fill_paste} OBJECTS = {PastealaconPaste: fill_paste}

View file

@ -23,15 +23,18 @@ import re
from weboob.tools.browser import BaseBrowser, BrowserUnavailable, BrowserHTTPNotFound from weboob.tools.browser import BaseBrowser, BrowserUnavailable, BrowserHTTPNotFound
from weboob.capabilities.paste import PasteNotFound from weboob.capabilities.paste import PasteNotFound
from weboob.tools.browser.decorators import id2url
from .pages import PastePage, CaptchaPage, PostPage from .pages import PastePage, CaptchaPage, PostPage
from .paste import PastealaconPaste
__all__ = ['PastealaconBrowser'] __all__ = ['PastealaconBrowser']
class PastealaconBrowser(BaseBrowser): class PastealaconBrowser(BaseBrowser):
DOMAIN = 'pastealacon.com' DOMAIN = 'pastealacon.com'
ENCODING = 'ISO-8859-1' 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/%s' % (DOMAIN, re.escape('pastebin.php?captcha=1')): CaptchaPage,
'http://%s/' % DOMAIN: PostPage} 'http://%s/' % DOMAIN: PostPage}
@ -39,6 +42,11 @@ class PastealaconBrowser(BaseBrowser):
kwargs['factory'] = RobustFactory() kwargs['factory'] = RobustFactory()
BaseBrowser.__init__(self, *args, **kwargs) 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): def fill_paste(self, paste):
""" """
Get as much as information possible from the paste page 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) p = self.backend.get_paste(_id)
self.backend.fillobj(p, ['title']) self.backend.fillobj(p, ['title'])
assert p.title == 'ouiboube' 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 assert u'héhéhé' in p.contents
# raw method # raw method
p = self.backend.get_paste(_id) p = self.backend.get_paste(_id)
self.backend.fillobj(p, ['contents']) self.backend.fillobj(p, ['contents'])
assert p.title is NotLoaded 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 assert u'héhéhé' in p.contents
def test_post(self): def test_post(self):
@ -52,17 +52,22 @@ class PastealaconTest(BackendTest):
assert p.title == 'ouiboube' assert p.title == 'ouiboube'
assert p.id in p.page_url assert p.id in p.page_url
# test all get methods from the Paste we just created
self._get_paste(p.id) self._get_paste(p.id)
# same but from the full URL
self._get_paste('http://pastealacon.com/'+p.id)
def test_spam(self): def test_spam(self):
p = PastealaconPaste(None, title='viagra', contents='http://example.com/') p = PastealaconPaste(None, title='viagra', contents='http://example.com/')
self.assertRaises(BrowserUnavailable, self.backend.post_paste, p) self.assertRaises(BrowserUnavailable, self.backend.post_paste, p)
def test_notfound(self): def test_notfound(self):
for _id in ('424242424242424242424242424242424242', 'http://pastealacon.com/424242424242424242424242424242424242'):
# html method # html method
p = self.backend.get_paste('424242424242424242424242424242424242') p = self.backend.get_paste(_id)
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['title']) self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['title'])
# raw method # raw method
p = self.backend.get_paste('424242424242424242424242424242424242') p = self.backend.get_paste(_id)
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['contents']) self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['contents'])

View file

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

View file

@ -19,10 +19,12 @@
from weboob.tools.browser import BaseBrowser, BrowserHTTPNotFound from weboob.tools.browser import BaseBrowser, BrowserHTTPNotFound
from weboob.tools.browser.decorators import id2url
from weboob.capabilities.paste import PasteNotFound from weboob.capabilities.paste import PasteNotFound
from .pages import PastePage, PostPage from .pages import PastePage, PostPage
from .paste import PastebinPaste
import urllib import urllib
import re import re
@ -51,6 +53,11 @@ class PastebinBrowser(BaseBrowser):
except BrowserHTTPNotFound: except BrowserHTTPNotFound:
raise PasteNotFound() 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): def get_contents(self, _id):
""" """
Get the contents from the raw URL Get the contents from the raw URL

View file

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