#!/usr/bin/env python3
import argparse
import re
from datetime import datetime
updated_pattern = r"(?P.{25})"
datetime_pattern = "%Y-%m-%dT%H:%M:%S%z"
def get_post_datetimes(feed):
regex = re.compile(updated_pattern)
datetimes = list()
first_skipped = False
for match in regex.finditer(feed):
# Skip first found datetime, as this is what we want to replace.
if not first_skipped:
first_skipped = True
continue
dt = datetime.strptime(match.group("datetime"), datetime_pattern)
datetimes.append(dt)
return datetimes
def main():
parser = argparse.ArgumentParser(
description="Patch the updated date field on an Atom feed."
)
parser.add_argument(
"--file", "-f", type=str, required=True, help="Path to the Atom feed file."
)
args = parser.parse_args()
with open(args.file, "r") as f:
feed = f.read()
datetimes = get_post_datetimes(feed)
newest_datetime = max(datetimes)
print(
re.sub(
updated_pattern,
f"{newest_datetime.strftime(datetime_pattern)}",
feed,
count=1,
),
end="",
)
if __name__ == "__main__":
main()