dotfiles/bin/convert-opml-to-newsboat-urls

23 lines
818 B
Plaintext
Raw Normal View History

2021-04-16 15:25:24 +00:00
#!/usr/bin/env python
# TODO:
# Given a OPML file, convert the OPML into a `urls` file compatible for newsboat.
# It could also accept an OPML through stdin.
import itertools
2021-07-22 11:24:53 +00:00
import fileinput
2021-04-16 15:25:24 +00:00
from pathlib import Path
import sys
import xml.etree.ElementTree as ET
2021-07-22 11:24:53 +00:00
if len(sys.argv) > 1:
opml = ET.parse(sys.argv[1])
else:
opml = ET.fromstring(fileinput.input())
2021-04-16 15:25:24 +00:00
for outline in opml.findall("body/outline"):
categories = [category.strip("/") for category in outline.get("category", "").split(",") if category]
print('{xmlUrl} "~{text}" {category}'.format(xmlUrl = outline.get("xmlUrl"),
text = outline.get("text"),
category = " ".join([ f'"{tag}"' for tag in categories ]) if len(categories) else " "))