Skip to content

Instantly share code, notes, and snippets.

@dehowell
Created December 22, 2009 03:29
Show Gist options
  • Save dehowell/261475 to your computer and use it in GitHub Desktop.
Save dehowell/261475 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""Synchronizes bookmarks from http://pinboard.in/
"""
import base64
import datetime
import sys
import urllib2
from appscript import *
from BeautifulSoup import BeautifulSoup
def get_pinboard_bookmarks(username, password):
"""Fetch the raw HTML from the pinboard.in bookmarks export page.
Arguments:
username -- pinboard.in username
password -- pinboard.in password
"""
req = urllib2.Request('http://pinboard.in/export/')
auth = base64.encodestring('%s:%s' % (username, password))
req.add_header('Authorization', 'Basic %s' % auth)
try:
handle = urllib2.urlopen(req)
data = handle.read()
except IOError, e:
if not hasattr(e, 'code') or e.code != 401:
print "Unknown failure."
if e.code == 401:
print "Wrong username or password."
sys.exit(1)
return data
class Bookmark(object):
"""Represents a bookmark from pinboard.in"""
def __init__(self, title, url, add_date, tags):
self.title = title
self.url = url
self.add_date = add_date
self.tags = tags
def extract_bookmarks(pinboard_data):
soup = BeautifulSoup(pinboard_data)
links = soup.findAll('a')
bookmarks = []
for link in links:
try:
title = link.contents[0]
url = link['href']
add_date = datetime.datetime.fromtimestamp(int(link['add_date']))
tags = link['tags'].split(',')
bookmark = Bookmark(title, url, add_date, tags)
bookmarks.append(bookmark)
except Exception, e:
print e
return bookmarks
def make_yojimbo_bookmark(bookmark):
yb = app('Yojimbo').make(new=k.bookmark_item)
yb.add_tags(bookmark.tags + ['pinboard backup'])
yb.name.set(bookmark.title, resulttype=k.unicode_text)
yb.location.set(bookmark.url)
if __name__ == '__main__':
app('Yojimbo').activate()
d = get_pinboard_bookmarks('username', 'password')
for bookmark in extract_bookmarks(d):
make_yojimbo_bookmark(bookmark)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment