pastebin backend: Support posting via API if we have an apikey
This commit is contained in:
parent
667b8267db
commit
ae52f23ea8
2 changed files with 34 additions and 2 deletions
|
|
@ -21,6 +21,7 @@
|
||||||
from weboob.capabilities.paste import ICapPaste
|
from weboob.capabilities.paste import ICapPaste
|
||||||
from weboob.tools.backend import BaseBackend
|
from weboob.tools.backend import BaseBackend
|
||||||
from weboob.capabilities.base import NotLoaded
|
from weboob.capabilities.base import NotLoaded
|
||||||
|
from weboob.tools.value import Value, ValuesDict
|
||||||
|
|
||||||
from .browser import PastebinBrowser
|
from .browser import PastebinBrowser
|
||||||
from .paste import PastebinPaste
|
from .paste import PastebinPaste
|
||||||
|
|
@ -37,6 +38,9 @@ class PastebinBackend(BaseBackend, ICapPaste):
|
||||||
DESCRIPTION = 'Pastebin paste tool'
|
DESCRIPTION = 'Pastebin paste tool'
|
||||||
LICENSE = 'AGPLv3+'
|
LICENSE = 'AGPLv3+'
|
||||||
BROWSER = PastebinBrowser
|
BROWSER = PastebinBrowser
|
||||||
|
CONFIG = ValuesDict(
|
||||||
|
Value('apikey', label='Optional API key', default='', masked=True),
|
||||||
|
)
|
||||||
|
|
||||||
def get_paste(self, _id):
|
def get_paste(self, _id):
|
||||||
return PastebinPaste(_id)
|
return PastebinPaste(_id)
|
||||||
|
|
@ -52,6 +56,9 @@ class PastebinBackend(BaseBackend, ICapPaste):
|
||||||
return paste
|
return paste
|
||||||
|
|
||||||
def post_paste(self, paste):
|
def post_paste(self, paste):
|
||||||
self.browser.post_paste(paste)
|
if self.config['apikey']:
|
||||||
|
self.browser.api_post_paste(self.config['apikey'], paste)
|
||||||
|
else:
|
||||||
|
self.browser.post_paste(paste)
|
||||||
|
|
||||||
OBJECTS = {PastebinPaste: fill_paste}
|
OBJECTS = {PastebinPaste: fill_paste}
|
||||||
|
|
|
||||||
|
|
@ -22,12 +22,21 @@ from weboob.tools.browser import BaseBrowser
|
||||||
|
|
||||||
from .pages import PastePage, PostPage
|
from .pages import PastePage, PostPage
|
||||||
|
|
||||||
|
import urllib
|
||||||
|
import re
|
||||||
|
|
||||||
__all__ = ['PastebinBrowser']
|
__all__ = ['PastebinBrowser']
|
||||||
|
|
||||||
|
class BadAPIRequest(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class PastebinBrowser(BaseBrowser):
|
class PastebinBrowser(BaseBrowser):
|
||||||
DOMAIN = 'pastebin.com'
|
DOMAIN = 'pastebin.com'
|
||||||
ENCODING = 'UTF-8'
|
ENCODING = 'UTF-8'
|
||||||
PAGES = {'http://%s/(?P<id>.+)' % DOMAIN: PastePage,
|
PASTE_URL = 'http://%s/(?P<id>.+)' % DOMAIN
|
||||||
|
API_URL = 'http://%s/api/api_post.php' % DOMAIN
|
||||||
|
PAGES = {PASTE_URL: PastePage,
|
||||||
'http://%s/' % DOMAIN: PostPage}
|
'http://%s/' % DOMAIN: PostPage}
|
||||||
|
|
||||||
def fill_paste(self, paste):
|
def fill_paste(self, paste):
|
||||||
|
|
@ -49,3 +58,19 @@ class PastebinBrowser(BaseBrowser):
|
||||||
self.home()
|
self.home()
|
||||||
self.page.post(paste)
|
self.page.post(paste)
|
||||||
paste.id = self.page.get_id()
|
paste.id = self.page.get_id()
|
||||||
|
|
||||||
|
def api_post_paste(self, dev_key, paste):
|
||||||
|
data = {'api_dev_key': dev_key,
|
||||||
|
'api_option': 'paste',
|
||||||
|
'api_paste_code': paste.contents.encode(self.ENCODING),
|
||||||
|
}
|
||||||
|
if paste.title:
|
||||||
|
data['api_paste_name'] = paste.title.encode(self.ENCODING)
|
||||||
|
res = self.readurl(self.API_URL, urllib.urlencode(data)).decode(self.ENCODING)
|
||||||
|
self._validate_api_response(res)
|
||||||
|
paste.id = re.match(self.PASTE_URL, res).groupdict()['id']
|
||||||
|
|
||||||
|
def _validate_api_response(self, res):
|
||||||
|
matches = re.match('Bad API request, (?P<error>.+)', res)
|
||||||
|
if matches:
|
||||||
|
raise BadAPIRequest(matches.groupdict().get('error'))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue