Add tool to manage RSS/Atom feeds

- weboob.tools.newsfeed a class representing a feed
 - the DLFP backend has been updated to use weboob.tools.newsfeed
This commit is contained in:
Clément Schreiner 2010-10-13 22:39:14 +02:00
commit a5470b9ce6
3 changed files with 57 additions and 62 deletions

View file

@ -18,10 +18,11 @@
from __future__ import with_statement from __future__ import with_statement
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
from weboob.tools.newsfeed import NewsFeed
from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread, CantSendMessage from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread, CantSendMessage
from .feeds import ArticlesList
from .browser import DLFP from .browser import DLFP
from .tools import id2url, url2id
__all__ = ['DLFPBackend'] __all__ = ['DLFPBackend']
@ -41,6 +42,9 @@ class DLFPBackend(BaseBackend, ICapMessages, ICapMessagesPost):
} }
STORAGE = {'seen': {}} STORAGE = {'seen': {}}
BROWSER = DLFP BROWSER = DLFP
RSS_TELEGRAMS= "https://linuxfr.org/backend/journaux/rss20.rss"
RSS_NEWSPAPERS = "https://linuxfr.org/backend/news/rss20.rss"
def create_default_browser(self): def create_default_browser(self):
return self.create_browser(self.config['username'], self.config['password']) return self.create_browser(self.config['username'], self.config['password'])
@ -48,12 +52,13 @@ class DLFPBackend(BaseBackend, ICapMessages, ICapMessagesPost):
def iter_threads(self): def iter_threads(self):
whats = set() whats = set()
if self.config['get_news']: if self.config['get_news']:
whats.add('newspaper') whats.add(self.RSS_NEWSPAPERS)
if self.config['get_telegrams']: if self.config['get_telegrams']:
whats.add('telegram') whats.add(self.RSS_TELEGRAMS)
for what in whats: for what in whats:
for article in ArticlesList(what).iter_articles(): for article in NewsFeed(what, url2id).iter_entries():
thread = Thread(article.id) thread = Thread(article.id)
thread.title = article.title thread.title = article.title
yield thread yield thread

View file

@ -1,58 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010 Romain Bignon
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import feedparser
from datetime import datetime
from .tools import url2id
class Article:
RSS = None
def __init__(self, _id, url, title, author, datetime):
self.id = _id
self.url = url
self.title = title
self.author = author
self.datetime = datetime
class Newspaper(Article):
RSS = 'https://linuxfr.org/backend/news/rss20.rss'
class Telegram(Article):
RSS = 'https://linuxfr.org/backend/journaux/rss20.rss'
class ArticlesList:
RSS = {'newspaper': Newspaper,
'telegram': Telegram
}
def __init__(self, section=None):
self.section = section
self.articles = []
def iter_articles(self):
for section, klass in self.RSS.iteritems():
if self.section and self.section != section:
continue
url = klass.RSS
feed = feedparser.parse(url)
for item in feed['items']:
article = klass(url2id(item['link']), item['link'], item['title'], item['author'], datetime(*item['date_parsed'][:7]))
yield article

48
weboob/tools/newsfeed.py Normal file
View file

@ -0,0 +1,48 @@
import datetime
import feedparser
from weboob.backends.dlfp.tools import url2id
class Entry:
def __init__(self, entry, url2id=None):
if url2id:
self.id = url2id(entry.id)
else:
self.id = entry.id
if entry.has_key("link"):
self.link = entry["link"]
if entry.has_key("title"):
self.title = entry["title"]
else:
self.title = None
if entry.has_key("author"):
self.author = entry["author"]
else:
self.author = None
if entry.has_key("updated_parsed"):
#updated_parsed = entry["updated_parsed"]
self.datetime = datetime.datetime(*entry['updated_parsed'][:7])
else:
self.datetime = None
if entry.has_key("content"):
self.content = entry["content"][0]["value"]
else:
self.content = None
class NewsFeed:
def __init__(self, url, url2id=None):
self.feed = feedparser.parse(url)
self.url2id = url2id
def iter_entries(self):
for entry in self.feed['entries']:
yield Entry(entry, self.url2id)
def get_entry(self, id):
for entry in self.feed['entries']:
if entry.id == id:
return Entry(entry, self.url2id)