mirror of
https://tildegit.org/solderpunk/gemfeed
synced 2024-05-20 10:26:04 +02:00
Add --mtime option to use file modification time, not creation/update time, as that can be manually set via touch.
This commit is contained in:
parent
91d6d075bc
commit
2c58b4a576
28
gemfeed.py
28
gemfeed.py
|
@ -50,7 +50,7 @@ def get_feed_title(directory):
|
||||||
return extract_first_heading(index_file, default)
|
return extract_first_heading(index_file, default)
|
||||||
return 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
|
Return the n most recently created world readable files with extensions of
|
||||||
.gmi or .gemini, as a list sorted from most to least recent.
|
.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:
|
if index in files:
|
||||||
files.remove(index)
|
files.remove(index)
|
||||||
files = [f for f in files if is_world_readable(f)]
|
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]
|
return files[0:n]
|
||||||
|
|
||||||
def urljoin(base, url):
|
def urljoin(base, url):
|
||||||
|
@ -85,7 +85,7 @@ def urljoin(base, url):
|
||||||
joined = joined._replace(scheme="gemini")
|
joined = joined._replace(scheme="gemini")
|
||||||
return urllib.parse.urlunsplit(joined)
|
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
|
Set the id, title, updated and link attributes of the provided
|
||||||
FeedGenerator entry object according the contents of the named
|
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))
|
url = urljoin(base_url, os.path.basename(filename))
|
||||||
entry.guid(url)
|
entry.guid(url)
|
||||||
entry.link(href=url, rel="alternate")
|
entry.link(href=url, rel="alternate")
|
||||||
updated = get_update_time(filename)
|
updated = get_update_time(filename, time_func)
|
||||||
entry.updated(updated)
|
entry.updated(updated)
|
||||||
default_title = os.path.splitext(os.path.basename(filename))[0]
|
default_title = os.path.splitext(os.path.basename(filename))[0]
|
||||||
title = extract_first_heading(filename, default_title)
|
title = extract_first_heading(filename, default_title)
|
||||||
entry.title(title)
|
entry.title(title)
|
||||||
|
|
||||||
def get_update_time(filename):
|
def get_update_time(filename, time_func):
|
||||||
"""
|
"""
|
||||||
Return an update time for a Gemini file.
|
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
|
date = basename[0:10] + " Z" # Add UTC marker
|
||||||
return datetime.datetime.strptime(date, "%Y-%m-%d %z")
|
return datetime.datetime.strptime(date, "%Y-%m-%d %z")
|
||||||
else:
|
else:
|
||||||
updated = os.path.getctime(filename)
|
updated = time_func(filename)
|
||||||
return datetime.datetime.fromtimestamp(updated, tz=datetime.timezone.utc)
|
return datetime.datetime.fromtimestamp(updated, tz=datetime.timezone.utc)
|
||||||
|
|
||||||
def build_feed(directory, base_url, output="atom.xml", n=10, title="",
|
def build_feed(directory, time_func, base_url, output="atom.xml", n=10,
|
||||||
subtitle="", author="", email="", verbose=False):
|
title="", subtitle="", author="", email="", verbose=False):
|
||||||
"""
|
"""
|
||||||
Build an Atom feed for all world readable Gemini files in the current
|
Build an Atom feed for all world readable Gemini files in the current
|
||||||
directory, and write it to atom.xml.
|
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')
|
feed.link(href=base_url, rel='alternate')
|
||||||
|
|
||||||
# Add one entry per .gmi file
|
# Add one entry per .gmi file
|
||||||
files = find_files(directory, n)
|
files = find_files(directory, time_func, n)
|
||||||
if not files:
|
if not files:
|
||||||
if verbose:
|
if verbose:
|
||||||
print("No world-readable Gemini content found! :(")
|
print("No world-readable Gemini content found! :(")
|
||||||
return
|
return
|
||||||
for n, filename in enumerate(files):
|
for n, filename in enumerate(files):
|
||||||
entry = feed.add_entry()
|
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:
|
if n == 0:
|
||||||
feed.updated(entry.updated())
|
feed.updated(entry.updated())
|
||||||
if verbose:
|
if verbose:
|
||||||
|
@ -199,6 +199,8 @@ def main():
|
||||||
help='feed subtitle')
|
help='feed subtitle')
|
||||||
parser.add_argument('-t', '--title', dest='title', type=str,
|
parser.add_argument('-t', '--title', dest='title', type=str,
|
||||||
help='feed title')
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Normalise base URL
|
# Normalise base URL
|
||||||
|
@ -212,8 +214,10 @@ def main():
|
||||||
args.base_url += "/"
|
args.base_url += "/"
|
||||||
|
|
||||||
# Build the feed
|
# Build the feed
|
||||||
build_feed(args.directory, args.base_url, args.output, args.n, args.title,
|
time_function = os.path.getmtime if args.mtime else os.path.getctime
|
||||||
args.subtitle, args.author, args.email, args.verbose)
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue