<s> """<STR_LIT>""" <EOL> import codecs <EOL> import logging <EOL> import os <EOL> from datetime import datetime <EOL> from feedgenerator import Atom1Feed , Rss201rev2Feed <EOL> from jinja2 import Markup <EOL> from sigal import signals , compat <EOL> logger = logging . getLogger ( __name__ ) <EOL> def generate_feeds ( gallery ) : <EOL> images = [ img for album in gallery . albums . values ( ) <EOL> for img in album . images if img . date is not None ] <EOL> images . sort ( key = lambda im : im . date , reverse = True ) <EOL> settings = gallery . settings <EOL> if settings . get ( '<STR_LIT>' ) : <EOL> generate_feed ( gallery , images , feed_type = '<STR_LIT>' , ** settings [ '<STR_LIT>' ] ) <EOL> if settings . get ( '<STR_LIT>' ) : <EOL> generate_feed ( gallery , images , feed_type = '<STR_LIT>' , <EOL> ** settings [ '<STR_LIT>' ] ) <EOL> def generate_feed ( gallery , images , feed_type = None , feed_url = '<STR_LIT>' , nb_items = <NUM_LIT:0> ) : <EOL> root_album = gallery . albums [ '<STR_LIT:.>' ] <EOL> cls = Rss201rev2Feed if feed_type == '<STR_LIT>' else Atom1Feed <EOL> feed = cls ( <EOL> title = Markup ( root_album . title ) , <EOL> link = '<STR_LIT:/>' , <EOL> feed_url = feed_url , <EOL> description = Markup ( root_album . description ) . striptags ( ) <EOL> ) <EOL> nb_images = len ( images ) <EOL> nb_items = min ( nb_items , nb_images ) if nb_items > <NUM_LIT:0> else nb_images <EOL> for item in images [ : nb_items ] : <EOL> feed . add_item ( <EOL> title = Markup ( item . title or item . url ) , <EOL> link = '<STR_LIT>' % ( item . path , item . url ) , <EOL> description = '<STR_LIT>' % ( item . path , item . thumbnail ) , <EOL> author_name = getattr ( item , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> pubdate = item . date or datetime . now ( ) , <EOL> ) <EOL> output_file = os . path . join ( root_album . dst_path , feed_url . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] ) <EOL> logger . info ( '<STR_LIT>' , feed_type . upper ( ) , output_file ) <EOL> encoding = '<STR_LIT:utf-8>' if not compat . PY2 else None <EOL> with codecs . open ( output_file , '<STR_LIT:w>' , encoding ) as f : <EOL> feed . write ( f , '<STR_LIT:utf-8>' ) <EOL> def register ( settings ) : <EOL> signals . gallery_build . connect ( generate_feeds ) </s>
