From f8cfab4db9e197a403da6c444de1eb295357c9c3 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Sun, 22 Nov 2015 01:36:39 +0100 Subject: generation of rss feed --- watchnews/rss.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ watchnews/web.py | 26 ++++++++++++++++++++------ 2 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 watchnews/rss.py (limited to 'watchnews') diff --git a/watchnews/rss.py b/watchnews/rss.py new file mode 100644 index 0000000..30510c2 --- /dev/null +++ b/watchnews/rss.py @@ -0,0 +1,50 @@ +from . import data +from .web import Difftable + + +from ll.xist import xsc, parse +from ll.xist.ns import xml, rss20 + + +def rss(): + versions = data.Version.raw("""\ +SELECT * +FROM version v +WHERE EXISTS (SELECT 1 + FROM version v2 + WHERE v2.id != v.id + AND v2.item_id == v.item_id + AND v2.created_date < v.created_date) +ORDER BY v.created_date DESC +LIMIT 30""") + + items = [] + for to_version in versions: + from_version = data.Version.select() \ + .where((data.Version.id != to_version.id) & + (data.Version.item == to_version.item) & + (data.Version.created_date < to_version.created_date)) \ + .order_by(data.Version.created_date.desc()) \ + .first() + + difftable = Difftable(to_version, from_version, + inline_style=True).string("utf-8") + items.append(rss20.item( + rss20.title( + "{} - {}".format(to_version.item.feed.title, to_version.title)), + rss20.author(to_version.authors), + rss20.pubDate(to_version.created_date.strftime( + "%a, %d %b %Y %T %z")), + rss20.guid("from={},to={}".format(from_version.id, to_version.id)), + rss20.link(to_version.url), + rss20.description( + xsc.Text(difftable) + ) + )) + return xsc.Frag(xml.XML(), rss20.rss( + rss20.channel( + rss20.title("Watchnews change report"), + *items + ) + ) + ).string("utf-8") diff --git a/watchnews/web.py b/watchnews/web.py index 438e649..05ffcdc 100644 --- a/watchnews/web.py +++ b/watchnews/web.py @@ -9,6 +9,8 @@ import difflib class DiffSupport: + def __init__(self): + self.inline_style = False def _diff(self, line1, line2): diff = list(difflib._mdiff([line1], [line2])) @@ -27,21 +29,31 @@ class DiffSupport: html.td(*self._format_diff(diff2)) )) return html.table(rows, **{'class': 'textdiff'}) - + def _format_diff(self, line): + actionclass = { + '+': 'diff_add', + '-': 'diff_sub', + '^': 'diff_chg'} + actionname = { + '+': 'i', + '-': 'strike', + '^': 'strong'} elems = [] nextpos = line.find("\x00") while nextpos != -1 and nextpos + 1 < len(line): - actionclass = { - '+': 'diff_add', '-': 'diff_sub', - '^': 'diff_chg'}[line[nextpos + 1]] endpos = line.find("\x01", nextpos + 2) if nextpos != 0: # intermediate unchanged text elems += [html.span(line[:nextpos])] text = line[nextpos + 2:endpos] - elems += [html.span(text, **{'class': actionclass})] + if self.inline_style: + elem = html.span(text) + elem.xmlname = actionname[line[nextpos+1]] + elems += [elem] + else: + elems += [html.span(text, **{'class':actionclass[line[nextpos+1]]})] line = line[endpos:] nextpos = line.find("\x00") @@ -53,8 +65,10 @@ class DiffSupport: class Difftable(html.div, DiffSupport): - def __init__(self, to_version, from_version=None): + def __init__(self, to_version, from_version=None, inline_style=False): super().__init__() + self.xmlname = "div" + self.inline_style = inline_style if from_version == None: self.single_version(to_version) else: -- cgit v1.2.1