From 560d36840dc05504f2121e218a79e82900a7d54e Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Wed, 4 Jun 2014 00:50:50 +0200 Subject: [PATCH] pastealacon: Fully use Browser2 filters This also transforms the fill_ function into both a filling and a getting function. Magic! --- modules/pastealacon/browser.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/modules/pastealacon/browser.py b/modules/pastealacon/browser.py index 63993698..b1355373 100644 --- a/modules/pastealacon/browser.py +++ b/modules/pastealacon/browser.py @@ -20,9 +20,9 @@ import re import requests - from weboob.capabilities.paste import BasePaste, PasteNotFound -from weboob.tools.browser2 import HTMLPage, PagesBrowser, URL +from weboob.tools.browser2.filters import CleanText, DateTime, Env, RawText, Regexp +from weboob.tools.browser2.page import HTMLPage, ItemElement, method, PagesBrowser, URL class Spam(Exception): @@ -42,20 +42,19 @@ class PastealaconPaste(BasePaste): class PastePage(HTMLPage): - # TODO use magic Browser2 methods (if possible) - def fill_paste(self, paste): - # there is no 404, try to detect if there really is a content - if len(self.doc.xpath('id("content")/div[@class="syntax"]//ol')) != 1: - raise PasteNotFound() + @method + class fill_paste(ItemElement): + klass = PastealaconPaste - header = self.doc.xpath('id("content")/h3')[0] - matches = re.match(r'Posted by (?P.+) on (?P.+) \(', header.text) - paste.title = matches.groupdict().get('author') - paste.contents = unicode(self.doc.xpath('//textarea[@id="code"]')[0].text) - return paste + obj_id = Env('id') + obj_title = Regexp(CleanText('id("content")/h3'), r'Posted by (.+) on .+ \(') + obj__date = DateTime(Regexp(CleanText('id("content")/h3'), r'Posted by .+ on (.+) \(')) + obj_contents = RawText('//textarea[@id="code"]') - def get_id(self): - return self.params['id'] + def parse(self, el): + # there is no 404, try to detect if there really is a content + if len(el.xpath('id("content")/div[@class="syntax"]//ol')) != 1: + raise PasteNotFound() class CaptchaPage(HTMLPage): @@ -113,4 +112,4 @@ class PastealaconBrowser(PagesBrowser): self.post.stay_or_go().post(paste, expiration=expiration) if self.captcha.is_here(): raise Spam() - paste.id = self.page.get_id() + self.page.fill_paste(paste)