add rss support

This commit is contained in:
Adrien Beudin 2016-04-18 14:27:28 +02:00
parent 2668afd17b
commit de2b7433a8
6 changed files with 77 additions and 8 deletions

View File

@ -8,7 +8,7 @@ import shutil
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
from .cache import CACHE from .cache import CACHE
from .utils import error from .utils import error, warning, okgreen
SETTINGS = { SETTINGS = {
"show_date": True, "show_date": True,
@ -47,11 +47,11 @@ class Image(object):
} }
command = "gm convert {source} {auto-orient} {strip} {quality} {resize} {target}".format(**gm_switches) command = "gm convert {source} {auto-orient} {strip} {quality} {resize} {target}".format(**gm_switches)
print command warning("Generation", source)
os.system(command) os.system(command)
CACHE.cache_picture(source, target, options) CACHE.cache_picture(source, target, options)
else: else:
print "skipped %s since it's already generated (based on source unchanged size and images options set in your gallery's settings.yaml)" % target okgreen("Skipped", source + "it's already generated")
def copy(self): def copy(self):
source, target = os.path.join(self.base_dir, self.name), os.path.join(self.target_dir, self.name) source, target = os.path.join(self.base_dir, self.name), os.path.join(self.target_dir, self.name)
@ -97,6 +97,8 @@ def main():
error(isinstance(settings, dict), "Your settings.yaml should be a dict") error(isinstance(settings, dict), "Your settings.yaml should be a dict")
error(settings.get("title"), "You should specify a title in your main settings.yaml") error(settings.get("title"), "You should specify a title in your main settings.yaml")
if settings.get("rss") or settings.get("share"):
error(settings.get("url"), "If you want the rss and the social network share work, you should specify url in main settings")
if settings.get("settings", {}).get("gm"): if settings.get("settings", {}).get("gm"):
SETTINGS["gm"].update(settings["settings"]["gm"]) SETTINGS["gm"].update(settings["settings"]["gm"])
@ -114,13 +116,14 @@ def main():
os.makedirs("build") os.makedirs("build")
theme = settings.get("settings", {}).get("theme", "exposure") theme = settings.get("settings", {}).get("theme", "exposure")
error(os.path.exists(os.path.join(os.path.split(os.path.realpath(__file__))[0], "themes", theme)), "'%s' is not an existing theme, available themes are '%s'" % (theme, "', '".join(os.listdir(os.path.join(os.path.split(os.path.realpath(__file__))[0], "themes"))))) error(os.path.exists(os.path.join(os.path.split(os.path.realpath(__file__))[0], "themes", theme)), "'%s' is not an existing theme, available themes are '%s'" % (theme, "', '".join(os.listdir(os.path.join(os.path.split(os.path.realpath(__file__))[0], "themes")))))
templates = Environment(loader=FileSystemLoader([os.path.realpath(os.path.join(os.getcwd(), "templates")), os.path.join(os.path.split(os.path.realpath(__file__))[0], "themes", theme, "templates")])) templates = Environment(loader=FileSystemLoader([os.path.realpath(os.path.join(os.getcwd(), "templates")), os.path.join(os.path.split(os.path.realpath(__file__))[0], "themes", theme, "templates")]))
index_template = templates.get_template("index.html") index_template = templates.get_template("index.html")
gallery_index_template = templates.get_template("gallery-index.html") gallery_index_template = templates.get_template("gallery-index.html")
page_template = templates.get_template("page.html") page_template = templates.get_template("page.html")
feed_template = templates.get_template("feed.xml")
# XXX recursively merge directories # XXX recursively merge directories
if os.path.exists(os.path.join(os.getcwd(), "build", "static")): if os.path.exists(os.path.join(os.getcwd(), "build", "static")):
@ -161,6 +164,9 @@ def main():
template_to_render = page_template if gallery_settings.get("static") else gallery_index_template template_to_render = page_template if gallery_settings.get("static") else gallery_index_template
open(os.path.join("build", gallery, "index.html"), "w").write(template_to_render.render(settings=settings, gallery=gallery_settings, Image=Image, link=gallery).encode("Utf-8")) open(os.path.join("build", gallery, "index.html"), "w").write(template_to_render.render(settings=settings, gallery=gallery_settings, Image=Image, link=gallery).encode("Utf-8"))
if settings.get("rss"):
open(os.path.join("build", "feed.xml"), "w").write(feed_template.render(settings=settings, link=gallery, galleries=reversed(sorted(front_page_galleries_cover, key=lambda x: x["date"]))).encode("Utf-8"))
front_page_galleries_cover = reversed(sorted(front_page_galleries_cover, key=lambda x: x["date"])) front_page_galleries_cover = reversed(sorted(front_page_galleries_cover, key=lambda x: x["date"]))

View File

@ -4,7 +4,9 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
{% block css %} {% block css %}
{% endblock %} {% endblock %}
{% if settings.rss -%}
<link rel="alternate" type="application/rss+xml" title="{{ settings.title }}" href="{{ settings.url }}/feed.xml" />
{% endif -%}
<!--Let browser know website is optimized for mobile--> <!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>{{ settings.title }}</title> <title>{{ settings.title }}</title>

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ settings.title }}</title>
<description>{{ settings.sub_title }}</description>
<link>{{ settings.url }}</link>
<atom:link href="{{ settings.url }}/feed.xml" rel="self" type="application/rss+xml" />
{% for gallery in galleries %}
{% set absolute_url = settings.url + "/" + gallery.link -%}
<item>
<title>{{ gallery.title }}</title>
<link>{{ absolute_url }}</link>
<guid>{{ gallery.link }}</guid>
{% if gallery.sub_title -%}
<description>{{ gallery.sub_title }}</description>
{% endif -%}
<pubDate>{{ gallery.date }}</pubDate>
</item>
{% endfor %}
</channel>
</rss>

View File

@ -4,7 +4,9 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
{% block css %} {% block css %}
{% endblock %} {% endblock %}
{% if settings.rss -%}
<link rel="alternate" type="application/rss+xml" title="{{ settings.title }}" href="{{ settings.url }}/feed.xml" />
{% endif -%}
<!--Let browser know website is optimized for mobile--> <!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>{{ settings.title }}</title> <title>{{ settings.title }}</title>

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ settings.title }}</title>
<description>{{ settings.sub_title }}</description>
<link>{{ settings.url }}</link>
<atom:link href="{{ settings.url }}/feed.xml" rel="self" type="application/rss+xml" />
{% for gallery in galleries %}
{% set absolute_url = settings.url + "/" + gallery.link -%}
<item>
<title>{{ gallery.title }}</title>
<link>{{ absolute_url }}</link>
<guid>{{ gallery.link }}</guid>
{% if gallery.sub_title -%}
<description>{{ gallery.sub_title }}</description>
{% endif -%}
<pubDate>{{ gallery.date }}</pubDate>
</item>
{% endfor %}
</channel>
</rss>

View File

@ -1,11 +1,24 @@
import sys import sys
class bcolors:
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def error(test, error_message): def error(test, error_message):
if test: if test:
return return
sys.stderr.write(error_message) sys.stderr.write(bcolors.FAIL + "Abort : " + bcolors.ENDC + error_message)
sys.stderr.write("\n") sys.stderr.write("\n")
sys.stderr.write("Abort.\n")
sys.exit(1) sys.exit(1)
def warning(logging, warning_message):
sys.stderr.write( "%s%s : %s%s" % (bcolors.WARNING, logging, bcolors.ENDC, warning_message))
sys.stderr.write("\n")
def okgreen(logging, ok_message):
sys.stderr.write( "%s%s : %s%s" % (bcolors.OKGREEN, logging, bcolors.ENDC, ok_message))
sys.stderr.write("\n")