summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gemfeed.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/gemfeed.py b/gemfeed.py
new file mode 100644
index 0000000..42d23d7
--- /dev/null
+++ b/gemfeed.py
@@ -0,0 +1,47 @@
+import datetime
+import glob
+import os.path
+
+from feedgen.feed import FeedGenerator
+
+base_url = "gemini://gemini.circumlunar.space/"
+
+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()
+ 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("#"):
+ while line[0] == "#":
+ line = line[1:]
+ title = line.strip()
+ 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)