website/start_site.py

78 lines
2.6 KiB
Python

#!/usr/bin/env python
import os
from datetime import datetime
import pytz
from flask import Flask, url_for, render_template, send_from_directory
from feedgen.feed import FeedGenerator
app = Flask(__name__)
meta_data = {root[len("./templates/blog/"):] :
datetime.fromtimestamp(os.path.getmtime(os.path.join(root,"index.html")))
for root, dirs, files in os.walk("./templates/blog") if "index.html" in files}
@app.route('/')
def index(_paths=meta_data):
sorted_meta_data = dict(sorted(meta_data.items(), reverse=True, key=lambda item : item[1]))
return render_template("index.html", _paths=sorted_meta_data)
@app.route('/blog/<blog_item>/index.html')
def blog(blog_item, _date=meta_data):
return render_template(f"blog/{blog_item}/index.html", _date=meta_data[blog_item])
@app.route("/about.html")
def about():
return render_template("about.html")
@app.route("/contact.html")
def contact():
return render_template("contact.html")
@app.route("/rss.xml")
def rss(_items=meta_data):
#rss_feed = []
_tz = pytz.timezone('Europe/Berlin')
_fg = FeedGenerator()
_fg.title("Website of Stefan Friese")
_fg.description("test")
_fg.language("en-us")
#_fg.author({'name': "Stefan Friese", 'email': 'stefan@stefan.works'})
_fg.link( href="https://stefan.works", rel="self")
for key in meta_data.keys():
_fe = _fg.add_entry()
_fe.id(f"https://stefan.works/blog/{key}/index.html")
_fe.title(key)
#_fe.description("test")
#_fe.author({'name': "Stefan Friese", 'email': 'stefan@stefan.works'})
_fe.link(href=f"https://stefan.works/blog/{key}/index.html")
_fe.pubDate(pubDate=_tz.localize(meta_data[key]))
_fg.rss_str(pretty=True)
_fg.rss_file('./static/rss.xml')
return send_from_directory(os.path.join(app.root_path, 'static'), 'rss.xml')
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico')
@app.errorhandler(404)
def page_not_found(_error):
return render_template("/status_code/404.html"), 404
@app.errorhandler(400)
def bad_request(_error):
return render_template("/status_code/400.html"), 400
@app.errorhandler(500)
def internal_server_error(_error):
return render_template("/status_code/500.html"), 500
with app.test_request_context():
print(url_for("index"))
print(url_for("about"))
print(url_for("contact"))
print(url_for("rss"))
print(url_for("static", filename="stylesheet.css"))
print(meta_data)
print(url_for("blog", blog_item="first_blog"))