diff options
author | Stonewall Jackson <stonewall@sacredheartsc.com> | 2023-01-22 09:56:17 -0500 |
---|---|---|
committer | Stonewall Jackson <stonewall@sacredheartsc.com> | 2023-01-22 09:56:17 -0500 |
commit | 9f1b37fa346a7dcf77c1b6963a6d2e4b871fe5ed (patch) | |
tree | 14aa09de866a3283e1b07a94ea9cc6d70fc3e49d /scripts | |
download | www-9f1b37fa346a7dcf77c1b6963a6d2e4b871fe5ed.tar.gz www-9f1b37fa346a7dcf77c1b6963a6d2e4b871fe5ed.zip |
initial commit
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/bloglist.py | 26 | ||||
-rw-r--r-- | scripts/common.py | 53 | ||||
-rwxr-xr-x | scripts/rss.py | 48 |
3 files changed, 127 insertions, 0 deletions
diff --git a/scripts/bloglist.py b/scripts/bloglist.py new file mode 100755 index 0000000..9e10354 --- /dev/null +++ b/scripts/bloglist.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import argparse +from common import get_blog_posts + +DATE_FORMAT = '%Y-%m-%d' + +parser = argparse.ArgumentParser('bloglist') +parser.add_argument('BLOG_DIR', type=str, help='Directory containing markdown blog posts') +parser.add_argument('LIMIT', nargs='?', default=None, type=int, help='Maximum number of posts to show') +args = parser.parse_args() + +posts = get_blog_posts(args.BLOG_DIR) + +if args.LIMIT is not None: + posts = posts[0:args.LIMIT] + +if len(posts) == 0: + print('Nothing has been posted yet!') +else: + for post in posts: + post_date = post['date'].strftime(DATE_FORMAT) + + print(f'- [{post["title"]}]({post["href"]}) ({post_date})\n') + if post['description']: + print(f' {post["description"]}\n') diff --git a/scripts/common.py b/scripts/common.py new file mode 100644 index 0000000..9b23816 --- /dev/null +++ b/scripts/common.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import os +import frontmatter +import dateparser +import datetime +from pathlib import Path + +BLOG_LIST_FILE = '.bloglist.md' + +def get_href(path): + path = Path(path) + path = path.relative_to(*path.parts[:1]) + if path.name == 'index.md': + return '/{}/'.format(path.parent) + else: + return '/{}/{}.html'.format(path.parent, path.stem) + +def read_metadata(file): + post = frontmatter.load(file) + + for field in ['title', 'date']: + if post.get(field) is None: + raise Exception("{} is missing metadata field '{}'".format(file, field)) + + if type(post['date']) not in [datetime.datetime, datetime.date]: + date = dateparser.parse(post['date']) + else: + date = post['date'] + + return { + 'title': post.get('title'), + 'date': date, + 'author': post.get('author'), + 'description': post.get('description'), + 'draft': post.get('draft'), + 'href': get_href(file) + } + +def get_blog_posts(blog_dir): + blog_index = os.path.join(blog_dir, 'index.md') + posts = [] + + for root, dirs, files in os.walk(blog_dir): + for file in files: + path = os.path.join(root, file) + if path.endswith('.md') and path != blog_index and file != BLOG_LIST_FILE: + metadata = read_metadata(path) + if not metadata['draft']: + posts.append(metadata) + + posts.sort(key=lambda p: (p is None, p['date']), reverse=True) + return posts diff --git a/scripts/rss.py b/scripts/rss.py new file mode 100755 index 0000000..cf0cb9a --- /dev/null +++ b/scripts/rss.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 + +import argparse +import email.utils +from datetime import datetime +from common import get_blog_posts + +parser = argparse.ArgumentParser('rss') +parser.add_argument('BLOG_DIR', type=str, help='Directory containing markdown blog posts') +parser.add_argument('--limit', default=15, type=int, help='Maximum number of posts to show') +parser.add_argument('--title', help='Feed title', required=True) +parser.add_argument('--description', help='Feed description', required=True) +parser.add_argument('--url', help='Root URL', required=True) +parser.add_argument('--blog-path', help='Blog path', required=True) +parser.add_argument('--feed-path', help='RSS feed path', required=True) +args = parser.parse_args() + +posts = get_blog_posts(args.BLOG_DIR) +posts = posts[0:args.limit] + +build_date = email.utils.format_datetime(datetime.now().astimezone()) + +print(f'''<?xml version="1.0"?> +<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> +<channel> + <title>{args.title}</title> + <link>{args.url}{args.blog_path}</link> + <language>en-US</language> + <description>{args.description}</description> + <lastBuildDate>{build_date}</lastBuildDate> + <atom:link href="{args.url}{args.feed_path}" rel="self" type="application/rss+xml"/>''') + +for post in posts: + pub_date = email.utils.format_datetime(post['date'].astimezone()) + + print(f''' <item> + <title>{post["title"]}</title> + <link>{args.url}{post["href"]}</link> + <guid>{args.url}{post["href"]}</guid> + <pubDate>{pub_date}</pubDate>''') + + if 'description' in post: + print(f' <description>{post["description"]}</description>') + + print(' </item>') + +print('</channel>') +print('</rss>') |