From 2c58b4a576324753e67221ddb1e72e962990053b Mon Sep 17 00:00:00 2001 From: Solderpunk Date: Tue, 7 Jul 2020 14:00:18 +0200 Subject: Add --mtime option to use file modification time, not creation/update time, as that can be manually set via touch. --- gemfeed.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/gemfeed.py b/gemfeed.py index 7143263..93d5886 100755 --- a/gemfeed.py +++ b/gemfeed.py @@ -50,7 +50,7 @@ def get_feed_title(directory): return extract_first_heading(index_file, default) return default -def find_files(directory, n=10): +def find_files(directory, time_func, 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. @@ -63,7 +63,7 @@ def find_files(directory, n=10): if index in files: files.remove(index) files = [f for f in files if is_world_readable(f)] - files.sort(key=os.path.getctime, reverse=True) + files.sort(key=time_func, reverse=True) return files[0:n] def urljoin(base, url): @@ -85,7 +85,7 @@ def urljoin(base, url): joined = joined._replace(scheme="gemini") return urllib.parse.urlunsplit(joined) -def populate_entry_from_file(filename, base_url, entry): +def populate_entry_from_file(filename, base_url, entry, time_func): """ Set the id, title, updated and link attributes of the provided FeedGenerator entry object according the contents of the named @@ -94,13 +94,13 @@ def populate_entry_from_file(filename, base_url, entry): url = urljoin(base_url, os.path.basename(filename)) entry.guid(url) entry.link(href=url, rel="alternate") - updated = get_update_time(filename) + updated = get_update_time(filename, time_func) entry.updated(updated) default_title = os.path.splitext(os.path.basename(filename))[0] title = extract_first_heading(filename, default_title) entry.title(title) -def get_update_time(filename): +def get_update_time(filename, time_func): """ Return an update time for a Gemini file. @@ -115,11 +115,11 @@ def get_update_time(filename): date = basename[0:10] + " Z" # Add UTC marker return datetime.datetime.strptime(date, "%Y-%m-%d %z") else: - updated = os.path.getctime(filename) + updated = time_func(filename) return datetime.datetime.fromtimestamp(updated, tz=datetime.timezone.utc) -def build_feed(directory, base_url, output="atom.xml", n=10, title="", - subtitle="", author="", email="", verbose=False): +def build_feed(directory, time_func, 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. @@ -150,14 +150,14 @@ def build_feed(directory, base_url, output="atom.xml", n=10, title="", feed.link(href=base_url, rel='alternate') # Add one entry per .gmi file - files = find_files(directory, n) + files = find_files(directory, time_func, n) if not files: if verbose: 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) + populate_entry_from_file(filename, base_url, entry, time_func) if n == 0: feed.updated(entry.updated()) if verbose: @@ -199,6 +199,8 @@ def main(): help='feed subtitle') parser.add_argument('-t', '--title', dest='title', type=str, help='feed title') + parser.add_argument('--mtime', action="store_true", + help='Use file modification time, not file update time, in feeds') args = parser.parse_args() # Normalise base URL @@ -212,8 +214,10 @@ def main(): args.base_url += "/" # Build the feed - build_feed(args.directory, args.base_url, args.output, args.n, args.title, - args.subtitle, args.author, args.email, args.verbose) + time_function = os.path.getmtime if args.mtime else os.path.getctime + build_feed(args.directory, time_function, args.base_url, args.output, + args.n, args.title, args.subtitle, args.author, args.email, + args.verbose) if __name__ == "__main__": main() -- cgit v1.2.3-54-g00ecf