From 913d89192e123aa31642a045e7651947c0f30173 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 3 Apr 2010 18:01:52 +0200 Subject: [PATCH] [dlfp] url2id and id2url functions --- weboob/backends/dlfp/feeds.py | 9 +++---- weboob/backends/dlfp/tools.py | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 weboob/backends/dlfp/tools.py diff --git a/weboob/backends/dlfp/feeds.py b/weboob/backends/dlfp/feeds.py index b602e3ba..00123327 100644 --- a/weboob/backends/dlfp/feeds.py +++ b/weboob/backends/dlfp/feeds.py @@ -23,6 +23,8 @@ import feedparser import re from datetime import datetime +from .tools import url2id + class Article: RSS = None @@ -56,10 +58,5 @@ class ArticlesList: url = klass.RSS feed = feedparser.parse(url) for item in feed['items']: - m = re.match('.*/([0-9]+).html', item['link']) - if not m: - warning('Unable to parse ID from link \'%s\'' % item['link']) - continue - _id = m.group(1) - article = klass(_id, item['link'], item['title'], item['author'], datetime(*item['date_parsed'][:7])) + article = klass(url2id(item['link']), item['link'], item['title'], item['author'], datetime(*item['date_parsed'][:7])) yield article diff --git a/weboob/backends/dlfp/tools.py b/weboob/backends/dlfp/tools.py new file mode 100644 index 00000000..55fb67e0 --- /dev/null +++ b/weboob/backends/dlfp/tools.py @@ -0,0 +1,44 @@ +# -*- 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 re + +ID2URL_NEWSPAPER = re.compile('.*/(\d{4})/(\d{2})/(\d{2})/(\d+)\.html$') +ID2URL_TELEGRAM = re.compile('.*/~([A-Za-z0-9_]+)/(\d+)\.html$') +URL2ID_NEWSPAPER = re.compile('^N(\d{4})(\d{2})(\d{2}).(\d+)$') +URL2ID_TELEGRAM = re.compile('^T([A-Za-z0-9_]+).(\d+)$') + +def url2id(url): + m = ID2URL_NEWSPAPER.match(url) + if m: + return 'N%04d%02d%02d.%d' % (int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))) + m = ID2URL_TELEGRAM.match(url) + if m: + return 'T%s.%d' % (m.group(1), int(m.group(2))) + return None + +def id2url(_id): + m = URL2ID_NEWSPAPER.match(_id) + if m: + return '/%04d/%02d/%02d/%d.html' % (int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))) + m = URL2ID_TELEGRAM.match(_id) + if m: + return '/~%s/%d.html' % (m.group(1), int(m.group(2))) + return None