summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gemfeed.py33
1 files changed, 22 insertions, 11 deletions
diff --git a/gemfeed.py b/gemfeed.py
index 23ca0aa..e85d532 100644
--- a/gemfeed.py
+++ b/gemfeed.py
@@ -29,7 +29,7 @@ def extract_first_heading(filename, default=""):
return line.strip()
return default
-def get_feed_title():
+def get_feed_title(directory):
"""
If an index.gmi or index.gemini file exists and is worldreadable, return
the content of the first heading line in the file, otherwise return a
@@ -37,18 +37,20 @@ def get_feed_title():
"""
default = "Just another Gemini feed"
for index_file in ("index.gmi", "index.gemini"):
+ index_file = os.path.join(directory, index_file)
if os.path.exists(index_file) and is_world_readable(index_file):
return extract_first_heading(index_file, default)
return default
-def find_files(n=10):
+def find_files(directory, n=10):
"""
Return the n most recently created world readable files with extensions of
.gmi or .gemini, as a list sorted from most to least recent.
"""
files = []
for extension in ("gmi", "gemini"):
- files.extend(glob.glob("*.{}".format(extension)))
+ glob_pattern = os.path.join(directory, "*.{}".format(extension))
+ files.extend(glob.glob(glob_pattern))
index = "index.{}".format(extension)
if index in files:
files.remove(index)
@@ -87,18 +89,19 @@ def populate_entry_from_file(filename, base_url, entry):
updated = os.path.getctime(filename)
updated = datetime.datetime.fromtimestamp(updated, tz=datetime.timezone.utc)
entry.updated(updated)
- title = extract_first_heading(filename, filename)
+ default_title = os.path.splitext(os.path.basename(filename))[0]
+ title = extract_first_heading(filename, default_title)
entry.title(title)
-def build_feed(base_url, output="atom.xml", n=10, title="", subtitle="",
- author="", email="", verbose=False):
+def build_feed(directory, base_url, output="atom.xml", n=10, title="",
+ subtitle="", author="", email="", verbose=False):
"""
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()
+ title = get_feed_title(directory)
# Setup feed
feed = FeedGenerator()
@@ -117,7 +120,7 @@ def build_feed(base_url, output="atom.xml", n=10, title="", subtitle="",
feed.link(href=urljoin(base_url, output), rel='self')
# Add one entry per .gmi file
- files = find_files(n)
+ files = find_files(directory, n)
if not files:
if verbose:
print("No world-readable Gemini content found! :(")
@@ -128,9 +131,11 @@ def build_feed(base_url, output="atom.xml", n=10, title="", subtitle="",
if n == 0:
feed.updated(entry.updated())
if verbose:
- print("Adding {} with title '{}'...".format(filename, entry.title()))
+ print("Adding {} with title '{}'...".format(os.path.basename(filename),
+ entry.title()))
# Write file
+ output = os.path.join(directory, output)
feed.atom_file(output, pretty=True)
if verbose:
print("Wrote Atom feed to {}.".format(output))
@@ -140,12 +145,18 @@ def main():
Parse command line arguments, do some minor processing, and then invoke
the build_feed command with the provided settings.
"""
+
+ # Get cwd as default value for --directory
+ cwd = os.getcwd()
+
# 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('-d', '--directory', dest='directory', type=str,
+ default=cwd, help='directory to find content and save feed to')
parser.add_argument('-e', '--email', dest='email', type=str,
help="feed author's email address")
parser.add_argument('-n', dest='n', type=int, default=10,
@@ -169,8 +180,8 @@ def main():
args.base_url = urllib.parse.urlunsplit(base_url)
# Build the feed
- build_feed(args.base_url, args.output, args.n, args.title, args.subtitle,
- args.author, args.email, args.verbose)
+ build_feed(args.directory, args.base_url, args.output, args.n, args.title,
+ args.subtitle, args.author, args.email, args.verbose)
if __name__ == "__main__":
main()