Add Pastebin backend and Paste capability

It is able to get a paste, and post a new one.
Only the contents and title fields are supported for now.
This commit is contained in:
Laurent Bachelier 2011-04-09 19:45:49 +02:00 committed by Romain Bignon
commit 8b874f6b0a
7 changed files with 308 additions and 0 deletions

View file

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Laurent Bachelier
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from .backend import PastebinBackend
__all__ = ['PastebinBackend']

View file

@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Laurent Bachelier
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.paste import ICapPaste
from weboob.tools.backend import BaseBackend
from .browser import PastebinBrowser
from .paste import PastebinPaste
__all__ = ['PastebinBackend']
class PastebinBackend(BaseBackend, ICapPaste):
NAME = 'pastebin'
MAINTAINER = 'Laurent Bachelier'
EMAIL = 'laurent@bachelier.name'
VERSION = '0.8'
DESCRIPTION = 'Pastebin paste tool'
LICENSE = 'AGPLv3+'
BROWSER = PastebinBrowser
def get_paste(self, _id):
paste = PastebinPaste(_id)
self.browser.fill_paste(paste)
return paste
def fill_paste(self, paste, fields):
self.browser.fill_paste(paste)
if 'contents' in fields:
contents = self.browser.get_contents(paste.id)
paste.contents = contents
return paste
def post_paste(self, paste):
self.browser.post_paste(paste)
OBJECTS = {PastebinPaste: fill_paste}

View file

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Laurent Bachelier
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BaseBrowser
from .pages import PastePage, PostPage
from .paste import PastebinPaste
__all__ = ['PastebinBrowser']
from weboob.tools.browser import BaseBrowser
BaseBrowser.SAVE_RESPONSES=True
class PastebinBrowser(BaseBrowser):
DOMAIN = 'pastebin.com'
ENCODING = 'UTF-8'
PAGES = {'http://%s/(?P<id>.+)' % DOMAIN: PastePage,
'http://%s/' % DOMAIN: PostPage}
def fill_paste(self, paste):
self.location(paste.page_url)
return self.page.fill_paste(paste)
def get_contents(self, _id):
return self.readurl('http://%s/raw.php?i=%s' % (self.DOMAIN, _id))
def post_paste(self, paste):
self.home()
self.page.post(paste)
paste.id = self.page.get_id()
self.fill_paste(paste)

View file

@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Laurent Bachelier
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BasePage
from urlparse import urlparse
__all__ = ['PastePage']
class PastePage(BasePage):
def fill_paste(self, paste):
header = self.parser.select(self.document.getroot(), '#content_left div.paste_box_info', 1)
paste.title = self.parser.select(header, 'div.paste_box_line1 h1', 1).text
def get_id(self):
"""
Find out the ID from the URL
"""
path = urlparse(self.url).path
return path[1:]
class PostPage(BasePage):
def post(self, paste):
self.browser.select_form(name='myform')
self.browser['paste_code'] = paste.contents
self.browser['paste_name'] = paste.title
self.browser.submit()

View file

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Laurent Bachelier
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.paste import BasePaste
__all__ = ['PastebinPaste']
class PastebinPaste(BasePaste):
@classmethod
def id2url(cls, _id):
return 'http://pastebin.com/%s' % _id

View file

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Laurent Bachelier
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.test import BackendTest
from .paste import PastebinPaste
class PastebinTest(BackendTest):
BACKEND = 'pastebin'
def test_get_paste(self):
p = self.backend.get_paste('7HmXwzyt')
self.backend.fillobj(p, ('title', 'contents'))
assert p.title == 'plop'
assert p.page_url == 'http://pastebin.com/7HmXwzyt'
assert p.contents == 'prout'
def test_post(self):
p = PastebinPaste(None, title='ouiboube', contents='Weboob Test')
self.backend.post_paste(p)
assert p.id
assert p.title == 'ouiboube'
assert p.id in p.page_url

View file

@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Laurent Bachelier
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from .base import IBaseCap, CapBaseObject, NotLoaded
__all__ = ['BasePaste', 'ICapPaste']
class BasePaste(CapBaseObject):
"""
Represents a pasted text.
"""
def __init__(self, _id, title=NotLoaded, language=NotLoaded, contents=NotLoaded):
CapBaseObject.__init__(self, unicode(_id))
self.add_field('title', basestring, title)
self.add_field('language', basestring, language)
self.add_field('contents', basestring, contents)
@classmethod
def id2url(cls, _id):
"""Overloaded in child classes provided by backends."""
raise NotImplementedError()
@property
def page_url(self):
return self.id2url(self.id)
class ICapPaste(IBaseCap):
"""
This capability represents the ability for a website backend to store text.
"""
def get_paste(self, _id):
"""
Get a Video from an ID.
@param _id the video id. It can be a numeric ID, or a page url, or so.
@return a Video object
"""
raise NotImplementedError()
def post_message(self, paste):
"""
Post a paste.
@param paste Paste object
@return
"""
raise NotImplementedError()