Parsing Chrome bookmarks

November 6, 2018

Recently I decided to keep on my site a list of links to interesting sites around the net. You can find this section under “Links” on the top bar.

I have been keeping track of such links mostly through Chrome’s bookmark function. So I wrote a very simple Python program to parse the Chrome bookmarks file, extract links according to a regex, and write those links to a file formatted as a markdown list. In retrospect I also should have added some de-duplication logic, as I had to do that manually.

Maybe someone will come across this post and it will save them a couple of minutes. Or maybe they will say “Yuck, I’ll do it myself” :-)

import re

def read(path):
    with open(path, 'r') as f:
        data = f.read()
    print('Read file')
    return data


def parse(filename):
    # do something
    data = read(filename)
    urls = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', data)
    print('Extracted URLs')
    return urls


def write(list):
    with open('/file/to/write/to', 'a') as f:
        print('Opened for writing')
        for url in list:
            entry = '* {}\n'.format(url)
            f.write(entry)
            print('Wrote entry')


if __name__ == "__main__":
    write(parse('/Users/Username/Library/Application Support/Google/Chrome/Default/Bookmarks'))