CapPaste: Support expiration for posting, as a max_age parameter

This commit is contained in:
Laurent Bachelier 2011-04-30 00:26:23 +02:00
commit f63180c187
10 changed files with 71 additions and 27 deletions

View file

@ -102,8 +102,8 @@ class Pastoob(ReplApplication):
p.public = params.get('public')
p.title = os.path.basename(filename)
p.contents = contents
backend.post_paste(p)
backend.post_paste(p, max_age=params.get('max_age'))
print 'Successfuly posted paste: %s' % p.page_url
def _get_params(self):
return {'public': True}
return {'public': True, 'max_age': 3600*24*3}

View file

@ -20,7 +20,7 @@
from __future__ import with_statement
from weboob.capabilities.paste import ICapPaste
from weboob.tools.capabilities.paste import BasePasteBackend
from weboob.tools.backend import BaseBackend
from weboob.capabilities.base import NotLoaded
@ -31,7 +31,7 @@ from .paste import PastealaconPaste
__all__ = ['PastealaconBackend']
class PastealaconBackend(BaseBackend, ICapPaste):
class PastealaconBackend(BaseBackend, BasePasteBackend):
NAME = 'pastealacon'
MAINTAINER = 'Laurent Bachelier'
EMAIL = 'laurent@bachelier.name'
@ -40,12 +40,21 @@ class PastealaconBackend(BaseBackend, ICapPaste):
LICENSE = 'AGPLv3+'
BROWSER = PastealaconBrowser
EXPIRATIONS = {
24*3600: 'd',
24*3600*30: 'm',
False: 'f',
}
def new_paste(self, *args, **kwargs):
return PastealaconPaste(*args, **kwargs)
def can_post(self, public=None):
def can_post(self, public=None, max_age=None):
if public is False:
return 0
if max_age is not None:
if self.get_closest_expiration(max_age) is None:
return 0
return 1
def get_paste(self, _id):
@ -65,8 +74,12 @@ class PastealaconBackend(BaseBackend, ICapPaste):
self.browser.fill_paste(paste)
return paste
def post_paste(self, paste):
def post_paste(self, paste, max_age = None):
if max_age is not None:
expiration = self.get_closest_expiration(max_age)
else:
expiration = None
with self.browser:
self.browser.post_paste(paste)
self.browser.post_paste(paste, expiration=self.EXPIRATIONS.get(expiration))
OBJECTS = {PastealaconPaste: fill_paste}

View file

@ -66,9 +66,9 @@ class PastealaconBrowser(BaseBrowser):
except BrowserHTTPNotFound:
raise PasteNotFound()
def post_paste(self, paste):
def post_paste(self, paste, expiration=None):
self.home()
self.page.post(paste)
self.page.post(paste, expiration=expiration)
if self.is_on_page(CaptchaPage):
raise BrowserUnavailable("Detected as spam and unable to handle the captcha")
paste.id = self.page.get_id()

View file

@ -47,11 +47,12 @@ class PastePage(BasePage):
return self.group_dict['id']
class PostPage(BasePage):
def post(self, paste):
def post(self, paste, expiration=None):
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']
if expiration:
self.browser['expiry'] = [expiration]
self.browser.submit()
class CaptchaPage(BasePage):

View file

@ -46,7 +46,7 @@ class PastealaconTest(BackendTest):
def test_post(self):
p = self.backend.new_paste(None, title='ouiboube', contents=u'Weboob Test héhéhé')
self.backend.post_paste(p)
self.backend.post_paste(p, max_age=3600*24)
assert p.id
self.backend.fill_paste(p, ['title'])
assert p.title == 'ouiboube'
@ -83,3 +83,9 @@ class PastealaconTest(BackendTest):
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)

View file

@ -20,7 +20,7 @@
from __future__ import with_statement
from weboob.capabilities.paste import ICapPaste
from weboob.tools.capabilities.paste import BasePasteBackend
from weboob.tools.backend import BaseBackend
from weboob.capabilities.base import NotLoaded
from weboob.tools.value import Value, ValuesDict
@ -32,7 +32,7 @@ from .paste import PastebinPaste
__all__ = ['PastebinBackend']
class PastebinBackend(BaseBackend, ICapPaste):
class PastebinBackend(BaseBackend, BasePasteBackend):
NAME = 'pastebin'
MAINTAINER = 'Laurent Bachelier'
EMAIL = 'laurent@bachelier.name'
@ -44,10 +44,21 @@ class PastebinBackend(BaseBackend, ICapPaste):
Value('apikey', label='Optional API key', default='', masked=True),
)
EXPIRATIONS = {
600: '10M',
3600: '1H',
3600*24: '1D',
3600*24*30: '1M',
False: 'N',
}
def new_paste(self, *args, **kwargs):
return PastebinPaste(*args, **kwargs)
def can_post(self, public=None):
def can_post(self, public=None, max_age=None):
if max_age is not None:
if self.get_closest_expiration(max_age) is None:
return 0
return 1
def get_paste(self, _id):
@ -67,11 +78,15 @@ class PastebinBackend(BaseBackend, ICapPaste):
self.browser.fill_paste(paste)
return paste
def post_paste(self, paste):
def post_paste(self, paste, max_age=None):
if max_age is not None:
expiration = self.get_closest_expiration(max_age)
else:
expiration = None
with self.browser:
if self.config['apikey']:
self.browser.api_post_paste(self.config['apikey'], paste)
self.browser.api_post_paste(self.config['apikey'], paste, expiration=self.EXPIRATIONS.get(expiration))
else:
self.browser.post_paste(paste)
self.browser.post_paste(paste, expiration=self.EXPIRATIONS.get(expiration))
OBJECTS = {PastebinPaste: fill_paste}

View file

@ -70,12 +70,12 @@ class PastebinBrowser(BaseBrowser):
except BrowserHTTPNotFound:
raise PasteNotFound()
def post_paste(self, paste):
def post_paste(self, paste, expiration=None):
self.home()
self.page.post(paste)
self.page.post(paste, expiration=expiration)
paste.id = self.page.get_id()
def api_post_paste(self, dev_key, paste):
def api_post_paste(self, dev_key, paste, expiration=None):
data = {'api_dev_key': dev_key,
'api_option': 'paste',
'api_paste_expire_date': '1M',
@ -84,6 +84,8 @@ class PastebinBrowser(BaseBrowser):
}
if paste.title:
data['api_paste_name'] = paste.title.encode(self.ENCODING)
if expiration:
data['api_paste_expire_date'] = expiration
res = self.readurl(self.API_URL, urllib.urlencode(data)).decode(self.ENCODING)
self._validate_api_response(res)
paste.id = re.match('^%s$' % self.PASTE_URL, res).groupdict()['id']

View file

@ -48,10 +48,11 @@ class PastePage(BasePage):
class PostPage(BasePage):
def post(self, paste):
def post(self, paste, expiration=None):
self.browser.select_form(name='myform')
self.browser['paste_code'] = paste.contents.encode(self.browser.ENCODING)
self.browser['paste_name'] = paste.title.encode(self.browser.ENCODING)
self.browser['paste_private'] = ['0' if paste.public else '1']
self.browser['paste_expire_date'] = ['1M']
if expiration:
self.browser['paste_expire_date'] = [expiration]
self.browser.submit()

View file

@ -44,7 +44,7 @@ class PastebinTest(BackendTest):
def test_post(self):
p = self.backend.new_paste(None, title='ouiboube', contents='Weboob Test', public=True)
self.backend.post_paste(p)
self.backend.post_paste(p, max_age=600)
assert p.id
self.backend.fill_paste(p, ['title'])
assert p.title == 'ouiboube'
@ -54,7 +54,7 @@ class PastebinTest(BackendTest):
def test_specialchars(self):
# post a paste and get the contents through the HTML response
p1 = self.backend.new_paste(None, title='ouiboube', contents=u'Weboob <test>¿¡', public=False)
self.backend.post_paste(p1)
self.backend.post_paste(p1, max_age=600)
assert p1.id
assert p1.public is False
@ -82,3 +82,9 @@ class PastebinTest(BackendTest):
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

View file

@ -64,7 +64,7 @@ class ICapPaste(IBaseCap):
"""
raise NotImplementedError()
def can_post(self, public=None):
def can_post(self, 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
@ -89,7 +89,7 @@ class ICapPaste(IBaseCap):
"""
raise NotImplementedError()
def post_paste(self, _id):
def post_paste(self, paste, max_age=None):
"""
Post a paste.