Created
June 26, 2021 21:49
-
-
Save whilp/32eed89a78a54c59bc6dd074360a2785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Escape from Apple Podcasts. | |
# python3 podcasts.py ~/Library/Containers/com.apple.podcasts/Data/Documents/PodcastsDB.plist > ~/Downloads/export.opml | |
import os | |
import sys | |
import typing as t | |
import xml.etree.ElementTree as ET | |
def main(args: t.List[str], env: t.Dict[str, str]) -> None: | |
tree = ET.parse(args[1]) | |
root = tree.getroot() | |
opml = ET.Element("opml") | |
opml.set("version", "1.0") | |
head = ET.SubElement(opml, "head") | |
title = ET.SubElement(head, "title") | |
title.text = "Apple Podcast Export" | |
body = ET.SubElement(opml, "body") | |
for item in items(root): | |
title = item.get("title") | |
url = item.get("feedUrl") | |
if None in (title, url): | |
continue | |
outline = ET.SubElement(body, "outline") | |
outline.set("type", "rss") | |
outline.set("text", title) | |
outline.set("title", title) | |
outline.set("xmlUrl", url) | |
outline.set("htmlUrl", url) | |
sys.stdout.write(ET.tostring(opml, encoding="utf8", method="xml").decode("utf-8")) | |
def items(root): | |
for item in root.findall("./array/dict/array/dict/array/dict"): | |
yield asdict(item) | |
def asdict(element): | |
items = [x.text for x in element] | |
return dict(zip(items[::2], items[1::2])) | |
if __name__ == "__main__": | |
sys.exit(main(list(sys.argv), dict(os.environ))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment