replace 'url2id' predicate with 'rssid'

This commit is contained in:
Romain Bignon 2011-02-24 21:34:32 +01:00
commit 8012afe847
7 changed files with 24 additions and 15 deletions

View file

@ -21,7 +21,7 @@ from __future__ import with_statement
from weboob.capabilities.messages import ICapMessages, Message, Thread from weboob.capabilities.messages import ICapMessages, Message, Thread
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
from weboob.tools.newsfeed import Newsfeed from weboob.tools.newsfeed import Newsfeed
from .tools import url2id from .tools import rssid
class GenericNewspaperBackend(BaseBackend, ICapMessages): class GenericNewspaperBackend(BaseBackend, ICapMessages):
"GenericNewspaperBackend class" "GenericNewspaperBackend class"
@ -67,7 +67,7 @@ class GenericNewspaperBackend(BaseBackend, ICapMessages):
return thread return thread
def iter_threads(self): def iter_threads(self):
for article in Newsfeed(self.RSS_FEED, url2id).iter_entries(): for article in Newsfeed(self.RSS_FEED, rssid).iter_entries():
thread = Thread(article.id) thread = Thread(article.id)
thread.title = article.title thread.title = article.title
thread.date = article.datetime thread.date = article.datetime

View file

@ -32,3 +32,6 @@ def id2url(_id):
def url2id(url): def url2id(url):
"return an id from an url" "return an id from an url"
return url return url
def rssid(entry):
return url2id(entry.id)

View file

@ -21,7 +21,7 @@ from __future__ import with_statement
from weboob.capabilities.messages import ICapMessages, Message, Thread from weboob.capabilities.messages import ICapMessages, Message, Thread
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
from weboob.tools.newsfeed import Newsfeed from weboob.tools.newsfeed import Newsfeed
from .tools import url2id from .tools import rssid
class GenericNewspaperBackend(BaseBackend, ICapMessages): class GenericNewspaperBackend(BaseBackend, ICapMessages):
"GenericNewspaperBackend class" "GenericNewspaperBackend class"
@ -67,7 +67,7 @@ class GenericNewspaperBackend(BaseBackend, ICapMessages):
return thread return thread
def iter_threads(self): def iter_threads(self):
for article in Newsfeed(self.RSS_FEED, url2id).iter_entries(): for article in Newsfeed(self.RSS_FEED, rssid).iter_entries():
thread = Thread(article.id) thread = Thread(article.id)
thread.title = article.title thread.title = article.title
thread.date = article.datetime thread.date = article.datetime

View file

@ -32,3 +32,6 @@ def id2url(_id):
def url2id(url): def url2id(url):
"return an id from an url" "return an id from an url"
return url return url
def rssid(entry):
return url2id(entry.id)

View file

@ -21,7 +21,7 @@ from __future__ import with_statement
from weboob.capabilities.messages import ICapMessages, Message, Thread from weboob.capabilities.messages import ICapMessages, Message, Thread
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
from weboob.tools.newsfeed import Newsfeed from weboob.tools.newsfeed import Newsfeed
from .tools import url2id from .tools import rssid
class GenericNewspaperBackend(BaseBackend, ICapMessages): class GenericNewspaperBackend(BaseBackend, ICapMessages):
"GenericNewspaperBackend class" "GenericNewspaperBackend class"
@ -67,7 +67,7 @@ class GenericNewspaperBackend(BaseBackend, ICapMessages):
return thread return thread
def iter_threads(self): def iter_threads(self):
for article in Newsfeed(self.RSS_FEED, url2id).iter_entries(): for article in Newsfeed(self.RSS_FEED, rssid).iter_entries():
thread = Thread(article.id) thread = Thread(article.id)
thread.title = article.title thread.title = article.title
thread.date = article.datetime thread.date = article.datetime

View file

@ -34,3 +34,6 @@ def url2id(url):
regexp = re.compile("http://www.20minutes.fr/(\w+)/([0-9]+)/(.*$)") regexp = re.compile("http://www.20minutes.fr/(\w+)/([0-9]+)/(.*$)")
match = regexp.match(url) match = regexp.match(url)
return '%s.%d.%s' % (match.group(1), int(match.group(2)), match.group(3)) return '%s.%d.%s' % (match.group(1), int(match.group(2)), match.group(3))
def rssid(entry):
return url2id(entry.id)

View file

@ -23,11 +23,8 @@ __all__ = ['Entry', 'Newsfeed']
class Entry: class Entry:
def __init__(self, entry, url2id=None): def __init__(self, entry, rssid_func=None):
if url2id: self.id = entry.id
self.id = url2id(entry.id)
else:
self.id = entry.id
if entry.has_key("link"): if entry.has_key("link"):
self.link = entry["link"] self.link = entry["link"]
@ -63,16 +60,19 @@ class Entry:
else: else:
self.content = None self.content = None
if rssid_func:
self.id = rssid_func(self)
class Newsfeed: class Newsfeed:
def __init__(self, url, url2id=None): def __init__(self, url, rssid_func=None):
self.feed = feedparser.parse(url) self.feed = feedparser.parse(url)
self.url2id = url2id self.rssid_func = rssid_func
def iter_entries(self): def iter_entries(self):
for entry in self.feed['entries']: for entry in self.feed['entries']:
yield Entry(entry, self.url2id) yield Entry(entry, self.rssid_func)
def get_entry(self, id): def get_entry(self, id):
for entry in self.feed['entries']: for entry in self.feed['entries']:
if entry.id == id: if entry.id == id:
return Entry(entry, self.url2id) return Entry(entry, self.rssid_func)