Add a feeds plugin, using all images sorted by date.

This commit is contained in:
Simon Conseil
2014-06-07 23:02:43 +02:00
parent ea3eae4657
commit c5d6497bd5
3 changed files with 90 additions and 1 deletions

View File

@@ -108,3 +108,8 @@ Watermark plugin
===================
.. automodule:: sigal.plugins.watermark
Feeds plugin
============
.. automodule:: sigal.plugins.feeds

80
sigal/plugins/feeds.py Normal file
View File

@@ -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='<img src="%s/%s" />' % (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)

View File

@@ -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