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 .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
# Browser
@ -33,6 +33,7 @@ class DLFP(BaseBrowser):
'https://linuxfr.org/login.html': LoginPage,
'https://linuxfr.org/news/[^\.]+': 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$': NodePage,
}
@ -101,3 +102,30 @@ class DLFP(BaseBrowser):
def close_session(self):
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.body = self.browser.parser.tostring(div.find('div'))
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')
if subs is not None:
@ -70,6 +77,14 @@ class Article(object):
self.browser = browser
self.url = 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')
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')
self.date = local2utc(self.date)
self.comments = []
def append_comment(self, comment):
self.comments.append(comment)
@ -96,10 +109,24 @@ class Article(object):
def parse_part2(self, 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):
def on_loaded(self):
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):
if not self.article:
self.article = Article(self.browser,