mirror of
https://tildegit.org/solderpunk/gemfeed
synced 2024-11-08 19:59:22 +01:00
92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
import argparse
|
|
import datetime
|
|
import glob
|
|
import os.path
|
|
|
|
from feedgen.feed import FeedGenerator
|
|
|
|
def find_files():
|
|
files = []
|
|
for extension in ("gmi", "gem", "gemini"):
|
|
files.extend(glob.glob("*.{}".format(extension)))
|
|
index = "index.{}".format(extension)
|
|
if index in files:
|
|
files.remove(index)
|
|
return files
|
|
|
|
def populate_entry_from_file(filename, base_url, entry):
|
|
url = base_url + filename
|
|
entry.guid(url)
|
|
entry.link(href=url, rel="alternate")
|
|
updated = os.path.getctime(filename)
|
|
updated = datetime.datetime.fromtimestamp(updated, tz=datetime.timezone.utc)
|
|
entry.updated(updated)
|
|
with open(filename) as fp:
|
|
for line in fp:
|
|
if line.startswith("#"):
|
|
while line[0] == "#":
|
|
line = line[1:]
|
|
title = line.strip()
|
|
break
|
|
else:
|
|
title = filename
|
|
entry.title(title)
|
|
|
|
def main():
|
|
|
|
# Parse arguments
|
|
parser = argparse.ArgumentParser(description='Generate an Atom feed for Gemini content.')
|
|
parser.add_argument('-a', '--author', dest='author', type=str,
|
|
help="feed author's name")
|
|
parser.add_argument('-b', '--base', dest='base_url', type=str,
|
|
required=True, help='base URL for feed and entries')
|
|
parser.add_argument('-e', '--email', dest='email', type=str,
|
|
help="feed author's email address")
|
|
parser.add_argument('-o', '--output', dest='output', type=str,
|
|
default="atom.xml", help='output filename')
|
|
parser.add_argument('-s', '--subtitle', dest='subtitle', type=str,
|
|
help='feed subtitle')
|
|
parser.add_argument('-t', '--title', dest='title', type=str,
|
|
default="A Gemini feed", help='feed title')
|
|
args = parser.parse_args()
|
|
|
|
# Normalise base URL, quick and dirty
|
|
if not args.base_url.startswith("gemini://"):
|
|
args.base_url = "gemini://" + args.base_url
|
|
if not args.base_url.endswith("/"):
|
|
args.base_url = args.base_url + "/"
|
|
|
|
# Setup feed
|
|
feed = FeedGenerator()
|
|
feed.id(args.base_url)
|
|
feed.title(args.title)
|
|
if args.subtitle:
|
|
feed.subtitle(args.subtitle)
|
|
author = {}
|
|
if args.author:
|
|
author["name"] = args.author
|
|
if args.email:
|
|
author["email"] = args.email
|
|
if author:
|
|
feed.author(author)
|
|
feed.link(href=args.base_url, rel='alternate')
|
|
feed.link(href=args.base_url + args.output, rel='self')
|
|
|
|
# Add one entry per .gmi file
|
|
files = find_files()
|
|
latest_update = datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc)
|
|
for filename in files:
|
|
entry = feed.add_entry()
|
|
populate_entry_from_file(filename, args.base_url, entry)
|
|
print("Adding {} with title '{}'...".format(filename, entry.title()))
|
|
if entry.updated() > latest_update:
|
|
latest_update = entry.updated()
|
|
feed.updated(latest_update)
|
|
|
|
# Write file
|
|
feed.atom_file(args.output, pretty=True)
|
|
print("Wrote Atom feed to {}.".format(args.output))
|
|
|
|
if __name__ == "__main__":
|
|
main()
|