factorize

This commit is contained in:
Romain Bignon 2011-03-21 11:28:42 +01:00
commit 65a8470335

View file

@ -18,68 +18,60 @@
import re import re
RSSID_RE = re.compile('tag:.*:(\w)\w+/(\d+)') RSSID_RE = re.compile('tag:.*:(\w+)/(\d+)')
ID2URL_RE = re.compile('^(\w)([\w\-_]*)\.([^ \.]+)$') ID2URL_RE = re.compile('^(\w)([\w\-_]*)\.([^ \.]+)$')
URL2ID_DIARY_RE = re.compile('.*/users/([\w\-_]+)/journaux/([^\.]+)')
URL2ID_NEWSPAPER_RE = re.compile('.*/news/(.+)') REGEXPS = {'/users/%s/journaux/%s': 'D%s.%s',
URL2ID_WIKI_RE = re.compile('.*/wiki/([^ /]+)') '/news/%s': 'N.%s',
URL2ID_SUIVI_RE = re.compile('.*/suivi/([^ /]+)') '/wiki/%s': 'W.%s',
URL2ID_SONDAGE_RE = re.compile('.*/sondages/([^ /]+)') '/suivi/%s': 'T.%s',
URL2ID_FORUM_RE = re.compile('.*/forums/([\w\-_]+)/posts/([^\.]+)') '/sondages/%s': 'P.%s',
'/forums/%s/posts/%s': 'B.%s',
}
def f2re(f):
return '.*' + f.replace('%s', '([^ /]+)')
def rssid(entry): def rssid(entry):
m = RSSID_RE.match(entry.id) m = RSSID_RE.match(entry.id)
if not m: if not m:
return None return None
if m.group(1) == 'D':
mm = URL2ID_DIARY_RE.match(entry.link) ind = m.group(1).replace('Post', 'Board')[0]
if not mm:
return for url_re, id_re in REGEXPS.iteritems():
return 'D%s.%s' % (mm.group(1), m.group(2)) if id_re[0] != ind:
if m.group(1) == 'F': continue
mm = URL2ID_FORUM_RE.match(entry.link)
if not mm: if id_re.count('%s') == 2:
return mm = re.match(f2re(url_re), entry.link)
return 'F%s.%s' % (mm.group(1), m.group(2)) if not mm:
return '%s.%s' % (m.group(1), m.group(2)) return
return '%s%s.%s' % (ind, mm.group(1), m.group(2))
else:
return '%s.%s' % (ind, m.group(2))
def id2url(id): def id2url(id):
m = ID2URL_RE.match(id) m = ID2URL_RE.match(id)
if not m: if not m:
return None return None
if m.group(1) == 'N': for url_re, id_re in REGEXPS.iteritems():
return '/news/%s' % m.group(3) if id_re[0] != m.group(1):
if m.group(1) == 'D': continue
return '/users/%s/journaux/%s' % (m.group(2), m.group(3))
if m.group(1) == 'W': if id_re.count('%s') == 2:
return '/wiki/%s' % m.group(3) return url_re % (m.group(2), m.group(3))
if m.group(1) == 'F': else:
return '/forums/%s/posts/%s' % (m.group(2), m.group(3)) return url_re % m.group(3)
if m.group(1) == 'S':
return '/suivi/%s' % m.group(3)
if m.group(1) == 's':
return '/sondages/%s?results=1' % m.group(3)
def url2id(url): def url2id(url):
m = URL2ID_NEWSPAPER_RE.match(url) for url_re, id_re in REGEXPS.iteritems():
if m: m = re.match(f2re(url_re), url)
return 'N.%s' % (m.group(1)) if not m:
m = URL2ID_DIARY_RE.match(url) continue
if m:
return 'D%s.%s' % (m.group(1), m.group(2)) return id_re % m.groups()
m = URL2ID_WIKI_RE.match(url)
if m:
return 'W.%s' % (m.group(1))
m = URL2ID_FORUM_RE.match(url)
if m:
return 'F%s.%s' % (m.group(1), m.group(2))
m = URL2ID_SUIVI_RE.match(url)
if m:
return 'S.%s' % (m.group(1))
m = URL2ID_SONDAGE_RE.match(url)
if m:
return 's.%s' % (m.group(1))
def id2threadid(id): def id2threadid(id):
m = ID2URL_RE.match(id) m = ID2URL_RE.match(id)