Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ctaloi/6070521 to your computer and use it in GitHub Desktop.
Save ctaloi/6070521 to your computer and use it in GitHub Desktop.

INSTANT Pinboard search from Alfred 2

I've had a Python script that makes an HTML Bookmarks file for LaunchBar.
Now that I use Alfred 2, I modified it to make XML for Alfred.
This allows me to search my bookmarks with GREP SPEED!

Installation

First, add your credentials to ~/.netrc

machine pinboard.in
  login LoGiN
  password PaSsWoRD512

Then:

  1. Get pinboard_xml (I've also attached it to the gist in case I change something in my dotfiles repo)
  2. Put in the folder where you keep your scripts (eg. ~/bin)
  3. chmod +x it
  4. Add to crontab (eg. */30 * * * * ~/bin/pinboard_xml)
  5. Make a workflow in Alfred 2 with the following Script Filter (using /bin/bash) to Open URL:
echo "<?xml version=\"1.0\"?>"
echo "<items>"
cat $HOME/.bookmarks.xml | grep "{query}"
echo "</items>"

(or just download the workflow)

Enjoy!

#!/usr/bin/env python
# This came from Greg V's dotfiles:
# https://github.com/myfreeweb/dotfiles
# Feel free to steal it, but attribution is nice
import os
import sys
import json
import netrc
import errno
import requests
def die(msg, err):
sys.stderr.write(msg)
sys.exit(err)
login = netrc.netrc().authenticators('pinboard.in')
if not login:
die('pinboard.in not found in netrc', errno.ENODATA)
try:
getreq = requests.get('https://api.pinboard.in/v1/posts/all?format=json',
auth=(login[0], login[2]))
if getreq.status_code == 429:
die('too often', errno.EPERM)
if getreq.status_code != requests.codes.ok:
die('http error %s on getting, maybe wrong password?' %
getreq.status_code, errno.EACCES)
items = json.loads(getreq.text)
res = '''<?xml version="1.0"?>
<items>'''
for item in items:
href = item['href'].encode('utf-8').replace('\n', '')
desc = item['description'].encode('utf-8').replace('\n', '')
res += '<item uid="%s" arg="%s"><title>%s</title><subtitle>%s</subtitle></item>\n' % \
(href, href, desc, href)
res += '</items>'
open(os.environ['HOME']+'/.bookmarks.xml', 'w').write(res)
except requests.exceptions.RequestException:
die('connection error', errno.ECONNREFUSED)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment