CapPaste: Support expiration for posting, as a max_age parameter
This commit is contained in:
parent
a9220c96af
commit
f63180c187
10 changed files with 71 additions and 27 deletions
|
|
@ -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}
|
||||
|
|
|
|||
|
|
@ -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']
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue