weboob-devel/weboob/tools/capabilities/messages/GenericBackend.py
Laurent Bachelier 006e97a8be PEP8 style fixes and other small style fixes
I used autopep8 on some files and did carefully check the changes.
I ignored E501,E302,E231,E225,E222,E221,E241,E203 in my search, and at
least E501 on any autopep8 run.

Other style fixes not related to PEP8:
* Only use new-style classes. I don't think the usage of old-style
  classes was voluntary. Old-style classes are removed in Python 3.
* Convert an if/else to a one-liner in mediawiki, change docstring style
  change to a comment something that wasn't really appropriate for a
  docstring.
* Unneeded first if condition in meteofrance
2012-03-14 04:51:46 +01:00

126 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
# Copyright(C) 2011 Julien Hebert
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
# python2.5 compatibility
from __future__ import with_statement
import time
from weboob.capabilities.messages import ICapMessages, Message, Thread
from weboob.tools.backend import BaseBackend
from weboob.tools.newsfeed import Newsfeed
class GenericNewspaperBackend(BaseBackend, ICapMessages):
"GenericNewspaperBackend class"
MAINTAINER = 'Julien Hebert'
EMAIL = 'juke@free.fr'
VERSION = '0.b'
LICENSE = 'AGPLv3+'
STORAGE = {'seen': {}}
RSS_FEED = None
RSSID = None
URL2ID = None
RSSSIZE = 0
def get_thread(self, _id):
if isinstance(_id, Thread):
thread = _id
_id = thread.id
else:
thread = None
with self.browser:
content = self.browser.get_content(_id)
if content is None:
return None
if not thread:
thread = Thread(_id)
flags = Message.IS_HTML
if not thread.id in self.storage.get('seen', default={}):
flags |= Message.IS_UNREAD
thread.title = content.title
if not thread.date:
thread.date = content.date
thread.root = Message(
thread=thread,
id=0,
title=content.title,
sender=content.author,
receivers=None,
date=thread.date,
parent=None,
content=content.body,
signature='URL: %s' % content.url,
flags=flags,
children=[])
return thread
def iter_threads(self):
for article in Newsfeed(self.RSS_FEED, GenericNewspaperBackend.RSSID).iter_entries():
thread = Thread(article.id)
thread.title = article.title
thread.date = article.datetime
yield(thread)
def fill_thread(self, thread, fields):
"fill the thread"
t = self.get_thread(thread)
return t or thread
def iter_unread_messages(self, thread=None):
for thread in self.iter_threads():
if thread.id in self.storage.get('seen', default={}):
continue
self.fill_thread(thread, 'root')
for msg in thread.iter_all_messages():
yield msg
def set_message_read(self, message):
self.storage.set(
'seen',
message.thread.id,
'comments',
self.storage.get(
'seen',
message.thread.id,
'comments',
default=[]) + [message.id])
if self.URL2ID and self.RSSSIZE != 0:
url2id = self.URL2ID
lastpurge = self.storage.get('lastpurge', default=0)
l = []
if time.time() - lastpurge > 7200:
self.storage.set('lastpurge', time.time())
for id in self.storage.get('seen', default={}):
l.append((int(url2id(id)), id))
l.sort()
l.reverse()
tosave = [v[1] for v in l[0:self.RSSSIZE + 10]]
toremove = set([v for v in self.storage.get('seen', default={})]).difference(tosave)
for id in toremove:
self.storage.delete('seen', id)
self.storage.save()
OBJECTS = {Thread: fill_thread}