Skip to content

Instantly share code, notes, and snippets.

@christianjunk
Forked from mpraglowski/readinglist.py
Last active August 30, 2018 15:02
Show Gist options
  • Save christianjunk/d6f67cf3ab246ef9a8468ef2d9edf8a6 to your computer and use it in GitHub Desktop.
Save christianjunk/d6f67cf3ab246ef9a8468ef2d9edf8a6 to your computer and use it in GitHub Desktop.
Export Safari Reading List bookmarks to pinboard
#!/usr/bin/env python
"""
forked from http://alexwlchan.net/2015/11/export-urls-from-safari-reading-list/
Requires Python 3.
"""
import os
import plistlib
INPUT_FILE = os.path.join(os.environ['HOME'], 'Library/Safari/Bookmarks.plist')
OUTPUT_FILE = 'readinglist.html'
# Load and parse the Bookmarks file
with open(INPUT_FILE, 'rb') as plist_file:
plist = plistlib.load(plist_file)
# Look for the child node which contains the Reading List data.
# There should only be one Reading List item
children = plist['Children']
for child in children:
if child.get('Title', None) == 'com.apple.ReadingList':
reading_list = child
# Extract the bookmarks
bookmarks = reading_list['Children']
# For each bookmark in the bookmark list, grab the URL
urls = ('<dt><a href="' + bookmark['URLString'] + '">' + bookmark['URIDictionary']['title'] + '</a>' for bookmark in bookmarks)
# Write the URLs to a file
with open(OUTPUT_FILE, 'w') as outfile:
outfile.write('<!DOCTYPE NETSCAPE-Bookmark-file-1>\n\n')
outfile.write('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">\n')
outfile.write('<TITLE>Bookmarks</TITLE>\n')
outfile.write('<H1>Bookmarks</H1>\n')
outfile.write('<DL><p>\n')
outfile.write('\n'.join(urls))
outfile.write('\n</DL><p>\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment