From aaab1db43da85b54f4682bd5b89f3c84c85b666d Mon Sep 17 00:00:00 2001 From: Florent Date: Wed, 22 Feb 2012 15:06:51 +0100 Subject: [PATCH] Parse a comment only if needed --- modules/dlfp/backend.py | 51 ++++++++++++++++++++++---------------- modules/dlfp/pages/news.py | 30 +++++++++++----------- 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/modules/dlfp/backend.py b/modules/dlfp/backend.py index 23a8a652..523f56a7 100644 --- a/modules/dlfp/backend.py +++ b/modules/dlfp/backend.py @@ -90,7 +90,7 @@ class DLFPBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapContent): thread.date = article.datetime yield thread - def get_thread(self, id): + def get_thread(self, id, getseen=True): if isinstance(id, Thread): thread = id id = thread.id @@ -127,39 +127,46 @@ class DLFPBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapContent): flags=flags) for com in content.comments: - self._insert_comment(com, thread.root) + self._insert_comment(com, thread.root, getseen) return thread - def _insert_comment(self, com, parent): + def _insert_comment(self, com, parent, getseen=True): """" Insert 'com' comment and its children in the parent message. """ flags = Message.IS_HTML if not com.id in self.storage.get('seen', parent.thread.id, 'comments', default=[]): flags |= Message.IS_UNREAD - - message = Message(thread=parent.thread, - id=com.id, - title=com.title, - sender=com.author or u'', - receivers=None, - date=com.date, - parent=parent, - content=com.body, - signature=com.signature + \ - '
'.join(['Score: %d' % com.score, - 'URL: %s' % com.url]), - children=[], - flags=flags) - + + if getseen or flags & Message.IS_UNREAD: + com.parse() + print "coin !" + message = Message(thread=parent.thread, + id=com.id, + title=com.title, + sender=com.author or u'', + receivers=None, + date=com.date, + parent=parent, + content=com.body, + signature=com.signature + \ + '
'.join(['Score: %d' % com.score, + 'URL: %s' % com.url]), + children=[], + flags=flags) + else: + message = Message(thread=parent.thread, + id=com.id, + children=[], + flags=flags) parent.children.append(message) for sub in com.comments: - self._insert_comment(sub, message) + self._insert_comment(sub, message, getseen) def iter_unread_messages(self, thread=None): for thread in self.iter_threads(): - self.fill_thread(thread, 'root') + self.fill_thread(thread, 'root', False) for m in thread.iter_all_messages(): if m.flags & m.IS_UNREAD: yield m @@ -169,8 +176,8 @@ class DLFPBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapContent): self.storage.get('seen', message.thread.id, 'comments', default=[]) + [message.id]) self.storage.save() - def fill_thread(self, thread, fields): - return self.get_thread(thread) + def fill_thread(self, thread, fields, getseen=True): + return self.get_thread(thread, getseen) #### ICapMessagesReply ######################################### def post_message(self, message): diff --git a/modules/dlfp/pages/news.py b/modules/dlfp/pages/news.py index c508818f..02f49983 100644 --- a/modules/dlfp/pages/news.py +++ b/modules/dlfp/pages/news.py @@ -51,23 +51,31 @@ class Comment(Content): Content.__init__(self, article.browser) self.reply_id = reply_id self.signature = u'' - + self.preurl = article.url + self.div = div self.id = div.attrib['id'].split('-')[1] - self.url = '%s#%s' % (article.url, div.attrib['id']) - self.title = unicode(self.browser.parser.select(div.find('h2'), 'a.title', 1).text) + subs = div.find('ul') + if subs is not None: + for sub in subs.findall('li'): + comment = Comment(article, sub, self.id) + self.comments.append(comment) + + def parse(self): + self.url = '%s#%s' % (self.preurl, self.div.attrib['id']) + self.title = unicode(self.browser.parser.select(self.div.find('h2'), 'a.title', 1).text) try: - a = self.browser.parser.select(div.find('p'), 'a[rel=author]', 1) + a = self.browser.parser.select(self.div.find('p'), 'a[rel=author]', 1) except BrokenPageError: self.author = 'Anonyme' self.username = None else: self.author = unicode(a.text) self.username = unicode(a.attrib['href'].split('/')[2]) - self.date = datetime.strptime(self.browser.parser.select(div.find('p'), 'time', 1).attrib['datetime'].split('+')[0], + self.date = datetime.strptime(self.browser.parser.select(self.div.find('p'), 'time', 1).attrib['datetime'].split('+')[0], '%Y-%m-%dT%H:%M:%S') self.date = local2utc(self.date) - content = div.find('div') + content = self.div.find('div') try: signature = self.browser.parser.select(content, 'p.signature', 1) except BrokenPageError: @@ -78,18 +86,12 @@ class Comment(Content): self.signature = self.browser.parser.tostring(signature) self.body = self.browser.parser.tostring(content) - self.score = int(self.browser.parser.select(div.find('p'), 'span.score', 1).text) - forms = self.browser.parser.select(div.find('footer'), 'form.button_to') + self.score = int(self.browser.parser.select(self.div.find('p'), 'span.score', 1).text) + forms = self.browser.parser.select(self.div.find('footer'), 'form.button_to') if len(forms) > 0: self.relevance_url = forms[0].attrib['action'].rstrip('for').rstrip('against') self.relevance_token = self.browser.parser.select(forms[0], 'input[name=authenticity_token]', 1).attrib['value'] - subs = div.find('ul') - if subs is not None: - for sub in subs.findall('li'): - comment = Comment(article, sub, self.id) - self.comments.append(comment) - def iter_all_comments(self): for comment in self.comments: yield comment