Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sorin-ionescu/925db6a1ad5df509577a to your computer and use it in GitHub Desktop.

Select an option

Save sorin-ionescu/925db6a1ad5df509577a to your computer and use it in GitHub Desktop.
Imports the Safari Reading List into read later services such as Instapaper, Readability, and Pocket.
#!/usr/bin/env python
# encoding: utf-8
#
# safari_reading_list_to_read_later_service.py
#
# Imports the Safari Reading List into read later services such as Instapaper,
# Readability, and Pocket.
#
# (The MIT License)
#
# Copyright (c) 2013 Sorin Ionescu.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import print_function, unicode_literals
import collections
import dns.resolver
import email.mime.text
import os
import plistlib
import re
import smtplib
import subprocess
import sys
PROGRAM = os.path.basename(__file__)
DUMMY_FROM_ADDRESS = '[email protected]'
def parse_safari_reading_list_plist():
urls = []
plist_path = os.path.expanduser('~/Library/Safari/Bookmarks.plist')
plutil_cmd = ['/usr/bin/plutil', '-convert', 'xml1', '-o', '-', plist_path]
plutil = subprocess.Popen(plutil_cmd, stdout=subprocess.PIPE)
plutil_stdout, _ = plutil.communicate()
plist = plistlib.readPlistFromString(plutil_stdout)
if 'Children' in plist:
for child in plist['Children']:
for value in child.values():
if isinstance(value, list):
for item in value:
if 'ReadingList' in item:
urls.append(item['URLString'])
urls.reverse()
return urls
def domain_for_email_address(email_address):
match = re.search('@([^@]+)$', email_address)
if match:
return match.group(1)
return None
def host_for_domain(domain):
answers = dns.resolver.query(domain, 'MX')
for rdata in answers:
return str(rdata.exchange)
def import_urls_to_read_later_service(from_address, to_address, urls):
domain = domain_for_email_address(to_address)
host = host_for_domain(domain)
smtp = smtplib.SMTP(host)
smtp.ehlo_or_helo_if_needed()
for url in urls:
print('Importing: %s' % url)
message = email.mime.text.MIMEText(url)
message['From'] = from_address
message['To'] = to_address
smtp.sendmail(from_address, to_address, message.as_string())
smtp.quit()
def print_error(message):
print('%s: %s' % (PROGRAM, message), file=sys.stderr)
exit(1)
def print_usage():
print(
'Usage: %s <service email address> [your email address]' % PROGRAM,
file=sys.stderr)
exit(1)
def main():
if sys.platform != 'darwin':
print_error('%s is for Mac OS X' % PROGRAM)
argv = collections.deque(sys.argv[1:])
if len(argv) < 1:
print_usage()
to_address = argv.popleft()
try:
from_address = argv.popleft()
except IndexError:
from_address = DUMMY_FROM_ADDRESS
try:
urls = parse_safari_reading_list_plist()
except Exception as e:
print_error('cannot read plist: %s' % e)
try:
import_urls_to_read_later_service(
from_address,
to_address,
urls)
except Exception as e:
print_error('cannot import: %s' % e)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment