Skip to content

Instantly share code, notes, and snippets.

@hghwng
Created August 7, 2016 12:21
Show Gist options
  • Select an option

  • Save hghwng/324cc28b007a8f650ce3aac5df099ef8 to your computer and use it in GitHub Desktop.

Select an option

Save hghwng/324cc28b007a8f650ce3aac5df099ef8 to your computer and use it in GitHub Desktop.

Revisions

  1. hghwng created this gist Aug 7, 2016.
    29 changes: 29 additions & 0 deletions opmlconv.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/usr/bin/env python
    import bs4


    def convert_element(lines, level=1):
    result = ''
    for line in lines:
    if not isinstance(line, bs4.element.Tag) or \
    line.name != 'outline':
    continue
    result += '*' * level + ' ' + line.attrs.get('text', '') + '\n'
    if 'note' in line.attrs:
    result += line.attrs['note'].replace('\r', '\n') + '\n'
    result += convert_element(line.children, level + 1)
    return result


    def convert_file(path):
    root = bs4.BeautifulSoup(open(path), "lxml")
    return convert_element(root.select('html body opml')[0])

    def main():
    import sys
    output_path = sys.argv[1][:-4] + 'org'
    result = convert_file(sys.argv[1])
    open(output_path, 'w').write(result)

    if __name__ == '__main__':
    main()