From c5d6497bd576e5cde91b9daa1aff914cce2ff978 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Sat, 7 Jun 2014 23:02:43 +0200 Subject: [PATCH] Add a feeds plugin, using all images sorted by date. --- docs/plugins.rst | 5 +++ sigal/plugins/feeds.py | 80 ++++++++++++++++++++++++++++++++++++++ tests/sample/sigal.conf.py | 6 ++- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 sigal/plugins/feeds.py diff --git a/docs/plugins.rst b/docs/plugins.rst index 558ae12..86f95f8 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -108,3 +108,8 @@ Watermark plugin =================== .. automodule:: sigal.plugins.watermark + +Feeds plugin +============ + +.. automodule:: sigal.plugins.feeds diff --git a/sigal/plugins/feeds.py b/sigal/plugins/feeds.py new file mode 100644 index 0000000..149a9fa --- /dev/null +++ b/sigal/plugins/feeds.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- + +"""Plugin which add RSS/ATOM feeds. + +This plugin requires feedgenerator_. It uses all the images of the gallery, +sorted by date, to show the most recent ones. + +.. _feedgenerator: https://pypi.python.org/pypi/feedgenerator + +Settings: + +- ``rss_feed`` and ``atom_feed``, see below. + +Example:: + + rss_feed = {'feed_url': 'http://example.org/feed.rss', 'nb_items': 10} + atom_feed = {'feed_url': 'http://example.org/feed.atom', 'nb_items': 10} + +""" + +import codecs +import logging +import os + +from datetime import datetime +from feedgenerator import Atom1Feed, Rss201rev2Feed +from jinja2 import Markup +from sigal import signals + +logger = logging.getLogger(__name__) + + +def generate_feeds(gallery): + # Get all images and sort by date + images = [img for album in gallery.albums.values() + for img in album.images if img.date is not None] + images.sort(key=lambda im: im.date, reverse=True) + + settings = gallery.settings + if settings.get('rss_feed'): + generate_feed(gallery, images, feed_type='rss', **settings['rss_feed']) + if settings.get('atom_feed'): + generate_feed(gallery, images, feed_type='atom', + **settings['atom_feed']) + + +def generate_feed(gallery, images, feed_type=None, feed_url='', nb_items=0): + root_album = gallery.albums['.'] + cls = Rss201rev2Feed if feed_type == 'rss' else Atom1Feed + feed = cls( + title=Markup(root_album.title), + link='/', + feed_url=feed_url, + description=Markup(root_album.description).striptags() + ) + + nb_images = len(images) + nb_items = min(nb_items, nb_images) if nb_items > 0 else nb_images + + for item in images[:nb_items]: + feed.add_item( + title=Markup(item.title or item.url), + link='%s/#%s' % (item.path, item.url), + # unique_id='tag:%s,%s:%s' % (urlparse(link).netloc, + # item.date.date(), + # urlparse(link).path.lstrip('/')), + description='' % (item.path, item.thumbnail), + # categories=item.tags if hasattr(item, 'tags') else None, + author_name=getattr(item, 'author', ''), + pubdate=item.date or datetime.now(), + ) + + output_file = os.path.join(root_album.dst_path, feed_url.split('/')[-1]) + logger.info('Generate %s feeds: %s', feed_type.upper(), output_file) + with codecs.open(output_file, 'w', 'utf-8') as f: + feed.write(f, 'utf-8') + + +def register(settings): + signals.gallery_build.connect(generate_feeds) diff --git a/tests/sample/sigal.conf.py b/tests/sample/sigal.conf.py index b12ac71..c5da888 100644 --- a/tests/sample/sigal.conf.py +++ b/tests/sample/sigal.conf.py @@ -9,7 +9,8 @@ keep_orig = True links = [('Example link', 'http://example.org'), ('Another link', 'http://example.org')] -plugins = ['sigal.plugins.adjust', 'sigal.plugins.copyright', 'sigal.plugins.watermark'] +plugins = ['sigal.plugins.adjust', 'sigal.plugins.copyright', + 'sigal.plugins.watermark', 'sigal.plugins.feeds', ] copyright = u"© An example copyright message" adjust_options = {'color': 0.0, 'brightness': 1.0, 'contrast': 1.0, 'sharpness': 0.0} @@ -20,6 +21,9 @@ watermark_opacity = 0.3 theme = 'colorbox' thumb_size = (200, 150) +rss_feed = {'feed_url': 'http://example.org/feed.rss', 'nb_items': 10} +atom_feed = {'feed_url': 'http://example.org/feed.atom', 'nb_items': 10} + # theme = 'galleria' # thumb_size = (280, 210) # show_map = True