summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gemfeed.py57
1 files changed, 34 insertions, 23 deletions
diff --git a/gemfeed.py b/gemfeed.py
index 42d23d7..5943dd3 100644
--- a/gemfeed.py
+++ b/gemfeed.py
@@ -4,33 +4,22 @@ import os.path
from feedgen.feed import FeedGenerator
-base_url = "gemini://gemini.circumlunar.space/"
+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
-feed = FeedGenerator()
-feed.id(base_url)
-feed.title('A Gemini feed.')
-feed.link( href=base_url, rel='alternate' )
-feed.link( href=base_url + "atom.xml", rel='self' )
-
-files = []
-for extension in ("gmi", "gem", "gemini"):
- files.extend(glob.glob("*.{}".format(extension)))
- index = "index.{}".format(extension)
- if index in files:
- files.remove(index)
-
-latest_update = datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc)
-
-for filename in files:
- entry = feed.add_entry()
+def populate_entry_from_file(filename, base_url, entry):
url = base_url + filename
entry.guid(url)
entry.link(href=url)
updated = os.path.getctime(filename)
updated = datetime.datetime.fromtimestamp(updated, tz=datetime.timezone.utc)
entry.updated(updated)
- if updated > latest_update:
- latest_update = updated
with open(filename) as fp:
for line in fp:
if line.startswith("#"):
@@ -40,8 +29,30 @@ for filename in files:
break
else:
title = filename
- print("Adding {} with title '{}'...".format(filename, title))
entry.title(title)
-feed.updated(latest_update)
-feed.atom_file('atom.xml', pretty=True)
+def main(base_url="gemini://gemini.circumlunar.space/"):
+
+ # Setup feed
+ feed = FeedGenerator()
+ feed.id(base_url)
+ feed.title('A Gemini feed.')
+ feed.link(href=base_url, rel='alternate')
+ feed.link(href=base_url + "atom.xml", 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, 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('atom.xml', pretty=True)
+
+if __name__ == "__main__":
+ main()