pastealacon: Fully use Browser2 filters

This also transforms the fill_ function into both a filling and a getting
function. Magic!
This commit is contained in:
Laurent Bachelier 2014-06-04 00:50:50 +02:00
commit 560d36840d

View file

@ -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,21 +42,20 @@ class PastealaconPaste(BasePaste):
class PastePage(HTMLPage):
# TODO use magic Browser2 methods (if possible)
def fill_paste(self, paste):
@method
class fill_paste(ItemElement):
klass = PastealaconPaste
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 parse(self, el):
# 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:
if len(el.xpath('id("content")/div[@class="syntax"]//ol')) != 1:
raise PasteNotFound()
header = self.doc.xpath('id("content")/h3')[0]
matches = re.match(r'Posted by (?P<author>.+) on (?P<date>.+) \(', header.text)
paste.title = matches.groupdict().get('author')
paste.contents = unicode(self.doc.xpath('//textarea[@id="code"]')[0].text)
return paste
def get_id(self):
return self.params['id']
class CaptchaPage(HTMLPage):
pass
@ -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)