Parse a comment only if needed

This commit is contained in:
Florent 2012-02-22 15:06:51 +01:00 committed by Romain Bignon
commit aaab1db43d
2 changed files with 45 additions and 36 deletions

View file

@ -90,7 +90,7 @@ class DLFPBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapContent):
thread.date = article.datetime thread.date = article.datetime
yield thread yield thread
def get_thread(self, id): def get_thread(self, id, getseen=True):
if isinstance(id, Thread): if isinstance(id, Thread):
thread = id thread = id
id = thread.id id = thread.id
@ -127,11 +127,11 @@ class DLFPBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapContent):
flags=flags) flags=flags)
for com in content.comments: for com in content.comments:
self._insert_comment(com, thread.root) self._insert_comment(com, thread.root, getseen)
return thread 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. Insert 'com' comment and its children in the parent message.
""" """
@ -139,6 +139,9 @@ class DLFPBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapContent):
if not com.id in self.storage.get('seen', parent.thread.id, 'comments', default=[]): if not com.id in self.storage.get('seen', parent.thread.id, 'comments', default=[]):
flags |= Message.IS_UNREAD flags |= Message.IS_UNREAD
if getseen or flags & Message.IS_UNREAD:
com.parse()
print "coin !"
message = Message(thread=parent.thread, message = Message(thread=parent.thread,
id=com.id, id=com.id,
title=com.title, title=com.title,
@ -152,14 +155,18 @@ class DLFPBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapContent):
'URL: %s' % com.url]), 'URL: %s' % com.url]),
children=[], children=[],
flags=flags) flags=flags)
else:
message = Message(thread=parent.thread,
id=com.id,
children=[],
flags=flags)
parent.children.append(message) parent.children.append(message)
for sub in com.comments: for sub in com.comments:
self._insert_comment(sub, message) self._insert_comment(sub, message, getseen)
def iter_unread_messages(self, thread=None): def iter_unread_messages(self, thread=None):
for thread in self.iter_threads(): for thread in self.iter_threads():
self.fill_thread(thread, 'root') self.fill_thread(thread, 'root', False)
for m in thread.iter_all_messages(): for m in thread.iter_all_messages():
if m.flags & m.IS_UNREAD: if m.flags & m.IS_UNREAD:
yield m 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.get('seen', message.thread.id, 'comments', default=[]) + [message.id])
self.storage.save() self.storage.save()
def fill_thread(self, thread, fields): def fill_thread(self, thread, fields, getseen=True):
return self.get_thread(thread) return self.get_thread(thread, getseen)
#### ICapMessagesReply ######################################### #### ICapMessagesReply #########################################
def post_message(self, message): def post_message(self, message):

View file

@ -51,23 +51,31 @@ class Comment(Content):
Content.__init__(self, article.browser) Content.__init__(self, article.browser)
self.reply_id = reply_id self.reply_id = reply_id
self.signature = u'' self.signature = u''
self.preurl = article.url
self.div = div
self.id = div.attrib['id'].split('-')[1] self.id = div.attrib['id'].split('-')[1]
self.url = '%s#%s' % (article.url, div.attrib['id']) subs = div.find('ul')
self.title = unicode(self.browser.parser.select(div.find('h2'), 'a.title', 1).text) 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: 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: except BrokenPageError:
self.author = 'Anonyme' self.author = 'Anonyme'
self.username = None self.username = None
else: else:
self.author = unicode(a.text) self.author = unicode(a.text)
self.username = unicode(a.attrib['href'].split('/')[2]) 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') '%Y-%m-%dT%H:%M:%S')
self.date = local2utc(self.date) self.date = local2utc(self.date)
content = div.find('div') content = self.div.find('div')
try: try:
signature = self.browser.parser.select(content, 'p.signature', 1) signature = self.browser.parser.select(content, 'p.signature', 1)
except BrokenPageError: except BrokenPageError:
@ -78,18 +86,12 @@ class Comment(Content):
self.signature = self.browser.parser.tostring(signature) self.signature = self.browser.parser.tostring(signature)
self.body = self.browser.parser.tostring(content) self.body = self.browser.parser.tostring(content)
self.score = int(self.browser.parser.select(div.find('p'), 'span.score', 1).text) self.score = int(self.browser.parser.select(self.div.find('p'), 'span.score', 1).text)
forms = self.browser.parser.select(div.find('footer'), 'form.button_to') forms = self.browser.parser.select(self.div.find('footer'), 'form.button_to')
if len(forms) > 0: if len(forms) > 0:
self.relevance_url = forms[0].attrib['action'].rstrip('for').rstrip('against') 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'] 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): def iter_all_comments(self):
for comment in self.comments: for comment in self.comments:
yield comment yield comment