diff options
Diffstat (limited to 'gemfeed.py')
-rw-r--r-- | gemfeed.py | 84 |
1 files changed, 48 insertions, 36 deletions
@@ -90,11 +90,52 @@ def populate_entry_from_file(filename, base_url, entry): title = extract_first_heading(filename, filename) entry.title(title) -def main(): +def build_feed(base_url, output="atom.xml", n=10, title="", subtitle="", author="", email=""): + """ + Build an Atom feed for all world readable Gemini files in the current + directory, and write it to atom.xml. + """ + # If a title hasn't been provided, try to get one from an index page + if not title: + title = get_feed_title() - # Get default title from index page, if there is one - feed_title = get_feed_title() + # Setup feed + feed = FeedGenerator() + feed.id(base_url) + feed.title(title) + if subtitle: + feed.subtitle(subtitle) + author_details = {} + if author: + author_details["name"] = author + if email: + author_details["email"] = email + if author_details: + feed.author(author_details) + feed.link(href=base_url, rel='alternate') + feed.link(href=urljoin(base_url, output), rel='self') + # Add one entry per .gmi file + files = find_files(n) + if not files: + print("No world-readable Gemini content found! :(") + return + for n, filename in enumerate(files): + entry = feed.add_entry() + populate_entry_from_file(filename, base_url, entry) + print("Adding {} with title '{}'...".format(filename, entry.title())) + if n == 0: + feed.updated(entry.updated()) + + # Write file + feed.atom_file(output, pretty=True) + print("Wrote Atom feed to {}.".format(output)) + +def main(): + """ + Parse command line arguments, do some minor processing, and then invoke + the build_feed command with the provided settings. + """ # Parse arguments parser = argparse.ArgumentParser(description='Generate an Atom feed for Gemini content.') parser.add_argument('-a', '--author', dest='author', type=str, @@ -110,7 +151,7 @@ def main(): parser.add_argument('-s', '--subtitle', dest='subtitle', type=str, help='feed subtitle') parser.add_argument('-t', '--title', dest='title', type=str, - default=feed_title, help='feed title') + help='feed title') args = parser.parse_args() # Normalise base URL @@ -121,38 +162,9 @@ def main(): base_url = base_url._replace(scheme="gemini") args.base_url = urllib.parse.urlunsplit(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=urljoin(args.base_url, args.output), rel='self') - - # Add one entry per .gmi file - files = find_files(args.n) - if not files: - print("No world-readable Gemini content found! :(") - return - - for n, filename in enumerate(files): - entry = feed.add_entry() - populate_entry_from_file(filename, args.base_url, entry) - print("Adding {} with title '{}'...".format(filename, entry.title())) - if n == 0: - feed.updated(entry.updated()) - - # Write file - feed.atom_file(args.output, pretty=True) - print("Wrote Atom feed to {}.".format(args.output)) + # Build the feed + build_feed(args.base_url, args.output, args.n, args.title, args.subtitle, + args.author, args.email) if __name__ == "__main__": main() |