Files
menu/examples/ytfeed.aggregate

27 lines
1.2 KiB
Python
Executable File

#!/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")
macrofeed = ET.Element('feed')
macrofeed.extend(
sorted([
entry
# Split the input by file. This relies on a typical <XML /> tag
# starting a file. Theoretically this assumption could be wrong,
# but in that case the XML parser would break before
# ytfeed.aggregate could spit malformed output.
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 # (this is a text sort but it still works)
: entry.findall("{http://www.w3.org/2005/Atom}published")[0].text,
)
)
print(ET.tostring(macrofeed, encoding="unicode"))