Last active
October 31, 2018 14:35
-
-
Save molobrakos/e10c0945ae77eadb9e266ef2491edd15 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/python3 | |
""" | |
Convert OPML file exported from Pocketcasts to YAML format used by | |
the podcasts-plugin in LMS | |
Usage: | |
opml2sb.py <opml-file> <lms-basedir> | |
Example | |
opml2sb.py podcasts_opml.xml /path/to/lms | |
""" | |
import sys | |
from os.path import join | |
from pprint import pprint | |
from yaml import (safe_load as load_yaml, | |
safe_dump as dump_yaml) | |
TARGET='config/plugin/podcast.prefs' | |
src, lms = sys.argv[1:] | |
target=join(lms, TARGET) | |
with open(target) as f: | |
feeds = load_yaml(f) | |
print('Loaded {} feeds from squeezeserver'.format(len(feeds['feeds']))) | |
import xml.etree.ElementTree as ET | |
root = ET.parse(src) | |
podcasts = [dict(name=elm.get('text'), value=elm.get('xmlUrl')) | |
for elm in root.findall('.//outline[@type="rss"]')] | |
result = feeds['feeds'] + podcasts | |
# transform into dict to remove duplicated by overwriting any feed with same name | |
result = { feed['name']: feed['value'] for feed in result } | |
print('Merged into {} feeds'.format(len(result))) | |
# transform back to list of dicts | |
result = [dict(name=name, value=value) for name, value in result.items() ] | |
# sort by name | |
result = sorted(result, key=lambda x: x['name']) | |
feeds['feeds'] = result | |
with open(target, 'w') as f: | |
dump_yaml(feeds, f, encoding='utf-8', default_flow_style=False) | |
print('done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment