#!/usr/bin/env python3 # 2025 dtb. public domain import re, sys, xml.etree.ElementTree as ET ET.register_namespace("", "http://www.w3.org/2005/Atom") ET.register_namespace("media", "http://search.yahoo.com/mrss/") ET.register_namespace("yt", "http://www.youtube.com/xml/schemas/2015") def sortby(entry): return entry.findall("{http://www.w3.org/2005/Atom}published")[0].text macrofeed = ET.Element('feed') macrofeed.extend( sorted([ entry # Split the input by file. for feed in re.split(r'<\?xml.*?\?>', sys.stdin.read())[1:] # Get the entries out of each file. for entry in ET.fromstring(feed) .findall("{http://www.w3.org/2005/Atom}entry") ], # Gets the publication date and sorts the entries from old to new. key = lambda entry : entry.findall("{http://www.w3.org/2005/Atom}published")[0].text, reverse = False # toggle this to switch the order ) ) print(ET.tostring(macrofeed, encoding="unicode"))