Skip to content

Instantly share code, notes, and snippets.

@dlo
Forked from ttscoff/allpinboard.rb
Last active September 5, 2019 15:26
Show Gist options
  • Save dlo/4629073 to your computer and use it in GitHub Desktop.
Save dlo/4629073 to your computer and use it in GitHub Desktop.
Python version of https://gist.github.com/3773519 that pulls all bookmarks on the first sync, and does incremental updates afterwards. Also uses the Mac OS X keychain to retrieve your password so it doesn't need to live in a file on your computer in plain text.
#!/usr/bin/env python
"""
Requirements:
* pytz
* requests
* dateutil
You should edit the username, bookmark_filename, and local_timezone variables
to suit your preferences.
"""
from datetime import datetime
import subprocess
import os
import pytz
import requests
from dateutil import parser as date_parser
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()
username = "dlo"
bookmark_format = u"<a href='{href}' title='{extended}'>{description}</a>"
bookmark_filename = "~/Dropbox/PinboardBookmarks.html"
local_timezone = "America/Los_Angeles"
basic_auth = (username, password)
url = None
try:
timestamp = os.stat(bookmark_filename).st_mtime
except IOError:
last_modified_on_server = None
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:
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_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