Skip to content

Instantly share code, notes, and snippets.

@gaelicWizard
Forked from dlo/allpinboard.py
Last active August 29, 2015 14:06
Show Gist options
  • Save gaelicWizard/4dd4cf915619fc043b49 to your computer and use it in GitHub Desktop.
Save gaelicWizard/4dd4cf915619fc043b49 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
FORK TODO: Do something like http://pythonwise.blogspot.com/2013/12/reading-passwords-of-osx-keychain.html for username access from keychain.
FORK TODO: Detect OSX-or-not.
This script is designed to generate a simple html file with _all_ of your
Pinboard.in bookmarks The HTML file can be added to Launchbar's index as a
custom bookmark file and you can search your entire Pinboard.in collection
instantly from Launchbar (by title only). It includes any applied tags as part
of the title to aid in searching.
You should edit the `username`, `bookmark_filename`, and `local_timezone`
variables to suit your preferences.
Requirements:
* pytz
* requests
* dateutil
"""
from datetime import datetime
import subprocess
import os
import cgi
import pytz
import requests
from dateutil import parser as date_parser
# Settings
# The path where you'd like to store your HTML export
bookmark_filename = os.environ('HOME')."/Backups/PinboardBookmarks.html"
# Your timezone
local_timezone = "America/Los_Angeles"
# (optional) A tag that you want to export by
tag = None
# Your Pinboard username, if can't pull from keychain.
username = ""
# Your password, if can't pull from keychain.
password = ""
# Use the Mac OS X keychain
keychain_process = subprocess.Popen(["security", "find-internet-password", "-s", "pinboard.in", "-w"], stdout=subprocess.PIPE)
trim_newlines = subprocess.Popen(["tr", "-d", "'\n'"], stdin=keychain_process.stdout, stdout=subprocess.PIPE)
keychain_process.stdout.close()
password, _ = trim_newlines.communicate()
url = None
bookmark_format = u"""<a href="{href}" title="{extended}">{description}</a>\n"""
basic_auth = (username, password)
def quote(text):
return cgi.escape(text, quote=True).replace("\n", " ")
try:
timestamp = os.stat(bookmark_filename).st_mtime
except OSError:
if tag:
url = "https://api.pinboard.in/v1/posts/all?format=json&tag={}".format(tag)
else:
url = "https://api.pinboard.in/v1/posts/all?format=json"
else:
last_modified_local_time = datetime.fromtimestamp(timestamp, \
pytz.timezone(local_timezone))
last_modified = last_modified_local_time.astimezone(pytz.timezone("UTC"))
# Check if bookmarks have been updated remotely since last local update
response = requests.get("https://api.pinboard.in/v1/posts/update?format=json",
auth=basic_auth)
last_modified_on_server = date_parser.parse(response.json()['update_time'])
if last_modified_on_server > last_modified:
if tag:
url = "https://api.pinboard.in/v1/posts/all?format=json&fromdt={}&tag={}" \
.format(last_modified.strftime("%FT%TZ"), tag)
else:
url = "https://api.pinboard.in/v1/posts/all?format=json&fromdt={}" \
.format(last_modified.strftime("%FT%TZ"))
finally:
if url is not None:
response = requests.get(url, auth=basic_auth)
with open(bookmark_filename, 'a+') as bookmark_file:
for bookmark in response.json():
bookmark['description'] = quote(bookmark['description'])
bookmark['extended'] = quote(bookmark['extended'])
bookmark_file.write(bookmark_format.format(**bookmark).encode("utf-8"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment