paste* backends: "Paste not found" exception

This commit is contained in:
Laurent Bachelier 2011-04-20 01:03:04 +02:00
commit 154c061398
6 changed files with 56 additions and 10 deletions

View file

@ -20,7 +20,9 @@
from mechanize import RobustFactory
import re
from weboob.tools.browser import BaseBrowser, BrowserUnavailable
from weboob.tools.browser import BaseBrowser, BrowserUnavailable, BrowserHTTPNotFound
from weboob.capabilities.paste import PasteNotFound
from .pages import PastePage, CaptchaPage, PostPage
@ -50,7 +52,10 @@ class PastealaconBrowser(BaseBrowser):
This is the fastest and safest method if you only want the content.
Returns unicode.
"""
return self.readurl('http://%s/pastebin.php?dl=%s' % (self.DOMAIN, _id)).decode(self.ENCODING)
try:
return self.readurl('http://%s/pastebin.php?dl=%s' % (self.DOMAIN, _id), if_fail='raise').decode(self.ENCODING)
except BrowserHTTPNotFound:
raise PasteNotFound()
def post_paste(self, paste):
self.home()

View file

@ -18,16 +18,23 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BasePage
import re
from weboob.tools.browser import BasePage, BrokenPageError
from weboob.capabilities.paste import PasteNotFound
__all__ = ['PastePage', 'PostPage', 'CaptchaPage']
class PastePage(BasePage):
def fill_paste(self, paste):
root = self.document.getroot()
header = self.parser.select(root, 'id("content")//h3', 1, 'xpath')
try:
# there is no 404, try to detect if there really is a content
self.parser.select(root, 'id("content")/div[@class="syntax"]//ol', 1, 'xpath')
except BrokenPageError:
raise PasteNotFound()
header = self.parser.select(root, 'id("content")/h3', 1, 'xpath')
matches = re.match(r'Posted by (?P<author>.+) on (?P<date>.+) \(', header.text)
paste.title = matches.groupdict().get('author')
paste.contents = self.parser.select(root, '//textarea[@id="code"]', 1, 'xpath').text

View file

@ -17,9 +17,13 @@
# 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 weboob.capabilities.base import NotLoaded
from weboob.tools.browser import BrowserUnavailable
from weboob.capabilities.paste import PasteNotFound
from .paste import PastealaconPaste
class PastealaconTest(BackendTest):
@ -53,3 +57,12 @@ class PastealaconTest(BackendTest):
def test_spam(self):
p = PastealaconPaste(None, title='viagra', contents='http://example.com/')
self.assertRaises(BrowserUnavailable, self.backend.post_paste, p)
def test_notfound(self):
# html method
p = self.backend.get_paste('424242424242424242424242424242424242')
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['title'])
# raw method
p = self.backend.get_paste('424242424242424242424242424242424242')
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['contents'])