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 re
import requests import requests
from weboob.capabilities.paste import BasePaste, PasteNotFound 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): class Spam(Exception):
@ -42,20 +42,19 @@ class PastealaconPaste(BasePaste):
class PastePage(HTMLPage): class PastePage(HTMLPage):
# TODO use magic Browser2 methods (if possible) @method
def fill_paste(self, paste): class fill_paste(ItemElement):
# there is no 404, try to detect if there really is a content klass = PastealaconPaste
if len(self.doc.xpath('id("content")/div[@class="syntax"]//ol')) != 1:
raise PasteNotFound()
header = self.doc.xpath('id("content")/h3')[0] obj_id = Env('id')
matches = re.match(r'Posted by (?P<author>.+) on (?P<date>.+) \(', header.text) obj_title = Regexp(CleanText('id("content")/h3'), r'Posted by (.+) on .+ \(')
paste.title = matches.groupdict().get('author') obj__date = DateTime(Regexp(CleanText('id("content")/h3'), r'Posted by .+ on (.+) \('))
paste.contents = unicode(self.doc.xpath('//textarea[@id="code"]')[0].text) obj_contents = RawText('//textarea[@id="code"]')
return paste
def get_id(self): def parse(self, el):
return self.params['id'] # 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): class CaptchaPage(HTMLPage):
@ -113,4 +112,4 @@ class PastealaconBrowser(PagesBrowser):
self.post.stay_or_go().post(paste, expiration=expiration) self.post.stay_or_go().post(paste, expiration=expiration)
if self.captcha.is_here(): if self.captcha.is_here():
raise Spam() raise Spam()
paste.id = self.page.get_id() self.page.fill_paste(paste)