ability to plusse/moinse a comment

This commit is contained in:
Romain Bignon 2011-03-05 21:54:57 +01:00
commit 6a779135c1
2 changed files with 58 additions and 3 deletions

View file

@ -22,7 +22,7 @@ from weboob.tools.browser import BaseBrowser, BrowserHTTPError, BrowserIncorrect
from weboob.capabilities.messages import CantSendMessage from weboob.capabilities.messages import CantSendMessage
from .pages.index import IndexPage, LoginPage from .pages.index import IndexPage, LoginPage
from .pages.news import ContentPage, NewCommentPage, NodePage from .pages.news import ContentPage, NewCommentPage, NodePage, CommentPage
from .tools import id2url, url2id from .tools import id2url, url2id
# Browser # Browser
@ -33,6 +33,7 @@ class DLFP(BaseBrowser):
'https://linuxfr.org/login.html': LoginPage, 'https://linuxfr.org/login.html': LoginPage,
'https://linuxfr.org/news/[^\.]+': ContentPage, 'https://linuxfr.org/news/[^\.]+': ContentPage,
'https://linuxfr.org/users/[\w_]+/journaux/[^\.]+': ContentPage, 'https://linuxfr.org/users/[\w_]+/journaux/[^\.]+': ContentPage,
'https://linuxfr.org/nodes/(\d+)/comments/(\d+)$': CommentPage,
'https://linuxfr.org/nodes/(\d+)/comments/nouveau': NewCommentPage, 'https://linuxfr.org/nodes/(\d+)/comments/nouveau': NewCommentPage,
'https://linuxfr.org/nodes/(\d+)/comments$': NodePage, 'https://linuxfr.org/nodes/(\d+)/comments$': NodePage,
} }
@ -101,3 +102,30 @@ class DLFP(BaseBrowser):
def close_session(self): def close_session(self):
self.openurl('/compte/deconnexion') self.openurl('/compte/deconnexion')
def plusse(self, url):
return self.relevance(url, 'for')
def moinse(self, url):
return self.relevance(url, 'against')
def relevance(self, url, what):
self.location(url)
comment = None
if self.is_on_page(CommentPage):
comment = self.page.get_comment()
elif self.is_on_page(ContentPage):
ignored, id = url.rsplit('#comment-', 1)
comment = self.page.get_comment(int(id))
if comment is None:
raise ValueError('The given URL isn\'t a comment.')
if comment.relevance_token is None:
return False
res = self.readurl('%s%s' % (comment.relevance_url, what),
urllib.urlencode({'authenticity_token': comment.relevance_token}))
return res

View file

@ -49,6 +49,13 @@ class Comment(object):
self.date = local2utc(self.date) self.date = local2utc(self.date)
self.body = self.browser.parser.tostring(div.find('div')) self.body = self.browser.parser.tostring(div.find('div'))
self.score = int(select(div.find('p'), 'span.score', 1).text) self.score = int(select(div.find('p'), 'span.score', 1).text)
forms = select(div.find('footer'), 'form.button_to')
if len(forms) == 0:
self.relevance_url = None
self.relevance_token = None
else:
self.relevance_url = forms[0].attrib['action'].rstrip('for').rstrip('against')
self.relevance_token = select(forms[0], 'input[name=authenticity_token]', 1).attrib['value']
subs = div.find('ul') subs = div.find('ul')
if subs is not None: if subs is not None:
@ -70,6 +77,14 @@ class Article(object):
self.browser = browser self.browser = browser
self.url = url self.url = url
self.id = url2id(self.url) self.id = url2id(self.url)
self.title = None
self.author = None
self.body = None
self.date = None
self.comments = []
if not tree:
return
header = tree.find('header') header = tree.find('header')
self.title = u''.join([a.text for a in header.find('h1').findall('a')]) self.title = u''.join([a.text for a in header.find('h1').findall('a')])
@ -82,8 +97,6 @@ class Article(object):
'%Y-%m-%dT%H:%M:%S') '%Y-%m-%dT%H:%M:%S')
self.date = local2utc(self.date) self.date = local2utc(self.date)
self.comments = []
def append_comment(self, comment): def append_comment(self, comment):
self.comments.append(comment) self.comments.append(comment)
@ -96,10 +109,24 @@ class Article(object):
def parse_part2(self, div): def parse_part2(self, div):
self.part2 = self.browser.parser.tostring(div) self.part2 = self.browser.parser.tostring(div)
class CommentPage(DLFPPage):
def get_comment(self):
article = Article(self.browser, self.url, None)
return Comment(article, select(self.document.getroot(), 'li.comment', 1), 0)
class ContentPage(DLFPPage): class ContentPage(DLFPPage):
def on_loaded(self): def on_loaded(self):
self.article = None self.article = None
def get_comment(self, id):
article = Article(self.browser, self.url, None)
try:
li = select(self.document.getroot(), 'li#comment-%s' % id, 1)
except SelectElementException:
return None
else:
return Comment(article, li, 0)
def get_article(self): def get_article(self):
if not self.article: if not self.article:
self.article = Article(self.browser, self.article = Article(self.browser,