Skip to content

Instantly share code, notes, and snippets.

@kamysheblid
Created May 30, 2021 03:13
Show Gist options
  • Save kamysheblid/f6fe9285be7301f0f609fa8fce4b92f1 to your computer and use it in GitHub Desktop.
Save kamysheblid/f6fe9285be7301f0f609fa8fce4b92f1 to your computer and use it in GitHub Desktop.
Python3 script to convert feedbro.opml to elfeed.org
import xml.etree.ElementTree as ET
from pathlib import Path
from sys import exit
import logging, argparse
#Parse args
parser = argparse.ArgumentParser(description='Python script to convert from feedbro RSS opml format to elfeed org format')
parser.add_argument('-v',action='count',default=0,help='Verbose output. Do -vv to get debug output')
parser.add_argument('file',help='Filename for input')
parser.add_argument('--force','-f',action='store_true',help='Overwrite elfeed.opml without asking')
parser.add_argument('--output','-o',metavar='out',default='elfeed.org',help='Output file')
args = parser.parse_args()
#Handle Logging
if args.v == 0:
level = logging.ERROR
elif args.v == 1:
level = logging.INFO
else:
level = logging.DEBUG
logging.basicConfig(level=level)
logging.info(args)
#Get a etree obj and parse it recursively
def parse(elt,level=2):
temp_string = ''
if elt.items() == []:
for child in elt:
temp_string += parse(child)
if len(elt) == 0:
temp_string += '*'*level+f' [[{elt.get("xmlUrl")}][{elt.get("title")}]]\n'
logging.debug(f'Found: {temp_string}')
elif len(elt) > 0:
temp_string += '*'*level+f' {elt.get("title")} :{elt.get("title")}:\n'
logging.debug(f'Found {temp_string}')
for child in elt:
logging.debug('going down a level')
temp_string += parse(child, level+1)
return temp_string
def main():
# Find the input feedbro file, if it doesnt exist then fail
feedbro_file = Path(args.file)
if not feedbro_file.exists():
logging.ERROR(f"File Not Found Error: {feedbro_file}")
exit(1)
# parse the feedbro file
tree = ET.parse('feedbro.opml')
body = tree.getroot()[1]
string = '* Blogs :elfeed:\n'
string += parse(body)
#Write the elfeed.org file and check if it should be overwritten
out = Path('elfeed.org')
if not args.force and (out.exists() and input('elfeed.org already exists, overwrite? (y/N)') != 'y'):
exit(0)
with out.open('w') as f:
f.write(string)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment